Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove page from PDF

Tags:

java

pdf

itext

I'm currently using iText and I'm wondering if there is a way to delete a page from a PDF file?

I have opened it up with a reader etc., and I want to remove a page before it is then saved back to a new file; how can I do that?

like image 714
AlanFoster Avatar asked Sep 05 '11 21:09

AlanFoster


People also ask

How can I remove a page from a PDF document?

Highlight the page thumbnail or thumbnails you want to delete from your PDF. Click the dustbin icon in the top toolbar to delete the selected PDF pages. Click Save and rename your new PDF file.

How do I extract pages from a PDF for free?

Select the pages you want to put into a new PDF. When you're ready, select Extract. Acrobat automatically creates a new, separate PDF file of your selected pages. Download the new document, get a link to share it, or give it a new file name.


2 Answers

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

like image 67
Ray Hulha Avatar answered Sep 28 '22 06:09

Ray Hulha


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.....

like image 22
Ankit Sharma Avatar answered Sep 28 '22 04:09

Ankit Sharma