Java: Split a PDF Page into Multiple Pages

Spire.PDF for java offers PdfDocument.split() method to split one PDF document to multiple files by pages. In this article, we will demonstrate how to horizontally or vertically split a single PDF page into multiple pages in Java by using Spire.PDF for Java.

Install Spire.PDF for Java

First of all, you need 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 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.0</version>
    </dependency>
</dependencies>
    

Split a PDF Page into multiple Pages

Spire.PDF for Java offers PdfPageBase.createTemplate().draw() method to draw the content of a source page on a new PDF page. Splitting a page into multiple pages actually means that the content of the source page will be drew on multiple smaller pages. The following are the main steps to split the first page into two pages:

  • Create an object of PdfDocument class and load a sample PDF document using PdfDocument.loadFromFile() method.
  • Get the desired page of PDF using PdfDocument.getPages().get() method.
  • Create a new PDF document and set the page margins to 0.
  • Set the page size of the new PDF to half or a fraction of that of the original.
  • Add a new page to the new PDF document using PdfDocument.getPages().add() method.
  • Draw content of source page on the new page using PdfPageBase.createTemplate().draw() method.
  • Save the document to another file using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import java.awt.geom.Point2D;

public class SplitPDF {
    public static void main(String[] args) throws Exception {

        //Create an object of PdfDocument class.
        PdfDocument pdf = new PdfDocument();

        //Load the sample PDF document
        pdf.loadFromFile("Sample.pdf");

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

        //Create a new PDF document and remove page margins
        PdfDocument newPdf = new PdfDocument();
        newPdf.getPageSettings().getMargins().setAll(0);

        //Horizontally Split
        newPdf.getPageSettings().setWidth((float) page.getSize().getWidth());
        newPdf.getPageSettings().setHeight((float) page.getSize().getHeight()/2);

        ////Vertically Split
        //newPdf.getPageSettings().setWidth((float) page.getSize().getWidth()/2);
        //newPdf.getPageSettings().setHeight((float) page.getSize().getHeight());

        // Add a new page to the new PDF document
        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 source page in the new page
        page.createTemplate().draw(newPage, new Point2D.Float(0, 0), layout);

        //Save the Pdf document
        newPdf.saveToFile("SplitPDF.pdf");
        newPdf.close();

    }
}

Java: Split a PDF Page into Multiple Pages

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.