使用iText复制带注释的PDF [英] Copy PDF with Annotations using iText

查看:350
本文介绍了使用iText复制带注释的PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要将现有的多个PDF导入到一个新的PDF中。部分代码的工作方式类似于 iText in Action 2nd edition 第6.2.1节中的示例代码:

We need to import existing multiple PDFs into a single new PDF. Part of codes work similar to the sample codes in section 6.2.1 of iText in Action 2nd edition:

Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(
    document, new FileOutputStream(RESULT));
document.open();
PdfPTable table = new PdfPTable(2);
PdfReader reader = new PdfReader(MovieTemplates.RESULT);
int n = reader.getNumberOfPages();
PdfImportedPage page;
for (int i = 1; i <= n; i++) {
    page = writer.getImportedPage(reader, i);
    table.addCell(Image.getInstance(page));
}
document.add(table);
document.close();

然而,我们刚刚意识到处理带注释的可填写PDF(在我们的例子中,那些是PDF)已填写数据),所有填写的数据都会以新PDF格式丢失。

However, we just realized when dealing with fill-able PDFs with annotations (in our case, those PDF already have data filled), all the filled data are lost in new PDF.

我们在本书的相同部分找到答案:

We found the answer in the same section of the book:


了解呈现页面内容所需资源与页面交互功能之间的区别非常重要。通常,这些功能称为注释。它们包括链接,文本注释和表单字段。注释不是内容流的一部分。它们未列在页面的资源字典中,而是列在注释字典中。使用 PdfImportedPage 时,不会复制这些交互式功能,这意味着在使用 getImportedPage() PdfWriter 类的方法。

It's important to understand the difference between resources needed to render the content of a page and the interactive features of a page. In general, these features are called annotations. They include links, text annotations, and form fields. Annotations aren't part of the content stream. They aren't listed in the resources dictionary of the page, but in the annotation dictionary. These interactive features aren't copied when using PdfImportedPage, which means that all interactivity is lost when copying a page with the getImportedPage() method of the PdfWriter class.

但是解决方法是什么保留那些注释?

But what is the solution to keep those annotations?

推荐答案

作为你所引用的书的作者,我想指出这些例子在书中有点过时了。本书将建议您使用 PdfCopyFields 来合并表单,但在最新版本的iText中不推荐使用该类。

Being the author of the book you refer to, I'd like to point out that the examples in the book are somewhat outdated. The book will advise you to use PdfCopyFields to merge forms, but that class is deprecated in recent versions of iText.

请看一下新的例子:

  • MergeForms
  • MergeForms2

换句话说:现在可以使用 PdfCopy class,但导入它是为了告诉 PdfCopy 这些字段需要合并,如下面的代码片段所示:

In other words: forms can now be copied/merged using the PdfCopy class, but it is imported to tell PdfCopy that the fields need to be merged as is done in the following code snippet:

public void createPdf(String filename) throws IOException, DocumentException {
    PdfReader[] readers = {
        new PdfReader(getFile1()),
        new PdfReader(getFile2())
    };
    createPdf(filename, readers);
}

public void createPdf(String filename, PdfReader[] readers)
    throws IOException, DocumentException {
    Document document = new Document();
    PdfCopy copy = new PdfCopy(document, new FileOutputStream(filename));
    copy.setMergeFields();
    document.open();
    for (PdfReader reader : readers) {
        copy.addDocument(reader);
    }
    document.close();
    for (PdfReader reader : readers) {
        reader.close();
    }
}

setMergeFields() method是你需要记住的方法。

The setMergeFields() method is the method you need to remember.

这篇关于使用iText复制带注释的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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