pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1850486 - in /pdfbox/branches/2.0/pdfbox/src: main/java/org/apache/pdfbox/multipdf/ main/java/org/apache/pdfbox/pdmodel/ main/java/org/apache/pdfbox/pdmodel/graphics/form/ main/java/org/apache/pdfbox/pdmodel/graphics/image/ main/java/org/a...
Date Sat, 05 Jan 2019 13:16:31 GMT
Author: tilman
Date: Sat Jan  5 13:16:31 2019
New Revision: 1850486

URL: http://svn.apache.org/viewvc?rev=1850486&view=rev
Log:
PDFBOX-4423: getStructParent() and getStructParents() must return -1 when not available; consider
this when merging and when testing merge; add previously failing test with file that has annotation
but no /StructParent

Modified:
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPage.java
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/form/PDFormXObject.java
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotation.java
    pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java?rev=1850486&r1=1850485&r2=1850486&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java
(original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java
Sat Jan  5 13:16:31 2019
@@ -1231,12 +1231,18 @@ public class PDFMergerUtility
      */
     private void updateStructParentEntries(PDPage page, int structParentOffset) throws IOException
     {
-        page.setStructParents(page.getStructParents() + structParentOffset);
+        if (page.getStructParents() >= 0)
+        {
+            page.setStructParents(page.getStructParents() + structParentOffset);
+        }
         List<PDAnnotation> annots = page.getAnnotations();
         List<PDAnnotation> newannots = new ArrayList<PDAnnotation>();
         for (PDAnnotation annot : annots)
         {
-            annot.setStructParent(annot.getStructParent() + structParentOffset);
+            if (annot.getStructParent() >= 0)
+            {
+                annot.setStructParent(annot.getStructParent() + structParentOffset);
+            }
             newannots.add(annot);
         }
         page.setAnnotations(newannots);

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPage.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPage.java?rev=1850486&r1=1850485&r2=1850486&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPage.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPage.java Sat Jan
 5 13:16:31 2019
@@ -235,11 +235,12 @@ public class PDPage implements COSObject
     /**
      * This will get the key of this Page in the structural parent tree.
      * 
-     * @return the integer key of the page's entry in the structural parent tree
+     * @return the integer key of the page's entry in the structural parent tree or -1 if
+     * there isn't any.
      */
     public int getStructParents()
     {
-        return page.getInt(COSName.STRUCT_PARENTS, 0);
+        return page.getInt(COSName.STRUCT_PARENTS);
     }
 
     /**

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/form/PDFormXObject.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/form/PDFormXObject.java?rev=1850486&r1=1850485&r2=1850486&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/form/PDFormXObject.java
(original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/form/PDFormXObject.java
Sat Jan  5 13:16:31 2019
@@ -239,14 +239,15 @@ public class PDFormXObject extends PDXOb
     }
 
     /**
-     * This will get the key of this XObjectForm in the structural parent tree.
-     * Required if the form XObject contains marked-content sequences that are
-     * structural content items.
-     * @return the integer key of the XObjectForm's entry in the structural parent tree
+     * This will get the key of this XObjectForm in the structural parent tree. Required
if the form
+     * XObject contains marked-content sequences that are structural content items.
+     *
+     * @return the integer key of the XObjectForm's entry in the structural parent tree or
-1 if
+     * there isn't any.
      */
     public int getStructParents()
     {
-        return getCOSObject().getInt(COSName.STRUCT_PARENTS, 0);
+        return getCOSObject().getInt(COSName.STRUCT_PARENTS);
     }
 
     /**

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java?rev=1850486&r1=1850485&r2=1850486&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java
(original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java
Sat Jan  5 13:16:31 2019
@@ -398,11 +398,12 @@ public final class PDImageXObject extend
 
     /**
      * Returns the key of this XObject in the structural parent tree.
-     * @return this object's key the structural parent tree
+     *
+     * @return this object's key the structural parent tree or -1 if there isn't any.
      */
     public int getStructParent()
     {
-        return getCOSObject().getInt(COSName.STRUCT_PARENT, 0);
+        return getCOSObject().getInt(COSName.STRUCT_PARENT);
     }
 
     /**

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotation.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotation.java?rev=1850486&r1=1850485&r2=1850486&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotation.java
(original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/PDAnnotation.java
Sat Jan  5 13:16:31 2019
@@ -589,12 +589,13 @@ public abstract class PDAnnotation imple
 
     /**
      * This will get the key of this annotation in the structural parent tree.
-     * 
-     * @return the integer key of the annotation's entry in the structural parent tree
+     *
+     * @return the integer key of the annotation's entry in the structural parent tree or
-1 if
+     * there isn't any.
      */
     public int getStructParent()
     {
-        return getCOSObject().getInt(COSName.STRUCT_PARENT, 0);
+        return getCOSObject().getInt(COSName.STRUCT_PARENT);
     }
 
     /**

Modified: pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java?rev=1850486&r1=1850485&r2=1850486&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java
(original)
+++ pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java
Sat Jan  5 13:16:31 2019
@@ -374,6 +374,47 @@ public class PDFMergerUtilityTest extend
     }
 
     /**
+     * PDFBOX-4423: test merging a PDF where a widget has no StructParent.
+     * 
+     * @throws IOException 
+     */
+    public void testStructureTreeMerge7() throws IOException
+    {
+        PDFMergerUtility pdfMergerUtility = new PDFMergerUtility();
+        PDDocument src = PDDocument.load(new File(TARGETPDFDIR, "PDFBOX-4423-000746.pdf"));
+
+        PDStructureTreeRoot structureTreeRoot = src.getDocumentCatalog().getStructureTreeRoot();
+        PDNumberTreeNode parentTree = structureTreeRoot.getParentTree();
+        Map<Integer, COSObjectable> numberTreeAsMap = PDFMergerUtility.getNumberTreeAsMap(parentTree);
+        assertEquals(33, numberTreeAsMap.size());
+        assertEquals(64, Collections.max(numberTreeAsMap.keySet()) + 1);
+        assertEquals(31, (int) Collections.min(numberTreeAsMap.keySet()));
+        assertEquals(126, structureTreeRoot.getParentTreeNextKey());        
+
+        PDDocument dst = new PDDocument();
+
+        pdfMergerUtility.appendDocument(dst, src);
+        src.close();
+        dst.save(new File(TARGETTESTDIR, "PDFBOX-4423-merged.pdf"));
+        dst.close();
+
+        dst = PDDocument.load(new File(TARGETTESTDIR, "PDFBOX-4423-merged.pdf"));
+        checkWithNumberTree(dst);
+        checkForPageOrphans(dst);
+
+        structureTreeRoot = dst.getDocumentCatalog().getStructureTreeRoot();
+        parentTree = structureTreeRoot.getParentTree();
+        numberTreeAsMap = PDFMergerUtility.getNumberTreeAsMap(parentTree);
+        assertEquals(33, numberTreeAsMap.size());
+        assertEquals(64, Collections.max(numberTreeAsMap.keySet()) + 1);
+        assertEquals(31, (int) Collections.min(numberTreeAsMap.keySet()));
+        assertEquals(64, structureTreeRoot.getParentTreeNextKey());
+        dst.close();
+
+        checkStructTreeRootCount(new File(TARGETTESTDIR, "PDFBOX-4423-merged.pdf"));
+    }
+
+    /**
      * PDFBOX-4009: Test that ParentTreeNextKey is recalculated correctly.
      */
     public void testMissingParentTreeNextKey() throws IOException
@@ -504,13 +545,21 @@ public class PDFMergerUtilityTest extend
             {
                 for (PDAnnotationWidget widget : field.getWidgets())
                 {
-                    assertTrue(keySet.contains(widget.getStructParent()));
+                    if (widget.getStructParent() >= 0)
+                    {
+                        assertTrue("field '" + field.getFullyQualifiedName() + "' /StructParent
" +
+                                   widget.getStructParent() + " missing in /ParentTree",
+                                   keySet.contains(widget.getStructParent()));
+                    }
                 }
             }
         }
         for (PDPage page : document.getPages())
         {
-            assertTrue(keySet.contains(page.getStructParents()));
+            if (page.getStructParents() >= 0)
+            {
+                assertTrue(keySet.contains(page.getStructParents()));
+            }
         }
 
         // might also test image and form dictionaries...



Mime
View raw message