iText duplicates /Popup annotations when copying pages

坚强是说给别人听的谎言 提交于 2019-12-06 13:44:52

问题


When using the following code to copy annotated pages, iText 7.1.0 duplicates the /Popup annotation, the only difference between the "original" and the duplicate being that one has a /Parent entry and the other doesn't.

Unfortunately, the one without the missing /Parent is registered in the /Annots list of the /Page dictionary. How can I make iText not duplicate the annotations or at least register the one with the /Parent entry? Any explanation for the odd behavior?

import com.itextpdf.kernel.pdf.*;
import java.io.IOException;

public class CopyPdfTest {
    public static void main(String[] args) throws IOException {
        PdfDocument inputDoc = new PdfDocument(new PdfReader("input.pdf"));
        PdfWriter writer = new PdfWriter("output.pdf");
        writer.setCompressionLevel(0);
        PdfDocument outputDoc = new PdfDocument(writer);

        for (int i = 1; i <= inputDoc.getNumberOfPages(); i++) {
            inputDoc.copyPagesTo(i, i, outputDoc);
        }

        inputDoc.close();
        outputDoc.close();
    }
}

input.pdf

%PDF-1.4
%âãÏÓ
5 0 obj 
<<
/M (D:20180107100338+01'00')
/NM (68bd5c7e-3071-4b10-83ad-6bb2e75a8f3d)
/Subtype /Popup
/Type /Annot
/Parent 4 0 R
/Open false
/F 28
/Rect [352.966 707.883 532.966 827.883]
/P 3 0 R
>>
endobj 
6 0 obj 
<<
/FormType 1
/Subtype /Form
/Type /XObject
/BBox [115.975 693.768 179.508 827.883]
/Length 69
/Matrix [1 0 0 1 -115.975 -693.768]
>>
stream
1.000 0.000 0.000 RG
2 w
0 J
0 j
116.975 694.768 61.534 132.115 re
S

endstream 
endobj 
4 0 obj 
<<
/Subtype /Square
/RD [0 0 0 0]
/RC (<?xml version="1.0"?><body xmlns="http://www.w3.org/1999/xhtml" xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:APIVersion="Acrobat:11.0.0" xfa:spec="2.0.2"><p dir="ltr"><span style="text-align:left;font-size:13pt;font-style:normal;font-weight:normal;color:#000000;font-family:Arial">test</span></p></body>)
/T (thw)
/Contents (test)
/Rect [115.975 693.768 179.508 827.883]
/CA 1
/P 3 0 R
/M (D:20180107100342+01'00')
/Type /Annot
/NM (fd33d765-e844-4226-aff8-3ef81361e787)
/F 4
/BS 
<<
/W 2
/S /S
>>
/AP 
<<
/N 6 0 R
>>
/C [1 0 0]
/Popup 5 0 R
/Subj (Rectangle)
/CreationDate (D:20180107100338+01'00')
>>
endobj 
8 0 obj 
<<
/OPM 1
/Type /ExtGState
>>
endobj 
7 0 obj 
<<
/R7 8 0 R
>>
endobj 
9 0 obj 
<<
/Length 30
>>
stream
q 0.1 0 0 0.1 0 0 cm
/R7 gs
Q

endstream 
endobj 
3 0 obj 
<<
/pdftk_PageNum 1
/Annots [4 0 R 5 0 R]
/Resources 
<<
/ProcSet [/PDF]
/ExtGState 7 0 R
>>
/Type /Page
/Parent 1 0 R
/Contents 9 0 R
/MediaBox [0 0 595 842]
>>
endobj 
1 0 obj 
<<
/Kids [3 0 R]
/Type /Pages
/Count 1
>>
endobj 
11 0 obj 
<<
/Type /Catalog
/Pages 1 0 R
>>
endobj 
12 0 obj 
<<
/ModDate (D:20180108114520+01'00')
/CreationDate (D:20180108114520+01'00')
/Creator (pdftk 2.02 - www.pdftk.com)
/Producer (itext-paulo-155 \(itextpdf.sf.net-lowagie.com\))
>>
endobj xref
0 13
0000000000 65535 f 
0000001472 00000 n 
0000000000 65535 f 
0000001293 00000 n 
0000000460 00000 n 
0000000015 00000 n 
0000000220 00000 n 
0000001177 00000 n 
0000001130 00000 n 
0000001210 00000 n 
0000000000 65535 f 
0000001531 00000 n 
0000001583 00000 n 
trailer

