You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2022/10/12 06:23:31 UTC

svn commit: r1904541 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos: COSArray.java COSDictionary.java

Author: lehmi
Date: Wed Oct 12 06:23:31 2022
New Revision: 1904541

URL: http://svn.apache.org/viewvc?rev=1904541&view=rev
Log:
PDFBOX-5489: include COSArrays when searching for indirect object numbers

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSArray.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDictionary.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSArray.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSArray.java?rev=1904541&r1=1904540&r2=1904541&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSArray.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSArray.java Wed Oct 12 06:23:31 2022
@@ -731,5 +731,69 @@ public class COSArray extends COSBase im
     {
         return updateState;
     }
-    
+
+    /**
+     * Collects all indirect objects numbers within this COSArray and all included dictionaries. It is used to avoid
+     * mixed up object numbers when importing an existing page to another pdf.
+     * 
+     * Expert use only. You might run into an endless recursion if choosing a wrong starting point.
+     * 
+     * @param indirectObjects a list of already found indirect objects.
+     * 
+     */
+    public void getIndirectObjectKeys(List<COSObjectKey> indirectObjects)
+    {
+        if (indirectObjects == null)
+        {
+            return;
+        }
+        COSObjectKey key = getKey();
+        if (key != null)
+        {
+            // avoid endless recursions
+            if (indirectObjects.contains(key))
+            {
+                return;
+            }
+            else
+            {
+                indirectObjects.add(key);
+            }
+        }
+
+        for (COSBase cosBase : objects)
+        {
+            COSObjectKey cosBaseKey = cosBase.getKey();
+            if (cosBaseKey != null && indirectObjects.contains(cosBaseKey))
+            {
+                continue;
+            }
+            if (cosBase instanceof COSObject)
+            {
+                // dereference object
+                COSBase referencedObject = ((COSObject) cosBase).getObject();
+                if (referencedObject instanceof COSDictionary)
+                {
+                    // descend to included dictionary to collect all included indirect objects
+                    ((COSDictionary) referencedObject).getIndirectObjectKeys(indirectObjects);
+                }
+                else if (referencedObject instanceof COSArray)
+                {
+                    // descend to included array to collect all included indirect objects
+                    ((COSArray) referencedObject).getIndirectObjectKeys(indirectObjects);
+                }
+            }
+            else if (cosBase instanceof COSDictionary)
+            {
+                // descend to included dictionary to collect all included indirect objects
+                ((COSDictionary) cosBase).getIndirectObjectKeys(indirectObjects);
+            }
+            else if (cosBase instanceof COSArray)
+            {
+                // descend to included array to collect all included indirect objects
+                ((COSArray) cosBase).getIndirectObjectKeys(indirectObjects);
+            }
+        }
+    }
+
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDictionary.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDictionary.java?rev=1904541&r1=1904540&r2=1904541&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDictionary.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDictionary.java Wed Oct 12 06:23:31 2022
@@ -25,6 +25,7 @@ import java.util.Collection;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Set;
 import java.util.function.BiConsumer;
 
@@ -1432,35 +1433,57 @@ public class COSDictionary extends COSBa
      */
     public void getIndirectObjectKeys(List<COSObjectKey> indirectObjects)
     {
-        // avoid endless recursions
-        if (indirectObjects == null || (getKey() != null && indirectObjects.contains(getKey())))
+        if (indirectObjects == null)
         {
             return;
         }
-        for (COSBase cosBase : items.values())
+        COSObjectKey key = getKey();
+        if (key != null)
         {
-            COSDictionary dictionary = null;
+            // avoid endless recursions
+            if (indirectObjects.contains(key))
+            {
+                return;
+            }
+            else
+            {
+                indirectObjects.add(key);
+            }
+        }
+        for (Entry<COSName, COSBase> entry : items.entrySet())
+        {
+            COSBase cosBase = entry.getValue();
+            COSObjectKey cosBaseKey = cosBase.getKey();
+            // avoid endless recursions
+            if (COSName.PARENT.equals(entry.getKey())
+                    || (cosBaseKey != null && indirectObjects.contains(cosBaseKey)))
+            {
+                continue;
+            }
             if (cosBase instanceof COSObject)
             {
-                // add indirect object key and dereference object
-                if (cosBase.getKey() != null && !indirectObjects.contains(cosBase.getKey()))
+                // dereference object
+                COSBase referencedObject = ((COSObject) cosBase).getObject();
+                if (referencedObject instanceof COSDictionary)
+                {
+                    // descend to included dictionary to collect all included indirect objects
+                    ((COSDictionary) referencedObject).getIndirectObjectKeys(indirectObjects);
+                }
+                else if (referencedObject instanceof COSArray)
                 {
-                    indirectObjects.add(cosBase.getKey());
-                    COSBase referencedObject = ((COSObject) cosBase).getObject();
-                    if (referencedObject instanceof COSDictionary)
-                    {
-                        dictionary = (COSDictionary) referencedObject;
-                    }
+                    // descend to included array to collect all included indirect objects
+                    ((COSArray) referencedObject).getIndirectObjectKeys(indirectObjects);
                 }
             }
             else if (cosBase instanceof COSDictionary)
             {
-                dictionary = (COSDictionary) cosBase;
+                // descend to included dictionary to collect all included indirect objects
+                ((COSDictionary) cosBase).getIndirectObjectKeys(indirectObjects);
             }
-            if (dictionary != null)
+            else if (cosBase instanceof COSArray)
             {
-                // descend to included dictionary to collect all included indirect objects
-                dictionary.getIndirectObjectKeys(indirectObjects);
+                // descend to included array to collect all included indirect objects
+                ((COSArray) cosBase).getIndirectObjectKeys(indirectObjects);
             }
         }
     }