How to add a blank page to an existing PDF in Java?

I have ordinary PDF files. I want to use iText library to insert a blank page at the end of the PDF without disturbing the PDF content

Solution

Dinup Kandel's answer is wrong because it's about creating documents from scratch

Nk123's answer is very wrong because it uses pdfwriter / pdfimportedpage to connect documents This example assumes that all pages in the original document have A4 size This is not always the case As documented, this also discards all interactivity

The only good answer is 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 references a document with 10 pages, the above code will add additional blank page 11 with the same page size as the first page

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>