By splitting PDF pages into separate files, you get smaller PDF documents that have one or some pages extracted from the original. A split file contains less information and is naturally smaller in size and easier to share over the internet. In this article, you will learn how to split PDF into single-page PDFs and how to split PDF by page ranges in Java using Spire.PDF for Java.
Install Spire.PDF for Java
First, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from ;this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.pdf</artifactId> <version>10.10.7</version> </dependency> </dependencies>
Split a PDF File into Multiple Single-Page PDFs in Java
Spire.PDF for Java offers the split() method to divide a multipage PDF document into multiple single-page files. The following are the detailed steps.
- Create a PdfDcoument object.
- Load a PDF document using PdfDocument.loadFromFile() method.
- Split the document into one-page PDFs using PdfDocument.split(string destFilePattern, int startNumber) method.
- Java
import com.spire.pdf.PdfDocument; public class SplitPdfByEachPage { public static void main(String[] args) { //Specify the input file path String inputFile = "C:\\Users\\Administrator\\Desktop\\Terms of Service.pdf"; //Specify the output directory String outputDirectory = "C:\\Users\\Administrator\\Desktop\\Output\\"; //Create a PdfDocument object PdfDocument doc = new PdfDocument(); //Load a PDF file doc.loadFromFile(inputFile); //Split the PDF to one-page PDFs doc.split(outputDirectory + "output-{0}.pdf", 1); } }
Split a PDF File by Page Ranges in Java
No straightforward method is offered for splitting PDF documents by page ranges. To do so, we create two or more new PDF documents and import the selected page or page range from the source document into them. Here are the detailed steps.
- Load the source PDF file while initialing the PdfDocument object.
- Create two additional PdfDocument objects.
- Import the first page from the source file to the first document using PdfDocument.insertPage() method.
- Import the remaining pages from the source file to the second document using PdfDocument.insertPageRange() method.
- Save the two documents as separate PDF files using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.PdfDocument; public class SplitPdfByPageRange { public static void main(String[] args) { //Specify the input file path String inputFile = "C:\\Users\\Administrator\\Desktop\\Terms of Service.pdf"; //Specify the output directory String outputDirectory = "C:\\Users\\Administrator\\Desktop\\Output\\"; //Load the source PDF file while initialing the PdfDocument object PdfDocument sourceDoc = new PdfDocument(inputFile); //Create two additional PdfDocument objects PdfDocument newDoc_1 = new PdfDocument(); PdfDocument newDoc_2 = new PdfDocument(); //Insert the first page of source file to the first document newDoc_1.insertPage(sourceDoc, 0); //Insert the rest pages of source file to the second document newDoc_2.insertPageRange(sourceDoc, 1, sourceDoc.getPages().getCount() - 1); //Save the two documents as PDF files newDoc_1.saveToFile(outputDirectory + "output-1.pdf"); newDoc_2.saveToFile(outputDirectory + "output-2.pdf"); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.