Remove page from PDF

Get the reader of existing pdf file by

PdfReader pdfReader = new PdfReader("source pdf file path");

Now update the reader by

 pdfReader.selectPages("1-5,15-20");

then get the pdf stamper object to write the changes into a file by

PdfStamper pdfStamper = new PdfStamper(pdfReader,
                new FileOutputStream("destination pdf file path"));

close the PdfStamper by

pdfStamper.close();

It will close the PdfReader too.

Cheers.....


The 'better' way to 'delete' pages is doing

reader.selectPages("1-5,10-12");

Which means we only select pages 1-5, 10-12 effectively 'deleting' pages 6-9.


For iText 7 I found this example:

    PdfReader pdfReader = new PdfReader(PATH + name + ".pdf");
    PdfDocument srcDoc = new PdfDocument(pdfReader);
    PdfDocument resultDoc = new PdfDocument(new PdfWriter(PATH + name + "_cut.pdf"));
    resultDoc.initializeOutlines();

    srcDoc.copyPagesTo(1, 2, resultDoc);

    resultDoc.close();
    srcDoc.close();

See also here: clone-reordering-pages and here: clone-splitting-pdf-file

Tags:

Java

Pdf

Itext