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/05/04 08:34:23 UTC

svn commit: r1917492 - in /pdfbox/branches/3.0/pdfbox/src: main/java/org/apache/pdfbox/multipdf/Splitter.java test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java test/resources/input/merge/PDFBOX-5811-362972.pdf

Author: tilman
Date: Sat May  4 08:34:22 2024
New Revision: 1917492

URL: http://svn.apache.org/viewvc?rev=1917492&view=rev
Log:
PDFBOX-5811: catch exceptions from broken destinations + test

Added:
    pdfbox/branches/3.0/pdfbox/src/test/resources/input/merge/PDFBOX-5811-362972.pdf   (with props)
Modified:
    pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/Splitter.java
    pdfbox/branches/3.0/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java

Modified: pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/Splitter.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/Splitter.java?rev=1917492&r1=1917491&r2=1917492&view=diff
==============================================================================
--- pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/Splitter.java (original)
+++ pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/Splitter.java Sat May  4 08:34:22 2024
@@ -740,15 +740,35 @@ public class Splitter
             clonedAnnotations.add(annotationClone);
             if (annotationClone instanceof PDAnnotationLink)
             {
-                PDAnnotationLink link = (PDAnnotationLink) annotationClone;   
-                PDDestination srcDestination = link.getDestination();
+                PDAnnotationLink link = (PDAnnotationLink) annotationClone;
+                PDDestination srcDestination = null;
+                try
+                {
+                    srcDestination = link.getDestination();
+                }
+                catch (IOException ex)
+                {
+                    LOG.warn("Incorrect destination in link annotation on page " +
+                            (currentPageNumber + 1) + " is removed", ex);
+                    link.setDestination(null);
+                }
                 PDAction action = null;
                 if (srcDestination == null)
                 {
                     action = link.getAction();
                     if (action instanceof PDActionGoTo)
                     {
-                        srcDestination = ((PDActionGoTo) action).getDestination();
+                        PDActionGoTo goToAction = (PDActionGoTo) action;
+                        try
+                        {
+                            srcDestination = goToAction.getDestination();
+                        }
+                        catch (IOException ex)
+                        {
+                            LOG.warn("GoToAction with incorrect destination in link annotation on page " +
+                                    (currentPageNumber + 1) + " is removed", ex);
+                            link.setAction(null);
+                        }
                     }
                 }
                 if (srcDestination instanceof PDPageDestination)

Modified: pdfbox/branches/3.0/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/3.0/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java?rev=1917492&r1=1917491&r2=1917492&view=diff
==============================================================================
--- pdfbox/branches/3.0/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java (original)
+++ pdfbox/branches/3.0/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java Sat May  4 08:34:22 2024
@@ -62,6 +62,7 @@ import org.apache.pdfbox.rendering.PDFRe
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.parallel.Execution;
@@ -1175,4 +1176,32 @@ class PDFMergerUtilityTest
             assertEquals(annotationText3.getPage(), doc.getPage(2));
         }
     }
+
+    @Test
+    void testSplitWithBrokenDestination() throws IOException
+    {
+        try (PDDocument doc = Loader.loadPDF(new File(SRCDIR, "PDFBOX-5811-362972.pdf")))
+        {
+            Splitter splitter = new Splitter();
+            splitter.setStartPage(2);
+            splitter.setEndPage(2);
+            List<PDDocument> splitResult = splitter.split(doc);
+            assertEquals(1, splitResult.size());
+            List<PDAnnotation> annotations;
+            try (PDDocument dstDoc = splitResult.get(0))
+            {
+                checkForPageOrphans(dstDoc);
+                assertEquals(1, dstDoc.getNumberOfPages());
+                annotations = dstDoc.getPage(0).getAnnotations();
+                assertEquals(1, annotations.size());
+                PDAnnotationLink link = (PDAnnotationLink) annotations.get(0);
+                assertNull(link.getDestination());
+            }
+            // Check source document
+            annotations = doc.getPage(1).getAnnotations();
+            assertEquals(1, annotations.size());
+            PDAnnotationLink link = (PDAnnotationLink) annotations.get(0);
+            assertThrows(IOException.class, () -> link.getDestination());
+        }
+    }
 }

Added: pdfbox/branches/3.0/pdfbox/src/test/resources/input/merge/PDFBOX-5811-362972.pdf
URL: http://svn.apache.org/viewvc/pdfbox/branches/3.0/pdfbox/src/test/resources/input/merge/PDFBOX-5811-362972.pdf?rev=1917492&view=auto
==============================================================================
Binary file - no diff available.

Propchange: pdfbox/branches/3.0/pdfbox/src/test/resources/input/merge/PDFBOX-5811-362972.pdf
------------------------------------------------------------------------------
    svn:mime-type = application/pdf