Skip to content

Commit

Permalink
PDFBOX-5811: catch exceptions from broken destinations + test
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1917494 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
THausherr committed May 4, 2024
1 parent 636c4ec commit 11596fe
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
26 changes: 23 additions & 3 deletions pdfbox/src/main/java/org/apache/pdfbox/multipdf/Splitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -741,15 +741,35 @@ private void processAnnotations(PDPage imported) throws IOException
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -1174,4 +1175,32 @@ void testSplitWithPopupAnnotations() throws IOException
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());
}
}
}
Binary file not shown.

0 comments on commit 11596fe

Please sign in to comment.