Split a PDF file (using iText)Tag(s): IO Open Source


This HowTo is based on the iText package. You need a recent version (ex. 2.*)

This a command line utility. You specify the pdf file to be split. Each page is extracted to its own pdf file.

/*
 * java SplitPDFFile file.pdf
 *
 *           gives   file-001.pdf ... file-nnn.pdf
 *
 *  itext-2.1.5.jar
 */

import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;

public class SplitPDFFile {

    /**
     * @param args
     */
    public static void main(String[] args) {

        try {
            String inFile = args[0].toLowerCase();
            System.out.println ("Reading " + inFile);
            PdfReader reader = new PdfReader(inFile);
            int n = reader.getNumberOfPages();
            System.out.println ("Number of pages : " + n);
            int i = 0;
            while ( i < n ) {
                String outFile = inFile.substring(0, inFile.indexOf(".pdf"))
                    + "-" + String.format("%03d", i + 1) + ".pdf";
                System.out.println ("Writing " + outFile);
                Document document = new Document(reader.getPageSizeWithRotation(1));
                PdfCopy writer = new PdfCopy(document, new FileOutputStream(outFile));
                document.open();
                PdfImportedPage page = writer.getImportedPage(reader, ++i);
                writer.addPage(page);
                document.close();
                writer.close();
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        /* example :
            java SplitPDFFile d:\temp\x\tx.pdf

            Reading d:\temp\x\tx.pdf
            Number of pages : 3
            Writing d:\temp\x\tx-001.pdf
            Writing d:\temp\x\tx-002.pdf
            Writing d:\temp\x\tx-003.pdf
         */

    }
}

blog comments powered by Disqus