[ 
https://issues.apache.org/jira/browse/PDFBOX-5714?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17799041#comment-17799041
 ] 

Tilman Hausherr edited comment on PDFBOX-5714 at 12/20/23 8:10 PM:
-------------------------------------------------------------------

I added the two prints before the first save and they are both "true", so 
whatever bad things happen, happen when saving.

writeReference() is when the (wrong) "2 0 R" and "3 0 R" are written. It calls 
{{getObjectKey(obj)}} to get the key. For some reason, that one returns "2 0 R" 
and "3 0 R" for the problematic objects instead of returning new keys.

Here's some modified code that aborts when the problem happens
{code:java}
private COSObjectKey getObjectKey( COSBase obj )
{
        COSBase actual = obj;
        if( actual instanceof COSObject )
        {
                if (reuseObjectNumbers)
                {
                        COSObjectKey key = obj.getKey();
                        if (key != null)
                        {
                                for (Entry<COSBase, COSObjectKey> entry : 
objectKeys.entrySet())
                                {
                                        if (key.equals(entry.getValue()) && 
entry.getKey() != obj && entry.getKey() != ((COSObject) obj).getObject())
                                        {
                                                throw new 
RuntimeException("Duplicate object key " + entry.getKey() + " " + 
entry.getValue());
                                        }
                                }
                                objectKeys.put(obj, key);
                                return key;
                        }
                }
                actual = ((COSObject) obj).getObject();
        }
        // PDFBOX-4540: because objectKeys is accessible from outside, it is 
possible
        // that a COSObject obj is already in the objectKeys map.
        return objectKeys.computeIfAbsent(actual, k -> new 
COSObjectKey(++number, 0));
}
{code}



was (Author: tilman):
I added the two prints before the first save and they are both "true", so 
whatever bad things happen, happen when saving.

writeReference() is when the (wrong) "2 0 R" and "3 0 R" are written. It calls 
{{getObjectKey(obj)}} to get the key. For some reason, that one returns "2 0 R" 
and "3 0 R" for the problematic objects instead of returning new keys.

> PDFBox 3.0 regression: duplicate references in dictionary values
> ----------------------------------------------------------------
>
>                 Key: PDFBOX-5714
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-5714
>             Project: PDFBox
>          Issue Type: Bug
>          Components: Writing
>    Affects Versions: 3.0.0 PDFBox
>         Environment: macOS 14, Java 11
>            Reporter: Derek Wickern
>            Priority: Major
>              Labels: regression
>         Attachments: PdfboxAnnotationRegressionTest.java, 
> basic-sticky-note.fdf, out-pdfbox2.pdf, out-pdfbox3.pdf, w4.pdf
>
>
> {{basic-sticky-note.fdf}} contains an Acrobat "sticky note", which consists 
> of two annotations: text (2 0 obj) and popup (3 0 obj). The annotations 
> reference each other. The text annotation's "/Popup 3 0 R" references the 
> popup annotation. The popup's "/Parent 2 0 R" references the text annotation.
>  
> {code:java}
> 2 0 obj
> <<... /Popup 3 0 R ...>>
> endobj
> 3 0 obj
> <<... /Parent 2 0 R ...>>
> endobj
> {code}
>  
> The attached Java program copies those annotations onto {{{}w4.pdf{}}}, saves 
> the file, then reads it back to verify the result.
> h3. Expected Behavior
> {{out-pdfbox2.pdf}} is the output using pdfbox 2.0.30. The annotation objects 
> are reassigned to some available ID:
>  
> {code:java}
> 38 0 obj
> << ... /Popup 39 0 R ... >>
> endobj
> 39 0 obj
> << ... /Parent 38 0 R ...>>
> endobj
> {code}
>  
> The program outputs:
> {code:java}
> correct parent? true
> correct popup? true{code}
> h3. Actual Behavior
> {{out-pdfbox3.pdf}} is the output using pdfbox 3.0.0. The objects are 
> assigned a new ID but their references are unchanged, so the /Popup and 
> /Parent reference some other arbitrary object:
>  
> {code:java}
> 3473 0 obj
> << ... /Popup 3 0 R ... >>
> endobj
> 3474 0 obj
> << ... /Parent 2 0 R ...>>
> endobj
> {code}
>  
> The program crashes since the dereferenced /Parent is not an annotation:
> {code:java}
> Exception in thread "main" java.lang.ClassCastException: class 
> org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationUnknown cannot 
> be cast to class 
> org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup 
> (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationUnknown and 
> org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup are in 
> unnamed module of loader 'app')
>     at 
> org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationPopup.getParent(PDAnnotationPopup.java:99)
>     at 
> PdfboxAnnotationRegressionTest.main(PdfboxAnnotationRegressionTest.java:45) 
> {code}
>  
>  



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to