|
| 1 | +package com.itextpdf.kernel.utils; |
| 2 | + |
| 3 | +import com.itextpdf.commons.utils.MessageFormatUtil; |
| 4 | +import com.itextpdf.kernel.logs.KernelLogMessageConstant; |
| 5 | +import com.itextpdf.kernel.pdf.PdfArray; |
| 6 | +import com.itextpdf.kernel.pdf.PdfDictionary; |
| 7 | +import com.itextpdf.kernel.pdf.PdfDocument; |
| 8 | +import com.itextpdf.kernel.pdf.PdfName; |
| 9 | +import com.itextpdf.kernel.pdf.PdfNameTree; |
| 10 | +import com.itextpdf.kernel.pdf.PdfObject; |
| 11 | +import com.itextpdf.kernel.pdf.PdfString; |
| 12 | +import org.slf4j.Logger; |
| 13 | +import org.slf4j.LoggerFactory; |
| 14 | + |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.Collections; |
| 17 | +import java.util.HashSet; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.Set; |
| 20 | + |
| 21 | +/** |
| 22 | + * Utility class which provides functionality to merge ECMA scripts from pdf documents |
| 23 | + */ |
| 24 | +public class PdfScriptMerger { |
| 25 | + private static final Logger LOGGER = LoggerFactory.getLogger(PdfScriptMerger.class); |
| 26 | + private static final Set<PdfName> allowedAAEntries = Collections |
| 27 | + .unmodifiableSet(new HashSet<>(Arrays.asList( |
| 28 | + PdfName.WC, |
| 29 | + PdfName.WS, |
| 30 | + PdfName.DS, |
| 31 | + PdfName.WP))); |
| 32 | + |
| 33 | + /** |
| 34 | + * Merges ECMA scripts from source to destinations from all possible places for them, |
| 35 | + * it only copies first action in chain for AA and OpenAction entries |
| 36 | + * |
| 37 | + * @param source source document from which script will be copied |
| 38 | + * @param destination destination document to which script will be copied |
| 39 | + */ |
| 40 | + public static void mergeScripts(PdfDocument source, PdfDocument destination) { |
| 41 | + mergeOpenActionsScripts(source, destination); |
| 42 | + mergeAdditionalActionsScripts(source, destination); |
| 43 | + mergeNamesScripts(source, destination); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Copies AA catalog entry ECMA scripts, it only copies first action in chain |
| 48 | + * |
| 49 | + * @param source source document from which script will be copied |
| 50 | + * @param destination destination document to which script will be copied |
| 51 | + */ |
| 52 | + public static void mergeAdditionalActionsScripts(PdfDocument source, PdfDocument destination) { |
| 53 | + PdfDictionary sourceAA = source.getCatalog().getPdfObject().getAsDictionary(PdfName.AA); |
| 54 | + PdfDictionary destinationAA = destination.getCatalog().getPdfObject().getAsDictionary(PdfName.AA); |
| 55 | + if (sourceAA == null || sourceAA.isEmpty()) { |
| 56 | + return; |
| 57 | + } |
| 58 | + if (destinationAA == null) { |
| 59 | + destinationAA = new PdfDictionary(); |
| 60 | + destination.getCatalog().getPdfObject().put(PdfName.AA, destinationAA); |
| 61 | + } |
| 62 | + for (Map.Entry<PdfName, PdfObject> entry : sourceAA.entrySet()) { |
| 63 | + if (destinationAA.containsKey(entry.getKey())) { |
| 64 | + LOGGER.error(MessageFormatUtil.format(KernelLogMessageConstant.CANNOT_MERGE_ENTRY, entry.getKey())); |
| 65 | + return; |
| 66 | + } |
| 67 | + if (!allowedAAEntries.contains(entry.getKey())) { |
| 68 | + continue; |
| 69 | + } |
| 70 | + destinationAA.put(entry.getKey(), copyECMAScriptActionsDictionary(destination, (PdfDictionary) entry.getValue())); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Copies open actions catalog entry ECMA scripts, it only copies first action in chain |
| 76 | + * |
| 77 | + * @param source source document from which script will be copied |
| 78 | + * @param destination destination document to which script will be copied |
| 79 | + */ |
| 80 | + public static void mergeOpenActionsScripts(PdfDocument source, PdfDocument destination) { |
| 81 | + PdfObject sourceOpenAction = source.getCatalog().getPdfObject().get(PdfName.OpenAction); |
| 82 | + if (sourceOpenAction instanceof PdfArray) { |
| 83 | + return; |
| 84 | + } |
| 85 | + PdfDictionary sourceOpenActionDict = source.getCatalog().getPdfObject().getAsDictionary(PdfName.OpenAction); |
| 86 | + if (sourceOpenActionDict == null || sourceOpenActionDict.isEmpty() || !PdfName.JavaScript.equals(sourceOpenActionDict.get(PdfName.S))) { |
| 87 | + return; |
| 88 | + } |
| 89 | + PdfObject destinationOpenAction = destination.getCatalog().getPdfObject().get(PdfName.OpenAction); |
| 90 | + if (destinationOpenAction != null) { |
| 91 | + LOGGER.error(MessageFormatUtil.format(KernelLogMessageConstant.CANNOT_MERGE_ENTRY, PdfName.OpenAction)); |
| 92 | + return; |
| 93 | + } |
| 94 | + destination.getCatalog().getPdfObject().put(PdfName.OpenAction, copyECMAScriptActionsDictionary(destination, sourceOpenActionDict)); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Copies ECMA scripts from Names catalog entry |
| 99 | + * |
| 100 | + * @param source source document from which script will be copied |
| 101 | + * @param destination destination document to which script will be copied |
| 102 | + */ |
| 103 | + public static void mergeNamesScripts(PdfDocument source, PdfDocument destination) { |
| 104 | + PdfDictionary namesDict = source.getCatalog().getPdfObject().getAsDictionary(PdfName.Names); |
| 105 | + if (namesDict == null || !namesDict.containsKey(PdfName.JavaScript)) { |
| 106 | + return; |
| 107 | + } |
| 108 | + PdfDictionary destinationNamesDict = destination.getCatalog().getPdfObject().getAsDictionary(PdfName.Names); |
| 109 | + if ((destinationNamesDict != null && destinationNamesDict.get(PdfName.JavaScript) != null) |
| 110 | + || destination.getCatalog().nameTreeContainsKey(PdfName.JavaScript)) { |
| 111 | + LOGGER.error(MessageFormatUtil.format(KernelLogMessageConstant.CANNOT_MERGE_ENTRY, PdfName.JavaScript)); |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + PdfNameTree sourceTree = new PdfNameTree(source.getCatalog(), PdfName.JavaScript); |
| 117 | + PdfNameTree destinationTree = destination.getCatalog().getNameTree(PdfName.JavaScript); |
| 118 | + for (Map.Entry<PdfString, PdfObject> entry : sourceTree.getNames().entrySet()) { |
| 119 | + PdfDictionary ECMAScriptActionsDirectCopy = copyECMAScriptActionsDictionary(destination, |
| 120 | + entry.getValue().isIndirect() ? (PdfDictionary) entry.getValue().getIndirectReference().getRefersTo() |
| 121 | + : (PdfDictionary) entry.getValue()); |
| 122 | + destinationTree.addEntry(entry.getKey(), ECMAScriptActionsDirectCopy); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private static PdfDictionary copyECMAScriptActionsDictionary(PdfDocument destination, PdfDictionary actions) { |
| 127 | + PdfObject originalScriptSource = actions.get(PdfName.JS); |
| 128 | + PdfObject scriptType = actions.get(PdfName.S); |
| 129 | + PdfDictionary actionsCopy = new PdfDictionary(); |
| 130 | + if (originalScriptSource != null) { |
| 131 | + actionsCopy.put(PdfName.JS, originalScriptSource.copyTo(destination)); |
| 132 | + } |
| 133 | + if (scriptType != null) { |
| 134 | + actionsCopy.put(PdfName.S, scriptType.copyTo(destination)); |
| 135 | + } |
| 136 | + return actionsCopy; |
| 137 | + } |
| 138 | +} |
0 commit comments