There could be various cases when you need to split a PDF into multiple PDF files. For example, you may have to split each page of the PDF containing invoices/receipts and save them as separate files. On the other hand, you may want to split a particular collection of pages from a PDF file. In order to deal with the above-mentioned scenarios, this article covers how to split a PDF file into multiple PDFs using Java.
- Java API to Split PDF Files
- Split a PDF File by Each Page using Java
- Split a PDF File by Page Collection using Java
- Get Free License
Java API to Split PDF Files – Free Download
In order to split the PDF files, we’ll leverage the PDF manipulation capabilities of Aspose.PDF for Java. The API lets you perform a PDF splitting operation within a few steps. You can either download the API or install it within your Maven-based applications using the following configurations.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>20.12</version>
<classifier>jdk17</classifier>
</dependency>
Split a PDF File using Java
The PDF splitting criteria may vary in different scenarios as per requirements. First, let’s check out how to split a PDF file by pages and save each page as a separate PDF. The following are the steps to perform this operation.
- Load the PDF document using Document class.
- Loop through the pages using Document.getPages() method.
- Access each page into a Page object.
- In every iteration, create a new Document, add the current page to the document and save it as a PDF file using Document.save(String) method.
The following code sample shows how to split a PDF file using Java.
Split a PDF File by Page Collection using Java
For the demonstration, let’s have a look at another PDF splitting scenario. In this example, we will split a PDF based on even and odd page numbers. The following are the steps to perform this operation.
- Load the PDF document using Document class.
- Loop through the pages using Document.getPages() method.
- Separate even and odd pages into ArrayList<Page> objects.
- Create a new Document object, add pages to the document and save it as a PDF file using Document.save(String) method.
The following code sample shows how to split even and odd pages in a PDF file using Java.
Get a Free License
You can get a free temporary license in order to try the API without evaluation limitations.
Conclusion
In this article, you have learned how to split a PDF file using Java. Furthermore, you have seen how to customize the PDF splitting criteria as per requirements. You can explore more about the API using the documentation.