首页 > 解决方案 > PDFBox: how to properly copy annotation from one pdf to another

问题描述

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

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.

标签: javapdfbox

解决方案


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

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


推荐阅读