FlattenPDFFacade.java
7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.aemfd.docmanager.Document
* com.adobe.forms.option.LCFormsOptions
* com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException
* com.adobe.internal.pdftoolkit.core.exceptions.PDFUnableToCompleteOperationException
* com.adobe.internal.pdftoolkit.core.fontset.PDFFontSet
* com.adobe.internal.pdftoolkit.pdf.document.PDFCatalog
* com.adobe.internal.pdftoolkit.pdf.document.PDFDocument
* com.adobe.internal.pdftoolkit.pdf.interactive.forms.PDFInteractiveForm
*/
package com.adobe.fd.output.internal;
import com.adobe.aemfd.docmanager.Document;
import com.adobe.fd.forms.internal.utils.ServiceUtil;
import com.adobe.fd.forms.internal.utils.Utils;
import com.adobe.fd.output.internal.NonInteractivePDFRenderService;
import com.adobe.fd.output.internal.exception.OutputServiceException;
import com.adobe.forms.option.LCFormsOptions;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFUnableToCompleteOperationException;
import com.adobe.internal.pdftoolkit.core.fontset.PDFFontSet;
import com.adobe.internal.pdftoolkit.pdf.document.PDFCatalog;
import com.adobe.internal.pdftoolkit.pdf.document.PDFDocument;
import com.adobe.internal.pdftoolkit.pdf.interactive.forms.PDFInteractiveForm;
import java.util.Map;
public class FlattenPDFFacade {
NonInteractivePDFRenderService nonInteractivePDFRenderService;
boolean retainUnsignedSignatureField;
PDFDocument pdfDocument;
boolean hasSignedSignatureField;
boolean shellPDF;
boolean isPDFCloseRequired;
boolean retainFormState;
LCFormsOptions lcOptions;
Map xfaImagesMap;
Map signatureAppearances;
public FlattenPDFFacade(LCFormsOptions lcFormsOptions, NonInteractivePDFRenderService nonInteractivePDFRenderService, boolean retainUnsignedSignatureField, boolean retainFormState) {
this.retainUnsignedSignatureField = retainUnsignedSignatureField;
this.nonInteractivePDFRenderService = nonInteractivePDFRenderService;
this.retainFormState = retainFormState;
this.lcOptions = lcFormsOptions;
}
public void init(byte[] pdfDoc) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
this.pdfDocument = Utils.createPDFDocument(pdfDoc);
this.isPDFCloseRequired = true;
this.hasSignedSignatureField = Utils.hasRegularSig(this.pdfDocument);
this.shellPDF = this.pdfDocument.requireCatalog().getNeedsRendering();
}
public void cleanup() throws PDFUnableToCompleteOperationException, PDFInvalidDocumentException, PDFSecurityException, PDFIOException {
if (this.pdfDocument != null && this.isPDFCloseRequired) {
this.pdfDocument.close();
}
}
public Document flattenPDF(Document data) throws Exception {
if (this.pdfDocument == null) {
throw new OutputServiceException("AEM_OUT_001_002", new String[]{"pdfDocument"});
}
if (this.pdfDocument.getInteractiveForm() == null) {
throw new OutputServiceException("AEM_OUT_001_004");
}
if (Utils.isXFAForm(this.pdfDocument) && (!this.hasSignedSignatureField || this.shellPDF)) {
boolean xtgSignablePDFFlag = this.hasSignedSignatureField || this.retainUnsignedSignatureField;
Document doc = this.reRenderPDFForm(data, xtgSignablePDFFlag);
return doc;
}
this.flattenUsingGibson(data);
Document doc = Utils.toDocument(this.pdfDocument);
this.isPDFCloseRequired = false;
return doc;
}
private Document overlaySignatureAppearances(Document inDoc, Map signatureAppearances) throws Exception {
PDFDocument pdf = Utils.createPDFDocument(inDoc);
Utils.overlaySignatureAppearances(pdf, signatureAppearances, !this.retainUnsignedSignatureField);
return Utils.toDocument(pdf);
}
private void flattenUsingGibson(Document data) throws Exception {
byte[] xmlData = Utils.getBytes(data);
boolean forceAppearanceGen = true;
if (xmlData != null && xmlData.length > 0) {
if (Utils.isAcroForm(this.pdfDocument)) {
Utils.importDataToAcroform(this.pdfDocument, xmlData, true);
forceAppearanceGen = false;
} else {
Utils.importXFAData(this.pdfDocument, xmlData);
}
}
Utils.flattenForm(this.pdfDocument, ServiceUtil.getFontManagerService().getPdfFontSet(), forceAppearanceGen, this.retainUnsignedSignatureField);
}
private byte[] getXDPFromPDF() throws Exception {
byte[] xdpBytes = Utils.getXDPFromPDF(this.pdfDocument);
return Utils.overlayXMPMetadataFromPdf(this.pdfDocument, xdpBytes);
}
private byte[] preProcessPDF() throws Exception {
if (this.hasSignedSignatureField) {
this.signatureAppearances = Utils.retrieveSignatureAppearances(this.pdfDocument);
}
this.xfaImagesMap = Utils.extractXFAImagesFromPDF(this.pdfDocument, ServiceUtil.getTempFileManager());
return this.getXDPFromPDF();
}
private Document postProcessPDF(Document doc) throws Exception {
if (this.hasSignedSignatureField) {
return this.overlaySignatureAppearances(doc, this.signatureAppearances);
}
return doc;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private Document xfaRender(byte[] xdpTemplate, Document data, boolean xtgSignablePDFFlag, Map xfaImagesMap) throws Exception {
Document pdfDataDoc = null;
try {
String imageResourcesString = Utils.buildImageResourceString(xfaImagesMap);
if (data == null && !this.retainFormState) {
pdfDataDoc = new Document(Utils.exportXFAData(this.pdfDocument));
}
Document document = this.nonInteractivePDFRenderService.render(xdpTemplate, pdfDataDoc != null ? pdfDataDoc : data, this.lcOptions, imageResourcesString, xtgSignablePDFFlag, this.retainFormState && data == null);
return document;
}
finally {
if (pdfDataDoc != null) {
pdfDataDoc.dispose();
}
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private Document reRenderPDFForm(Document data, boolean xtgSignablePDFFlag) throws Exception {
try {
byte[] xdpTemplate = this.preProcessPDF();
Document doc = this.xfaRender(xdpTemplate, data, xtgSignablePDFFlag, this.xfaImagesMap);
Document document = this.postProcessPDF(doc);
return document;
}
finally {
if (this.xfaImagesMap != null) {
Utils.removeXFAImagesFromDisc(this.xfaImagesMap);
}
}
}
}