How to Insert, Copy Pages from One PDF Document into Another using Java

Cloudmersive
2 min readAug 25, 2022

If you’ve ever tried to hastily include additional content in a PDF document, you’ve probably realized how finicky and frustrating that task can be; the resulting content hardly ever behaves the way you want it to. If you have a stack of 10 more PDFs that need to benefit from the same changes, you’ve likely all but given up on completing your task efficiently. So, how can you get around this? Your best bet is to employ a PDF conversion API, which can communicate with the document in its compressed form, removing the headache of making constant back and forth formatting conversions.

Via the below demonstration, you’ll learn how to easily utilize a PDF editing API which inserts pages from one PDF document into another seamlessly. This API allows you to customize which specific pages from the source document will enter the destination document, and where in the destination document those pages will be inserted. Your resulting document will retain all the same dimensions as the original (source and destination files), removing any stress in the conversion whatsoever. This demo will show how to utilize Java code examples to structure your API call.

Let’s begin by installing the SDK with Maven. First add a reference to the pom.xml repository:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Next, add a reference to the pom.xml dependency:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

Now you can include the imports and call the function:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.EditPdfApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
EditPdfApi apiInstance = new EditPdfApi();
File sourceFile = new File("/path/to/inputfile"); // File | Source PDF file to copy pages from.
File destinationFile = new File("/path/to/inputfile"); // File | Destination PDF file to copy pages into.
Integer pageStartSource = 56; // Integer | Page number (1 based) to start copying pages from (inclusive) in the Source file.
Integer pageEndSource = 56; // Integer | Page number (1 based) to stop copying pages pages from (inclusive) in the Source file.
Integer pageInsertBeforeDesitnation = 56; // Integer | Page number (1 based) to insert the pages before in the Destination file.
try {
byte[] result = apiInstance.editPdfInsertPages(sourceFile, destinationFile, pageStartSource, pageEndSource, pageInsertBeforeDesitnation);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling EditPdfApi#editPdfInsertPages");
e.printStackTrace();
}

And that’s all you need — no more code required. In order to get your secure API key, simply visit our website and register a free account (you’ll receive a limit of 800 API calls per month).

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.