如何在java中的现有PDF中添加空白页? [英] How to add blank pages in exist PDF in java?

查看:2231
本文介绍了如何在java中的现有PDF中添加空白页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有普通的PDF文件,我想使用 itext LIBRARY 在PDF末尾插入空白页,而不会打扰PDF内容。

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

推荐答案

Dinup Kandel的答案是错误的,因为它是关于从头开始创建文档。

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

NK123的回答非常错误,因为它使用 PdfWriter / PdfImportedPage 来连接文档。该示例假定原始文档中的所有页面都具有A4大小。情况并非总是如此。如文档所述,这也会抛弃所有交互性。

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.

唯一的好答案如下:

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();

如果 src 是指带有10的文档页面,上面的代码将添加一个额外的空白第11页,使用与第一页相同的页面大小。

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.

这篇关于如何在java中的现有PDF中添加空白页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