<<
/Info 12 0 R
/ID [<10172d770e5713ca1f4b5185f0508476><d91bf66464b334d552196d601c1d2f94>]
/Root 11 0 R
/Size 13
>>
startxref
1779
%%EOF

output.pdf

10 0 obj and 11 0 obj are the duplicated Popups.

%PDF-1.7
%âãÏÓ
5 0 obj
<</Length 30>>stream
q 0.1 0 0 0.1 0 0 cm
/R7 gs
Q

endstream
endobj
4 0 obj
<</Annots[8 0 R 11 0 R]/Contents 5 0 R/MediaBox[0 0 595 842]/Parent 2 0 R/Resources<</ExtGState 6 0 R/ProcSet[/PDF]>>/Type/Page/pdftk_PageNum 1>>
endobj
1 0 obj
<</Pages 2 0 R/Type/Catalog>>
endobj
3 0 obj
<</CreationDate(D:20180108123558+01'00')/ModDate(D:20180108123558+01'00')/Producer(iText® 7.1.0 ©2000-2017 iText Group NV \(AGPL-version\))>>
endobj
2 0 obj
<</Count 1/Kids[4 0 R]/Type/Pages>>
endobj
6 0 obj
<</R7 7 0 R>>
endobj
7 0 obj
<</OPM 1/Type/ExtGState>>
endobj
8 0 obj
<</AP<</N 9 0 R>>/BS<</S/S/W 2>>/C[1 0 0]/CA 1/Contents(test)/CreationDate(D:20180107100338+01'00')/F 4/M(D:20180107100342+01'00')/NM(fd33d765-e844-4226-aff8-3ef81361e787)/P 4 0 R/Popup 10 0 R/RC(<?xml version="1.0"?><body xmlns="http://www.w3.org/1999/xhtml" xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/" xfa:APIVersion="Acrobat:11.0.0" xfa:spec="2.0.2"><p dir="ltr"><span style="text-align:left;font-size:13pt;font-style:normal;font-weight:normal;color:#000000;font-family:Arial">test</span></p></body>)/RD[0 0 0 0]/Rect[115.975 693.768 179.508 827.883]/Subj(Rectangle)/Subtype/Square/T(thw)/Type/Annot>>
endobj
9 0 obj
<</BBox[115.975 693.768 179.508 827.883]/FormType 1/Length 69/Matrix[1 0 0 1 -115.975 -693.768]/Subtype/Form/Type/XObject>>stream
1.000 0.000 0.000 RG
2 w
0 J
0 j
116.975 694.768 61.534 132.115 re
S

endstream
endobj
10 0 obj
<</F 28/M(D:20180107100338+01'00')/NM(68bd5c7e-3071-4b10-83ad-6bb2e75a8f3d)/Open false/P 4 0 R/Parent 8 0 R/Rect[352.966 707.883 532.966 827.883]/Subtype/Popup/Type/Annot>>
endobj
11 0 obj
<</F 28/M(D:20180107100338+01'00')/NM(68bd5c7e-3071-4b10-83ad-6bb2e75a8f3d)/Open false/P 4 0 R/Rect[352.966 707.883 532.966 827.883]/Subtype/Popup/Type/Annot>>
endobj
xref
0 12
0000000000 65535 f 
0000000253 00000 n 
0000000455 00000 n 
0000000298 00000 n 
0000000092 00000 n 
0000000015 00000 n 
0000000506 00000 n 
0000000535 00000 n 
0000000576 00000 n 
0000001204 00000 n 
0000001429 00000 n 
0000001618 00000 n 
trailer
<</ID [<cdbc838ce680aa596f6ff6e5a2a101e7><cdbc838ce680aa596f6ff6e5a2a101e7>]/Info 3 0 R/Root 1 0 R/Size 12>>
%iText-7.1.0
startxref
1794
%%EOF

来源:https://stackoverflow.com/questions/48150167/itext-duplicates-popup-annotations-when-copying-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!