DEV Community

Alexis
Alexis

Posted on • Edited on

Java - How to Split a PDF into Multiple PDFs

Sometimes you may only need to share certain pages or sections of a PDF file, rather than the entire document. By splitting the PDF file, you can easily extract the information you need and share it with others, while keeping the rest of the file private. In this article, I will introduce how to split a PDF file into multiple PDF files using Java.

Java Library to Split PDF

In order to split PDF files, this article uses a third-party library called Spire.PDF for Java, which is a feature-rich library for creating, manipulating, converting, and printing PDF files in Java applications.

Before coding, you need to add needed dependencies for including Spire.PDF for Java into your Java project. There are two ways to do that.

Method 1: If you are using maven, you can easily import the JAR file of Spire.PDF for Java into 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>9.3.11</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Method 2: If you are not using maven, you can download Spire.PDF for Java from the official website, extract the zip file and then import the Spire.Pdf.jar file under the lib folder into your project as a dependency.

Split a PDF File by Each Page in Java

You can split a PDF file by each page using the PdfDocument.split(String destFilePattern, int startNumber) method. After splitting, each page in the PDF file will be saved as a separate PDF file.

The following steps demonstrate how to split a PDF file by each page:

  • Initialize an instance of the PdfDocument class and pass the PDF file’s path to the constructor of the class as a parameter.
  • Split every page of the PDF file into a separate file using the PdfDocument.split(String destFilePattern, int startNumber) method.
import com.spire.pdf.PdfDocument;

public class SplitPdfByEachPage {
    public static void main(String[] args) {
        //Initialize an instance of the PdfDocument class and pass the PDF file’s path to the constructor of the class as a parameter
        PdfDocument doc = new PdfDocument("Sample.pdf");

        //Split every page of the PDF into a separate file
        doc.split("Output/Split-{0}.pdf", 1);
        doc.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

Split PDF by Each Page in Java

Split a PDF File by Page Ranges in Java

Spire.PDF for Java offers the PdfDocument.insertPageRange(PdfDocument doc, int startIndex, int endIndex) method which enables you to import a range of pages from a PDF file into another PDF file. By using this method, you can easily split a PDF file by page ranges.

The detailed steps demonstrate how to split a PDF file by two page ranges (page 1~2, and page 3~5):

  • Initialize an instance of the PdfDocument class and pass the PDF file’s path to the constructor of the class as a parameter.
  • Initialize an instance of the PdfDocument class to create a new PDF file.
  • Import the 1st and 2nd pages of the source PDF document to the new PDF file using the PdfDocument.insertPageRange(PdfDocument doc, int startIndex, int endIndex) method.
  • Save the new PDF file using the PdfDocument.SaveToFile() method.
  • Initialize an instance of the PdfDocument class to create a new PDF file.
  • Import the 3rd, 4th, and 5th pages of the source PDF document to the new PDF file using the PdfDocument.insertPageRange(PdfDocument doc, int startIndex, int endIndex) method.
  • Save the new PDF file using the PdfDocument.SaveToFile() method.
import com.spire.pdf.PdfDocument;

public class SplitPdfByPageRanges {
    public static void main(String[] args) {
        //Initialize an instance of the PdfDocument class and pass the PDF file’s path to the constructor of the class as a parameter
        PdfDocument doc = new PdfDocument("Sample.pdf");

        //Initialize an instance of the PdfDocument class to create a new PDF file
        PdfDocument newDoc1 = new PdfDocument();
        //Import 1-2 pages of the source PDF to the new PDF
        newDoc1.insertPageRange(doc, 0, 1);
        //Save the result document
        newDoc1.saveToFile("File1.pdf");

        //Initialize an instance of the PdfDocument class to create a new PDF file
        PdfDocument newDoc2 = new PdfDocument();
        //Import 3-5 pages of the source PDF to the new PDF
        newDoc2.insertPageRange(doc, 2, 4);
        //Save the result document
        newDoc2.saveToFile("File2.pdf");
    }
}
Enter fullscreen mode Exit fullscreen mode

Split PDF by Page Ranges in Java

Split a PDF Page into Multiple Pages in Java

The way to split a PDF page into multiple pages is to create a new PDF file with a smaller page size, then draw different parts of the content on the source PDF page to different pages of the new PDF file using the PdfPageBase.createTemplate().draw() method.

The following steps demonstrate how to split a PDF page into two pages:

  • Initialize an instance of the PdfDocument class.
  • Load a PDF file using the PdfDocument.loadFromFile() method.
  • Get a specific page using the PdfDocument.getPages().get() method.
  • Initialize an instance of the PdfDocument class to create a new PDF file, and set its page margins to 0.
  • Set the page size of the new PDF to half or a fraction of that of the source PDF.
  • Add a page to the new PDF document using the PdfDocument.getPages().add() method.
  • Draw the content of the specific page of the source PDF on the newly added page of the new PDF using the PdfPageBase.createTemplate().draw() method.
  • Save the document to another file using PdfDocument.saveToFile() method.
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfLayoutBreakType;
import com.spire.pdf.graphics.PdfLayoutType;
import com.spire.pdf.graphics.PdfTextLayout;

import java.awt.geom.Point2D;

public class SplitPdfPage {
    public static void main(String[] args) {
        //Initialize an instance of the PdfDocument class
        PdfDocument pdf = new PdfDocument();
        //Load the source PDF file
        pdf.loadFromFile("Input.pdf");

        //Get the first page
        PdfPageBase page = pdf.getPages().get(0);

        //Initialize an instance of the PdfDocument class to create a new PDF file
        PdfDocument newPdf = new PdfDocument();
        //Remove page margins
        newPdf.getPageSettings().getMargins().setAll(0);

        //Set the page width of the new PDF as the same as that of the source PDF
        newPdf.getPageSettings().setWidth((float) page.getSize().getWidth());
        //Set the page height of the new PDF as 1/2 of the source PDF
        newPdf.getPageSettings().setHeight((float) page.getSize().getHeight()/2);

        //Add a page to the new PDF
        PdfPageBase newPage = newPdf.getPages().add();

        //Set the PdfLayoutType to Paginate to make the content paginated automatically
        PdfTextLayout layout = new PdfTextLayout();
        layout.setBreak(PdfLayoutBreakType.Fit_Page);
        layout.setLayout(PdfLayoutType.Paginate);

        //Draw the content of the source page onto the page of the new PDF
        page.createTemplate().draw(newPage, new Point2D.Float(0, 0), layout);

        //Save the new PDF to file
        newPdf.saveToFile("SplitPdfPage.pdf");
        newPdf.close();
    }
}
Enter fullscreen mode Exit fullscreen mode

Split PDF Page into Multiple Pages

Conclusion

This article introduced how to split a PDF file by each page and by page ranges, along with how to split a PDF page to multiple pages in Java using Spire.PDF for Java library. In addition to splitting PDF files, this library also supports merging PDF files along with many other manipulations on PDF files, it’s worth giving them a try by yourself.

Top comments (0)