Monday, 13 December 2021

Split a PDF Page into Multiple Pages in Java

Typically, when a page is split into multiple pages, the content of the source page will be displayed on several smaller pages. In this article, you will learn how to split a PDF document into multiple pages horizontally or vertically using Java codes.

DEPENDENCY

To achieve the operation mentioned above, you’re required to download a free third-party library called Free Spire.PDF for Java from the link, and then add Spire.PDF.jar file as a dependency in your Java program. If you use Maven, you can easily add the following code to the pom.xml file to import the JAR 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.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

USING THE CODE

Free Spire.PDF for Java supports splitting a single page into multiple pages horizontally or vertically by using methods provided by PdfDocument class. The following are detailed steps for your reference.

l  Create a PdfDocument instance and load a sample PDF document using PdfDocument.loadFromFile() method.

l  Get a specific page of PDF using PdfDocument.getPages().get() method.

l  Create another PdfDocument instance and set the page margins as 0.

l  Set the page size of the new PDF as half or a fraction of the original page.

l  Add a new page to the new PDF document using PdfDocument.getPages().add() method.

l  Create a PdfTextLayout instance and set the PdfLayoutType as Paginate to make the content paginated automatically.

l  Draw the content of the source page on the new page using PdfPageBase.createTemplate().draw() method.

l  Save the document to another file using PdfDocument.saveToFile() method.

import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import java.awt.geom.Point2D;

public class SplitPDFPage {
public static void main(String[] args) {
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();

//Load the sample PDF document
pdf.loadFromFile("C:\\Users\\Test1\\Desktop\\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 as 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 document to another file
newPdf.saveToFile("output/SplitPDFPage.pdf");
newPdf.close();

}
}





No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...