Split a PDF Page into Multiple Pages in Java

PDF files containing multiple pages can be split into multiple different PDF files using Free Spire.PDF for Java, and if you want to split a PDF page into multiple pages, the free library is also capable of doing that.

Import Dependency

<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

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