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 2024/04/28 04:34:28 UTC

svn commit: r1917395 - /pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java

Author: tilman
Date: Sun Apr 28 04:34:28 2024
New Revision: 1917395

URL: http://svn.apache.org/viewvc?rev=1917395&view=rev
Log:
PDFBOX-5809: avoid orphan pages through beads and annotations

Modified:
    pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java

Modified: pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java?rev=1917395&r1=1917394&r2=1917395&view=diff
==============================================================================
--- pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java (original)
+++ pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java Sun Apr 28 04:34:28 2024
@@ -62,6 +62,8 @@ import org.apache.pdfbox.pdmodel.encrypt
 import org.apache.pdfbox.pdmodel.font.PDFont;
 import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationMarkup;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationPopup;
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary;
 import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream;
@@ -691,6 +693,15 @@ public class PDDocument implements Close
     {
         PDPage importedPage = new PDPage(new COSDictionary(page.getCOSObject()), resourceCache);
         importedPage.getCOSObject().removeItem(COSName.PARENT);
+
+        // PDFBOX-5809: avoid orphan pages through beads and annotations
+        if (importedPage.getCOSObject().containsKey(COSName.B))
+        {
+            importedPage.getCOSObject().removeItem(COSName.B);
+            LOG.warn("/B entry (beads) of source page is not imported to destination page");
+        }
+        importedPage.getAnnotations().stream().forEach(ann -> fixAnnotation(ann, importedPage));
+
         PDStream dest = new PDStream(this, page.getContents(), COSName.FLATE_DECODE);
         importedPage.setContents(dest);
         addPage(importedPage);
@@ -706,6 +717,30 @@ public class PDDocument implements Close
     }
 
     /**
+     * Avoid orphan page references in annotations.
+     *
+     * @param ann
+     * @param importedPage 
+     */
+    private void fixAnnotation(PDAnnotation ann, PDPage importedPage)
+    {
+        if (ann.getCOSObject().containsKey(COSName.P))
+        {
+            ann.setPage(importedPage);
+        }
+        // getPopup() result of markup annotations needs not to be fixed, one can assume this one
+        // is in the page annotation list
+        if (ann instanceof PDAnnotationPopup)
+        {
+            PDAnnotationMarkup parentAnnotation = ((PDAnnotationPopup) ann).getParent();
+            if (parentAnnotation != null)
+            {
+                fixAnnotation(parentAnnotation, importedPage);
+            }
+        }
+    }
+
+    /**
      * Determine the highest object number from the imported page to avoid mixed up numbers when saving the new pdf.
      * 
      * @param importedPage the imported page.