Split a PDF Page into Multiple Pages in Java

Import Dependency

Method 1: Download the free library and unzip it. Then add the Spire.Pdf.jar file to your project as dependency.
Method 2: Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

<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.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>

Sample Code

Free Spire.PDF for Java supports horizontally or vertically split a single PDF page into multiple pages. The below example shows how to horizontally split a PDF page into two pages.

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("E:\\Files\\input1.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();
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store