使用 Java 导入和导出批注

将批注从一个 PDF 复制到另一个 PDF

  1. 打开源 PDF Document。
  2. 将一个 Page 添加到目标 Document。
  3. 将每个 Annotation 添加到目标 Page。
  4. 读取或遍历 Annotation 目标页上的项目。
  5. 保存更新后的 PDF Document。
  6. 枚举第一个源页面上的 Annotation 对象,并将每个对象添加到目标页面。
public static void importExport(Path inputFile, Path outputFile) {
    try (Document sourceDocument = new Document(inputFile.toString());
         Document destinationDocument = new Document()) {
        Page page = destinationDocument.getPages().add();

        for (Annotation annotation : sourceDocument.getPages().get_Item(1).getAnnotations()) {
            page.getAnnotations().add(annotation, true);
        }

        destinationDocument.save(outputFile.toString());
    }
}