How to split a PDF file into separate page PDF files in Java

By the end of today’s article, you will be fully set up for splitting up PDF files into their constituent pages. Let’s get this done as quickly as possible. Without any further ado, let’s dive straight in.

Let’s open our Maven POM files and add our references, first our repository…

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

… Then our dependency:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.54</version>
</dependency>
</dependencies>

And now we have access to our library, so we can go ahead with invoking our function using the example code you see below:

// 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.SplitDocumentApi;ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: ApikeyApiKeyAuth 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");SplitDocumentApi apiInstance = new SplitDocumentApi();File inputFile = new File("/path/to/file.txt"); // File | Input file to perform the operation on.Boolean returnDocumentContents = true; // Boolean | Set to true to directly return all of the document contents in the DocumentContents field; set to false to return contents as temporary URLs (more efficient for large operations).  Default is false.try {SplitPdfResult result = apiInstance.splitDocumentPdfByPage(inputFile, returnDocumentContents);System.out.println(result);} catch (ApiException e) {System.err.println("Exception when calling SplitDocumentApi#splitDocumentPdfByPage");e.printStackTrace();}

And you are already finished. This same library also contains other document splitting functions, including DOCX and PPTX.

Image for post
Image for post

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store