I have normal PDF file, i want to insert blank pages at the end of PDF using itext LIBRARY
, without disturbing the PDF contents.
Asked
Active
Viewed 7,032 times
3

KhAn SaAb
- 5,248
- 5
- 31
- 52
-
What have you tried? Have you tried to import the PDF page by page, insert blank pages when needed, and stitch all back together? – Alexis Pigeon May 24 '13 at 09:18
-
i have tried this http://itextpdf.com/examples/iia.php?id=99 but i want to insert pages in exist PDF without disturbing existing content. – KhAn SaAb May 24 '13 at 09:20
-
What do you mean by "disturbing the content"? – Alexis Pigeon May 24 '13 at 09:21
-
1And please try to spell it correctly, it's iText, not `ITEXT LIBRARY`, nor `itext LIBRARY` :) – Alexis Pigeon May 24 '13 at 09:22
-
suppose i have "10" pages in my PDF, i want to add "1" more in it.after adding pages become "11" like wise m looking for this. – KhAn SaAb May 24 '13 at 09:22
-
so you want the page numbering to be updated too? – Alexis Pigeon May 24 '13 at 09:23
-
yes i thing now u got my requirement – KhAn SaAb May 24 '13 at 09:24
-
Then iText, and PDF in general, is not the correct tool/format for what you intend to do. PDF is a publication format, not an edition format. – Alexis Pigeon May 24 '13 at 09:28
-
@alexisPigeon after our satisfactory edition we can publish it, first step is edition.am now in edition phase.:) – KhAn SaAb May 24 '13 at 09:31
-
There's a flaw in you process if it consists of creating AND later editing a PDF file. It should only consist of a creation phase. – Alexis Pigeon May 24 '13 at 09:37
-
@Alexis Pigeon i just want to add blank pages at the end of uploaded pdf. – KhAn SaAb May 24 '13 at 09:42
2 Answers
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
-
4That'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
-
-
-
no :( this example creating new pdf. and disturbing the existing content. – KhAn SaAb May 24 '13 at 09:10
-
You may have to create a new Pdf ->> Copy contents of old Pdf into it ->> Add new page at the end ->> Save new pdf with the same name. – Shreyas Dave May 24 '13 at 09:21
-
ok, u want me to copy paste all content, u dont thing it takes to much time for large files.? – KhAn SaAb May 24 '13 at 09:26
-
Yeah, I don't think there any way to edit PDF after once generated. – Shreyas Dave May 24 '13 at 10:26