Thang Pham
Thang Pham

Reputation: 38705

iText: split a PDF into several PDF (1 per page)

What I want is that: given a 10-pages-pdf-file, I want to display each page of that pdf inside a table on the web. What is the best way to achieve this? I guess one way is to split this 10-pages-pdf-file into 10 1-pages pdf, and programmatically display each pdf onto a row of a table. Can I do this with iText? Is there a better way to accomplish this?

Upvotes: 3

Views: 34270

Answers (4)

MauroB
MauroB

Reputation: 580

I created this method to work with byte[]. It receives a byte array (obtained from a file in the filesystem or a decoded base64 string) and produces a list of byte[] for other operations.

private static List<byte[]> splitPdf(final byte[] pdf) throws IOException, DocumentException {
    final var pdfList = new ArrayList<byte[]>();
    final var pdfReader = new PdfReader(pdf);
    final var numberOfPages = pdfReader.getNumberOfPages();
    for (int currentPage = 1; currentPage <= numberOfPages; currentPage++) {
        try (var byteArrayOutputStream = new ByteArrayOutputStream();) {
            var document = new Document(pdfReader.getPageSizeWithRotation(currentPage));
            var pdfCopy = new PdfCopy(document, byteArrayOutputStream);
            document.open();
            pdfCopy.addPage(pdfCopy.getImportedPage(pdfReader, currentPage));
            document.close();
            pdfCopy.close();
            pdfList.add(byteArrayOutputStream.toByteArray());
        }
    }
    return pdfList;
}

Upvotes: 0

RealHowTo
RealHowTo

Reputation: 35372

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
         */
        
    }
}

Upvotes: 15

Jvo
Jvo

Reputation: 58

I can't comment, but this line in the most voted answer

Document document = new Document(reader.getPageSizeWithRotation(1));

should be

Document document = new Document(reader.getPageSizeWithRotation(i+1));

to get the correct pdf size if other pages have different page size (it know it's rare)

Upvotes: 0

Bittu Choudhary
Bittu Choudhary

Reputation: 65

With PDDocument you can do so very easily.

You just have to use a Java List of PDDocument type and Splitter function to split a document.

List<PDDocument> Pages=new ArrayList<PDDocument>();
PDDocument.load(filePath);
try {
    Splitter splitter = new Splitter(); 
    Pages = splitter.split(document);
}
catch(Exception e) {
    e.printStackTrace(); // print reason and line number where error exist
}

Upvotes: 1

Related Questions