Author: tilman Date: Fri Jun 14 13:10:25 2024 New Revision: 1918327 URL: http://svn.apache.org/viewvc?rev=1918327&view=rev Log: PDFBOX-5840: keep and transform named page destinations that are part of target document
Added: pdfbox/branches/2.0/pdfbox/src/test/resources/input/merge/PDFBOX-5840-410609.pdf (with props) Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/Splitter.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/Splitter.java URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/Splitter.java?rev=1918327&r1=1918326&r2=1918327&view=diff ============================================================================== --- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/Splitter.java (original) +++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/Splitter.java Fri Jun 14 13:10:25 2024 @@ -57,6 +57,7 @@ import org.apache.pdfbox.pdmodel.interac import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationPopup; import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream; import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDDestination; +import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDNamedDestination; import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageDestination; /** @@ -775,6 +776,14 @@ public class Splitter } } } + if (srcDestination instanceof PDNamedDestination) + { + srcDestination = sourceDocument.getDocumentCatalog(). + findNamedDestinationPage((PDNamedDestination) srcDestination); + // we do not use the named destination anymore because names get modified, e.g. + // 0xAD becomes 0, see file 410609.pdf where the name no longer matches with the + // entry in the new name tree; plus the original solution was 40 additional loc + } if (srcDestination instanceof PDPageDestination) { // preserve links to pages within the split result: 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=1918327&r1=1918326&r2=1918327&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 Fri Jun 14 13:10:25 2024 @@ -54,6 +54,7 @@ import org.apache.pdfbox.pdmodel.interac import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationPopup; import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationText; import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget; +import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDNamedDestination; import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageDestination; import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageFitDestination; import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm; @@ -1160,4 +1161,47 @@ public class PDFMergerUtilityTest extend }); doc.close(); } + + public void testSplitWithNamedDestinations() throws IOException + { + PDDocument doc = PDDocument.load(new File(SRCDIR, "PDFBOX-5840-410609.pdf")); + Splitter splitter = new Splitter(); + splitter.setSplitAtPage(6); + List<PDDocument> splitResult = splitter.split(doc); + assertEquals(1, splitResult.size()); + List<PDAnnotation> annotations; + PDDocument dstDoc = splitResult.get(0); + checkForPageOrphans(dstDoc); + assertEquals(6, dstDoc.getNumberOfPages()); + annotations = dstDoc.getPage(0).getAnnotations(); + assertEquals(5, annotations.size()); + PDAnnotationLink link1 = (PDAnnotationLink) annotations.get(0); + PDAnnotationLink link2 = (PDAnnotationLink) annotations.get(1); + PDAnnotationLink link3 = (PDAnnotationLink) annotations.get(2); + PDAnnotationLink link4 = (PDAnnotationLink) annotations.get(3); + PDAnnotationLink link5 = (PDAnnotationLink) annotations.get(4); + PDPageDestination pd1 = + (PDPageDestination) ((PDActionGoTo) link1.getAction()).getDestination(); + PDPageDestination pd2 = + (PDPageDestination) ((PDActionGoTo) link2.getAction()).getDestination(); + PDPageDestination pd3 = + (PDPageDestination) ((PDActionGoTo) link3.getAction()).getDestination(); + PDPageDestination pd4 = + (PDPageDestination) ((PDActionGoTo) link4.getAction()).getDestination(); + PDPageDestination pd5 = + (PDPageDestination) ((PDActionGoTo) link5.getAction()).getDestination(); + PDPageTree pageTree = dstDoc.getPages(); + assertEquals(0, pageTree.indexOf(pd1.getPage())); + assertEquals(1, pageTree.indexOf(pd2.getPage())); + assertEquals(3, pageTree.indexOf(pd3.getPage())); + assertEquals(3, pageTree.indexOf(pd4.getPage())); + assertEquals(5, pageTree.indexOf(pd5.getPage())); + dstDoc.close(); + // Check that source document is unchanged + annotations = doc.getPage(0).getAnnotations(); + assertEquals(5, annotations.size()); + PDAnnotationLink link = (PDAnnotationLink) annotations.get(0); + assertTrue(((PDActionGoTo) link.getAction()).getDestination() instanceof PDNamedDestination); + doc.close(); + } } Added: pdfbox/branches/2.0/pdfbox/src/test/resources/input/merge/PDFBOX-5840-410609.pdf URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/test/resources/input/merge/PDFBOX-5840-410609.pdf?rev=1918327&view=auto ============================================================================== Binary file - no diff available. Propchange: pdfbox/branches/2.0/pdfbox/src/test/resources/input/merge/PDFBOX-5840-410609.pdf ------------------------------------------------------------------------------ svn:mime-type = application/pdf