|
67 | 67 | import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureElement; |
68 | 68 | import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureTreeRoot; |
69 | 69 | import org.apache.pdfbox.pdmodel.graphics.color.PDOutputIntent; |
| 70 | +import org.apache.pdfbox.pdmodel.interactive.action.PDAction; |
| 71 | +import org.apache.pdfbox.pdmodel.interactive.action.PDActionFactory; |
70 | 72 | import org.apache.pdfbox.pdmodel.interactive.action.PDActionGoTo; |
71 | 73 | import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation; |
72 | 74 | import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDDestination; |
@@ -538,49 +540,7 @@ public void appendDocument(PDDocument destination, PDDocument source) throws IOE |
538 | 540 | destination.setVersion(srcVersion); |
539 | 541 | } |
540 | 542 |
|
541 | | - int pageIndexOpenActionDest = -1; |
542 | 543 | PDDocumentCatalog destCatalog = destination.getDocumentCatalog(); |
543 | | - if (destCatalog.getOpenAction() == null) |
544 | | - { |
545 | | - // PDFBOX-3972: get local dest page index, it must be reassigned after the page cloning |
546 | | - PDDestinationOrAction openAction = null; |
547 | | - try |
548 | | - { |
549 | | - openAction = srcCatalog.getOpenAction(); |
550 | | - } |
551 | | - catch (IOException ex) |
552 | | - { |
553 | | - // PDFBOX-4223 |
554 | | - LOG.error("Invalid OpenAction ignored", ex); |
555 | | - } |
556 | | - PDDestination openActionDestination = null; |
557 | | - if (openAction instanceof PDActionGoTo) |
558 | | - { |
559 | | - openActionDestination = ((PDActionGoTo) openAction).getDestination(); |
560 | | - } |
561 | | - else if (openAction instanceof PDDestination) |
562 | | - { |
563 | | - openActionDestination = (PDDestination) openAction; |
564 | | - } |
565 | | - // note that it can also be something else, e.g. PDActionJavaScript, then do nothing |
566 | | - |
567 | | - if (openActionDestination instanceof PDPageDestination) |
568 | | - { |
569 | | - PDPage page = ((PDPageDestination) openActionDestination).getPage(); |
570 | | - if (page != null) |
571 | | - { |
572 | | - pageIndexOpenActionDest = srcCatalog.getPages().indexOf(page); |
573 | | - if (pageIndexOpenActionDest == -1) |
574 | | - { |
575 | | - LOG.warn("OpenAction entry ignored because destination page doesn't exist"); |
576 | | - openAction = null; |
577 | | - } |
578 | | - } |
579 | | - } |
580 | | - |
581 | | - destCatalog.setOpenAction(openAction); |
582 | | - } |
583 | | - |
584 | 544 | PDFCloneUtility cloner = new PDFCloneUtility(destination); |
585 | 545 |
|
586 | 546 | mergeAcroForm(cloner, destCatalog, srcCatalog); |
@@ -853,25 +813,9 @@ else if (destOCP != null && srcOCP != null) |
853 | 813 | // TODO update mapping for XObjects |
854 | 814 | } |
855 | 815 | destinationPageTree.add(newPage); |
856 | | - |
857 | | - if (pageIndex == pageIndexOpenActionDest) |
858 | | - { |
859 | | - // PDFBOX-3972: reassign the page. |
860 | | - // The openAction is either a PDActionGoTo or a PDPageDestination |
861 | | - PDDestinationOrAction openAction = destCatalog.getOpenAction(); |
862 | | - PDPageDestination pageDestination; |
863 | | - if (openAction instanceof PDActionGoTo) |
864 | | - { |
865 | | - pageDestination = (PDPageDestination) ((PDActionGoTo) openAction).getDestination(); |
866 | | - } |
867 | | - else |
868 | | - { |
869 | | - pageDestination = (PDPageDestination) openAction; |
870 | | - } |
871 | | - pageDestination.setPage(newPage); |
872 | | - } |
873 | 816 | ++pageIndex; |
874 | 817 | } |
| 818 | + mergeOpenAction(srcCatalog, destCatalog, cloner); |
875 | 819 | if (mergeStructTree) |
876 | 820 | { |
877 | 821 | updatePageReferences(cloner, srcNumberTreeAsMap, objMapping); |
@@ -904,6 +848,51 @@ else if (destOCP != null && srcOCP != null) |
904 | 848 | } |
905 | 849 | } |
906 | 850 |
|
| 851 | + private void mergeOpenAction(PDDocumentCatalog srcCatalog, PDDocumentCatalog dstCatalog, |
| 852 | + PDFCloneUtility cloner) throws IOException |
| 853 | + { |
| 854 | + PDDestinationOrAction srcOpenAction = null; |
| 855 | + PDDestinationOrAction dstOpenAction = null; |
| 856 | + try |
| 857 | + { |
| 858 | + dstOpenAction = dstCatalog.getOpenAction(); |
| 859 | + srcOpenAction = srcCatalog.getOpenAction(); |
| 860 | + } |
| 861 | + catch (IOException ex) |
| 862 | + { |
| 863 | + // PDFBOX-4223 |
| 864 | + LOG.error("Invalid OpenAction ignored", ex); |
| 865 | + } |
| 866 | + if (dstOpenAction == null && srcOpenAction != null) |
| 867 | + { |
| 868 | + COSBase clonedOpenActionBase = cloner.cloneForNewDocument(srcOpenAction.getCOSObject()); |
| 869 | + PDDestination openActionDestination = null; |
| 870 | + if (clonedOpenActionBase instanceof COSDictionary) |
| 871 | + { |
| 872 | + PDAction action = PDActionFactory.createAction((COSDictionary) clonedOpenActionBase); |
| 873 | + if (action instanceof PDActionGoTo) |
| 874 | + { |
| 875 | + openActionDestination = ((PDActionGoTo) action).getDestination(); |
| 876 | + } |
| 877 | + dstCatalog.setOpenAction(action); |
| 878 | + } |
| 879 | + else if (clonedOpenActionBase instanceof COSArray) |
| 880 | + { |
| 881 | + openActionDestination = PDDestination.create(clonedOpenActionBase); |
| 882 | + dstCatalog.setOpenAction(openActionDestination); |
| 883 | + } |
| 884 | + if (openActionDestination instanceof PDPageDestination) |
| 885 | + { |
| 886 | + PDPage page = ((PDPageDestination) openActionDestination).getPage(); |
| 887 | + if (page != null && dstCatalog.getPages().indexOf(page) == -1) |
| 888 | + { |
| 889 | + LOG.warn("OpenAction entry ignored because destination page doesn't exist"); |
| 890 | + dstCatalog.setOpenAction(null); |
| 891 | + } |
| 892 | + } |
| 893 | + } |
| 894 | + } |
| 895 | + |
907 | 896 | private void mergeViewerPreferences(PDDocumentCatalog destCatalog, PDDocumentCatalog srcCatalog) |
908 | 897 | { |
909 | 898 | PDViewerPreferences srcViewerPreferences = srcCatalog.getViewerPreferences(); |
|
0 commit comments