DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

CodeSharing
CodeSharing

Posted on

How to Split a PDF File in Java Application

In daily work, when we are manipulating a large PDF file, we can split it into multiple small files to make the reading or editing easier. Today I'm going to introduce two ways to split a PDF file by using Free Spire.PDF for Java.

1. Split every page of the PDF into a separate file.
2. Split specific page ranges of the PDF into multiple files.

Installation
Method 1: Download the Free Spire.PDF for Java and unzip it.Then add the Spire.Pdf.jar file to your project as dependency.

Method 2: You can also 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>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf.free</artifactId>
        <version>2.6.3</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

The original PDF file (including 5 pages):

【Example 1】Split every page of the PDF into a separate file:
Split the original PDF file into 5 small documents

import com.spire.pdf.*;

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

        //Load the PDF file
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile("input1.pdf");

        //Split every page of the PDF into a separate file
        doc.split("output/splitDocument-{0}.pdf", 0);
        doc.close();
    }
}

Enter fullscreen mode Exit fullscreen mode

【Example 2】Split specific page ranges of the PDF into multiple files:
Split pages 1-2 into a PDF file, and split pages 3-5 into another PDF file.

import com.spire.pdf.*;
import com.spire.pdf.graphics.PdfMargins;

import java.awt.geom.Point2D;

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

        //Load the PDF file
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile("input1.pdf");

        //Create a new PDF file
        PdfDocument newDoc1 = new PdfDocument();

        PdfPageBase page;

        //Add 2 pages to the new PDF, and draw the content of page 1-2 of the original PDF to the newly added pages
        for(int i = 0;i<2;i++)
        {
            page = newDoc1.getPages().add(doc.getPages().get(i).getSize(), new PdfMargins(0));
            doc.getPages().get(i).createTemplate().draw(page, new Point2D.Float(0,0));
        }

        //Save the result file
        newDoc1.saveToFile("split/Doc1.pdf");

        //Create a new PDF file
        PdfDocument newDoc2 = new PdfDocument();

        //Add 3 pages to the new PDF, and draw the content of page 3-5 of the original PDF to the newly added pages
        for(int i = 2;i<5;i++)
        {
            page = newDoc2.getPages().add(doc.getPages().get(i).getSize(), new PdfMargins(0));
            doc.getPages().get(i).createTemplate().draw(page, new Point2D.Float(0,0));
        }

        //Save the result file
        newDoc2.saveToFile("split/Doc2.pdf");
    }
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Regex for lazy developers

regex for lazy devs

You know who you are. Sorry for the callout πŸ˜†