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:35:03 UTC
svn commit: r1917396 - /pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
Author: tilman
Date: Sun Apr 28 04:35:03 2024
New Revision: 1917396
URL: http://svn.apache.org/viewvc?rev=1917396&view=rev
Log:
PDFBOX-5809: avoid orphan pages through beads and annotations; remove parent entry
Modified:
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java?rev=1917396&r1=1917395&r2=1917396&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java Sun Apr 28 04:35:03 2024
@@ -65,6 +65,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;
@@ -781,6 +783,19 @@ public class PDDocument implements Close
public PDPage importPage(PDPage page) throws IOException
{
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");
+ }
+ for (PDAnnotation ann : importedPage.getAnnotations())
+ {
+ fixAnnotation(ann, importedPage);
+ }
+
PDStream dest = new PDStream(this, page.getContents(), COSName.FLATE_DECODE);
importedPage.setContents(dest);
addPage(importedPage);
@@ -796,6 +811,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);
+ }
+ }
+ }
+
+ /**
* This will get the low level document.
*
* @return The document that this layer sits on top of.