3

I have normal PDF file, i want to insert blank pages at the end of PDF using itext LIBRARY, without disturbing the PDF contents.

KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52

2 Answers2

12

The answer by Dinup Kandel is wrong because it's about creating a document from scratch.

The answer by NK123 is very wrong because it uses PdfWriter/PdfImportedPage to concatenate documents. That example assumes that all pages in the original document have the size A4. This won't always be the case. As documented, this also throws away all interactivity.

The only good answer looks like this:

PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
stamper.insertPage(reader.getNumberOfPages() + 1, reader.getPageSizeWithRotation(1));
stamper.close();
reader.close();

If src refers to a document with 10 pages, the code above will add an extra blank 11th page, using the same page size as the first page.

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • 4
    That's not a good attitude. It means you're not willing to learn something for yourself, but counting on other people to help you instead. A real master teaches pupils by asking them questions, not by answering theirs. This wisdom goes back to the age of Socrates. I fear I'm not a real master if I continue answering questions. – Bruno Lowagie May 24 '13 at 12:28
  • http://stackoverflow.com/questions/16710439/how-to-add-blank-page-in-digitally-signed-pdf-using-java is this possible?i thing no, but wanted to confirm it from your side? – KhAn SaAb May 24 '13 at 12:43
  • I upvoted mkl's answer. You're trying to do something that invalidates the signature. – Bruno Lowagie May 24 '13 at 12:51
  • Yes.that's what i want to get from u.Thank you for your support.And thanks a lot for your itext library. – KhAn SaAb May 24 '13 at 12:54
  • You are welcome to look at [Bruno's book](http://www.manning.com/lowagie2/samplechapter6.pdf) if you'd like. (Moving this resource to a comment). – Qix - MONICA WAS MISTREATED Jul 08 '15 at 21:09
0

well i have searched the answer and found something like this but don't know if it will work or not

public static void main(String[] args) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer
            = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph("This page will NOT be followed by a blank page!"));
        document.newPage();
        // we don't add anything to this page: newPage() will be ignored
        document.newPage();
        document.add(new Paragraph("This page will be followed by a blank page!"));
        document.newPage();
        writer.setPageEmpty(false);
        document.newPage();
        document.add(new Paragraph("The previous page was a blank page!"));
        // step 5
        document.close();

    }
Dinup Kandel
  • 2,457
  • 4
  • 21
  • 38