PDFBox:如何将注释从一个 pdf 正确复制到另一个 [英] PDFBox: how to properly copy annotation from one pdf to another

查看:112
本文介绍了PDFBox:如何将注释从一个 pdf 正确复制到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将注释从一个 pdf 复制到另一个.但是即使复制 1 个注释也会使输出 pdf 文件的大小加倍.

I am trying to copy annotations from one pdf to another. But copying even 1 annotation DOUBLES the size of outputing pdf file.

请在下面找到简单的代码示例:

Please find below simple code sample:

    PDDocument pdf = PDDocument.load(new File("test1.pdf"));
    PDDocument pdf2 = PDDocument.load(new File("test/test1.pdf"));
    List<PDAnnotation> pdfAnnotations1 = pdf.getPage(0).getAnnotations();
    List<PDAnnotation> pdfAnnotations2 = pdf2.getPage(0).getAnnotations();

    pdfAnnotations1.add(pdfAnnotations2.get(0));
    pdf.save("test1.pdf");

如果我尝试使用 Adob​​e Reader 打开此输出文件并再次保存 - 大小会恢复正常.有什么想法吗?非常感谢您提供任何帮助.

If I try to open this output file with Adobe Reader and save it again - size comes back to normal. Any thoughts? Thank you very much in advance for any help.

推荐答案

每个注释都指向它所在的页面.因此,您还需要通过调用 pdfAnnotations1.get(0).setPage(pdf.getPage(0)) 来纠正该问题.

Each annotation points back to the page where it is. So you need to correct that as well by calling pdfAnnotations1.get(0).setPage(pdf.getPage(0)).

大小增加是因为没有我描述的调用,注解会指向旧页面,旧页面又指向父页面等

The size increase is because without the call I described, the annotation will point back to the old page, which points back to its parent, etc.

这篇关于PDFBox:如何将注释从一个 pdf 正确复制到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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