You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2020/04/16 13:04:45 UTC

svn commit: r1876603 - in /pdfbox/branches/2.0/pdfbox/src: main/java/org/apache/pdfbox/multipdf/PDFCloneUtility.java test/java/org/apache/pdfbox/multipdf/PDFCloneUtilityTest.java

Author: tilman
Date: Thu Apr 16 13:04:45 2020
New Revision: 1876603

URL: http://svn.apache.org/viewvc?rev=1876603&view=rev
Log:
PDFBOX-4814: dereference COSArray and COSDictionary targets whe needed + test

Modified:
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFCloneUtility.java
    pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFCloneUtilityTest.java

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFCloneUtility.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFCloneUtility.java?rev=1876603&r1=1876602&r2=1876603&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFCloneUtility.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFCloneUtility.java Thu Apr 16 13:04:45 2020
@@ -193,10 +193,17 @@ public class PDFCloneUtility
           }
           else if( base instanceof COSArray )
           {
-              COSArray array = (COSArray)base;
-              for( int i=0; i<array.size(); i++ )
+              if (target instanceof COSObject)
               {
-                  ((COSArray)target).add( cloneForNewDocument( array.get( i ) ) );
+                  cloneMerge(base, ((COSObject) target).getObject());
+              }
+              else
+              {
+                  COSArray array = (COSArray) base;
+                  for (int i = 0; i < array.size(); i++)
+                  {
+                      ((COSArray) target).add(cloneForNewDocument(array.get(i)));
+                  }
               }
           }
           else if( base instanceof COSStream )
@@ -218,19 +225,26 @@ public class PDFCloneUtility
           }
           else if( base instanceof COSDictionary )
           {
-              COSDictionary dic = (COSDictionary)base;
-              clonedVersion.put( base, retval );
-              for( Map.Entry<COSName, COSBase> entry : dic.entrySet() )
+              if (target instanceof COSObject)
               {
-                  COSName key = entry.getKey();
-                  COSBase value = entry.getValue();
-                  if (((COSDictionary)target).getItem(key) != null)
-                  {
-                      cloneMerge(value, ((COSDictionary)target).getItem(key));
-                  }
-                  else
+                  cloneMerge(base, ((COSObject) target).getObject());
+              }
+              else
+              {
+                  COSDictionary dic = (COSDictionary) base;
+                  clonedVersion.put(base, retval);
+                  for (Map.Entry<COSName, COSBase> entry : dic.entrySet())
                   {
-                      ((COSDictionary)target).setItem( key, cloneForNewDocument(value));
+                      COSName key = entry.getKey();
+                      COSBase value = entry.getValue();
+                      if (((COSDictionary) target).getItem(key) != null)
+                      {
+                          cloneMerge(value, ((COSDictionary) target).getItem(key));
+                      }
+                      else
+                      {
+                          ((COSDictionary) target).setItem(key, cloneForNewDocument(value));
+                      }
                   }
               }
           }

Modified: pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFCloneUtilityTest.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFCloneUtilityTest.java?rev=1876603&r1=1876602&r2=1876603&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFCloneUtilityTest.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFCloneUtilityTest.java Thu Apr 16 13:04:45 2020
@@ -16,6 +16,7 @@
 package org.apache.pdfbox.multipdf;
 
 import java.awt.Color;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
 import junit.framework.TestCase;
@@ -23,6 +24,7 @@ import org.apache.pdfbox.pdmodel.PDDocum
 import org.apache.pdfbox.pdmodel.PDPage;
 import org.apache.pdfbox.pdmodel.PDPageContentStream;
 import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
+import org.apache.pdfbox.pdmodel.graphics.optionalcontent.PDOptionalContentProperties;
 
 /**
  * Test suite for PDFCloneUtility, see PDFBOX-2052.
@@ -97,4 +99,25 @@ public class PDFCloneUtilityTest extends
         PDDocument.load(new File(TESTDIR + CLONEDST)).close();
         PDDocument.load(new File(TESTDIR + CLONEDST), (String)null).close();
     }
-}
+
+    /**
+     * PDFBOX-4814: this tests merging a direct and an indirect COSDictionary, when "target" is
+     * indirect in cloneMerge().
+     *
+     * @throws IOException
+     */
+    public void testDirectIndirect() throws IOException
+    {
+        PDDocument doc1 = new PDDocument();
+
+        doc1.addPage(new PDPage());
+        doc1.getDocumentCatalog().setOCProperties(new PDOptionalContentProperties());
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        doc1.save(baos);
+        PDDocument doc2 = PDDocument.load(baos.toByteArray());
+        PDFMergerUtility merger = new PDFMergerUtility();
+        merger.appendDocument(doc2, doc1);
+        doc2.close();
+        doc1.close();
+    }
+}
\ No newline at end of file