How to Split PDF Files into Separate PDFs (One per Page) using Java

Cloudmersive
2 min readAug 31, 2022

--

Consolidated PDF files are great for sharing a variety of content types in a single document. Sometimes, however, we only need a small slice of that content for our team’s purposes — in which case it’s useful to perform the opposite operation & divide the PDF pages into separate files. You can accomplish that transition with ease using our PDF Splitter API, which will simply return each individual page as a new file (or URL, if you choose to set ‘returnDocumentContents’ to true; this is more efficient for larger operations).

Below, I’ll demonstrate how you can use this API & structure your API call in Java. All you need is your input file & your Cloudmersive API key (which you can get for free by registering a free account on our website).

Our first task is to install the SDK with Maven. We can begin to do that by first adding a reference to the pom.xml repository:

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

And then adding 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>

Once that’s done, we can now add the imports to the top of our file and call the document conversion function. Include your API key where indicated below the imports, and configure your file path input in the following snippet:

// 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: 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");
SplitDocumentApi apiInstance = new SplitDocumentApi();
File inputFile = new File("/path/to/inputfile"); // 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();
}

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet