FlattenPDFFacade.java 7.08 KB
/*
 * 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);
            }
        }
    }
}