Split PDF Document with iText in Java

PDF documents can contain large amounts of pages. When your pdf document exceeds the amount of RAM on your system, these documents can become difficult to view. In this tutorial, we show you how to split a single PDF document into multiple PDF documents.

Split PDF Document

In the following code, we split a single PDF document into multiple PDF documents. Let’s explain what’s happening.

  • First, we read the PDF document using the PdfReader.
  • Get number of pages using the PdfReader#getNumberOfPages().
  • Loop over all the pages. – If you only need to split a range of pages, you can optionally write an if statement from and to which page you want split.
    1. Create a destination filename based upon the original file and append an index number.
    2. Create and initialize the PDF document with the same page size as the original.
    3. Create a PdfCopy object, assign the document and create an FileOutputStream and initialize it with the created destination variable.
    4. Open the PDF document, this makes it eligible for writing.
    5. Read the current page using the PdfCopy#getImportedPge() method. This returns a PdfImportedPage. Add this page to the PdfCopy variable.
    6. Finally, close the document. This results in the file being written to the file system. Don’t bother closing the PdfCopy, this happens implicitly.
package com.memorynotfound.pdf.itext;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

public class SplitPDF {

    public static void main(String... args) throws IOException, DocumentException {

        // read original pdf file
        String filename = "example.pdf";
        URL original = SplitPDF.class.getResource("/" + filename);
        PdfReader reader = new PdfReader(original);

        // get number of pages
        int n = reader.getNumberOfPages();
        System.out.println("Number of pages: " + n);

        // loop over all pages
        int i = 0;
        while (i < n){

            // create destination file name
            String destination = filename.substring(0, filename.indexOf(".pdf")) + "-" + String.format("%03d", i + 1) + ".pdf";
            System.out.println("Writing " + destination);

            // create new document with corresponding page size
            Document document = new Document(reader.getPageSizeWithRotation(1));

            // create writer and assign document and destination
            PdfCopy copy = new PdfCopy(document, new FileOutputStream(destination));
            document.open();

            // read original page and copy to writer
            PdfImportedPage page = copy.getImportedPage(reader, ++i);
            copy.addPage(page);

            // close and write the document
            document.close();
        }
    }
}

Output

The previous code prints the following info to the console. After the program is finished, a single PDF document is split into multiple PDF documents.

Number of pages: 4
Writing example-001.pdf
Writing example-002.pdf
Writing example-003.pdf
Writing example-004.pdf

References

Download

You may also like...