Split PDF into Multiple Files in Java

PDF is among the most famous file formats that support textual, graphical, and many other elements. One of the reasons for its popularity is its portability. In certain cases, you may need to split a large PDF file into multiple files. To address this programmatically, this article discusses different ways of how to split PDF files in Java.

Java API to Split PDF Files

GroupDocs.Merger provides the solution to merge and split files of many different file formats. We will use its Java API to split PDF files in different ways. Download the JAR file from the downloads section, or just use the latest repository and dependency Maven configurations within your Java applications.

<repository>
	<id>GroupDocsJavaAPI</id>
	<name>GroupDocs Java API</name>
	<url>http://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
        <groupId>com.groupdocs</groupId>
        <artifactId>groupdocs-merger</artifactId>
        <version>21.9</version> 
</dependency>

Split PDF File into MultiPage Files in Java

The following steps guide how you can split a PDF file into multipage files:

  • Load the PDF file using Merger class.
  • Define the output file(s) format.
  • Define the page intervals using SplitOptions.
  • Split the loaded PDF according to defined interval using split() method.

The following code sample shows how to split PDF files into multipage files in Java.

Split PDF File into Multiple Single-Page Files in Java

The following steps guide how you can split a PDF to extract pages into multiple single-page files:

  • Load the PDF file using Merger class.
  • Define the output file(s) format.
  • Define the exact page numbers using SplitOptions.
  • Split the loaded PDF according to defined pages using split() method.

The following code sample shows how to split PDF files into multiple single-page files in Java.

Extract Pages from PDF Files by Range in Java

The following steps guide how to extract pages from PDF by splitting according to the given range:

  • Load the PDF file using Merger class.
  • Define the output file(s) format.
  • Provide the pages range using SplitOptions.
  • Use split() method to split the loaded PDF according to the defined range.

The following code snippet shows how to split PDF and extract pages by providing range in Java.

Extract Pages from PDF Files using Even/Odd Filter in Java

The following steps guide how to extract even/odd pages in the given range from PDF file by splitting:

  • Load the PDF file using Merger class.
  • Define the output file(s) format.
  • Provide the pages range using SplitOptions.
  • Apply the even, odd, or all pages filter using RangeMode.
  • Use split() method to split the loaded PDF according to the defined filter.

The following code snippet shows how to extract all the odd/even pages in the defined range of a PDF file using Java.

Code Change Summary

The only thing that differs in the above scenarios is the way to create SplitOptions. You can use the following configurations as per your requirements within your code.

  • For Multipage Files - Use Interval: [1,2], [3,4,5], [6,7], [8,9,10].
new SplitOptions(outputFile,  new int[] { 3, 6, 8 }, SplitMode.Interval)
  • Individual Pages: [3], [6], [8]
new SplitOptions(outputFile, new int[] { 3, 6, 8 });
  • To Extract Pages in Range: [3], [4], [5]
new SplitOptions(outputFile, 3, 5);
  • Range with Filter: [3], [5], [7]
new SplitOptions(outputFile, 3, 7, (Integer)RangeMode.OddPages);

Get a Free API License

You can get a free temporary license in order to use the API without the evaluation limitations.

Conclusion

To sum up, you have learned different ways to split PDF files in Java. First, we split the PDF file into multipage documents as well as in several single-page documents. Then one by one we extracted all the pages, and even/odd pages of the PDF file within the given range. Now you should be confident to build your own PDF splitter Java App using the GroupDocs.Merger API.

To learn more about the API, visit the documentation. For queries, contact us via the forum.

See Also