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…


… Then our dependency:


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.