3

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

4

2 回答 2

12

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

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

唯一好的答案如下所示:

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 页空白,使用与第一页相同的页面大小。

于 2013-05-24T10:52:16.497 回答
0

好吧,我已经搜索了答案并找到了类似的东西,但不知道它是否会起作用

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

    }
于 2013-05-24T09:06:26.563 回答