How to Remote, Delete Pages from a PDF Document with Java
Is your application/website handling a lot of PDF documents? If you answered ‘yes,’ then you might want to give our PDF Editing APIs a look. We have more than a dozen API iterations designed specifically to help developers include efficient cloud-based PDF services into their projects. In this article, we’ll highlight and demonstrate one such iteration which can be employed to automatically remove/delete a specific number of pages from a PDF document based on custom specifications. To help you structure an API call, we’ve provided complementary Java code snippets below.
Before we begin, keep in mind there are four required parameters for this API:
- Your PDF file
- The page to start deleting from
- The page to end deleting on
- Your Cloudmersive API key for authentication (register a free account on our website to receive one)
Once you have that information squared away, you can begin by installing Maven. Add the below reference to the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Then, add the following reference to the pom.xml dependency:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
Up next, include the import classes:
// 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;
Finally, call the API, and ensure your parameters are properly configured:
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 inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
Integer pageStart = 56; // Integer | Page number (1 based) to start deleting pages from (inclusive).
Integer pageEnd = 56; // Integer | Page number (1 based) to stop deleting pages from (inclusive).
try {
byte[] result = apiInstance.editPdfDeletePages(inputFile, pageStart, pageEnd);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling EditPdfApi#editPdfDeletePages");
e.printStackTrace();
}