Example usage for com.lowagie.text.pdf PdfStamper getWriter

List of usage examples for com.lowagie.text.pdf PdfStamper getWriter

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfStamper getWriter.

Prototype

public PdfWriter getWriter() 

Source Link

Document

Gets the underlying PdfWriter.

Usage

From source file:es.gob.afirma.signers.pades.PdfPreProcessor.java

License:Open Source License

static void attachFile(final Properties extraParams, final PdfStamper stp) throws IOException {
    if (extraParams == null) {
        return;// w  ww  . j  a  v a  2 s. c  om
    }
    if (stp == null) {
        throw new IllegalArgumentException("No se puede adjuntar un fichero a un PdfStamper nulo"); //$NON-NLS-1$
    }
    // Contenido a adjuntar (en Base64)
    final String b64Attachment = extraParams.getProperty("attach"); //$NON-NLS-1$

    // Nombre que se pondra al fichero adjunto en el PDF
    final String attachmentFileName = extraParams.getProperty("attachFileName"); //$NON-NLS-1$

    // Descripcion del adjunto
    final String attachmentDescription = extraParams.getProperty("attachDescription"); //$NON-NLS-1$

    if (b64Attachment != null && attachmentFileName != null) {
        final byte[] attachment;
        try {
            attachment = Base64.decode(b64Attachment);
        } catch (final IOException e) {
            LOGGER.warning("Se ha indicado un adjunto, pero no estaba en formato Base64, se ignorara : " + e); //$NON-NLS-1$
            return;
        }
        stp.getWriter().addFileAttachment(attachmentDescription, attachment, null, attachmentFileName);
    }

}

From source file:es.gob.afirma.signers.pades.PdfSessionManager.java

License:Open Source License

/** Obtiene los datos PDF relevantes en cuanto a las firmas electrónicas, consistentes en los datos
 * a ser firmados con CAdES o PKCS#7 y los metadatos necesarios para su correcta inserción en el PDF.
 * @param inPDF Documento PDF que se desea firmar
 * @param certChain Cadena de certificados del firmante
 * @param signTime Hora de la firma/*from  w  w  w.  j  a v  a 2  s.com*/
 * @param extraParams Parámetros adicionales de la firma
 * @return Datos PDF relevantes en cuanto a las firmas electrónicas
 * @throws AOException En caso de que ocurra cualquier otro tipo de error
 * @throws IOException En caso de errores de entrada / salida
 * @throws DocumentException Si ocurren errores en la estampaciín de la firma PDF */
public static PdfTriPhaseSession getSessionData(final byte[] inPDF, final Certificate[] certChain,
        final Calendar signTime, final Properties extraParams)
        throws AOException, IOException, DocumentException {

    // *********************************************************************************************************************
    // **************** LECTURA PARAMETROS ADICIONALES *********************************************************************
    // *********************************************************************************************************************

    // Forzar la creacion de revisiones incluso en PDF no firmados
    final boolean alwaysCreateRevision = Boolean
            .parseBoolean(extraParams.getProperty("alwaysCreateRevision", "true")); //$NON-NLS-1$ //$NON-NLS-2$

    // Imagen de la rubrica
    final Image rubric = PdfPreProcessor.getImage(extraParams.getProperty("signatureRubricImage")); //$NON-NLS-1$

    // Motivo de la firma
    final String reason = extraParams.getProperty("signReason"); //$NON-NLS-1$

    // Nombre del campo de firma preexistente en el PDF a usar
    final String signatureField = extraParams.getProperty("signatureField"); //$NON-NLS-1$

    // Lugar de realizacion de la firma
    final String signatureProductionCity = extraParams.getProperty("signatureProductionCity"); //$NON-NLS-1$

    // Datos de contacto (correo electronico) del firmante
    final String signerContact = extraParams.getProperty("signerContact"); //$NON-NLS-1$

    // Pagina donde situar la firma visible
    int page = LAST_PAGE;
    try {
        page = Integer.parseInt(extraParams.getProperty("signaturePage")); //$NON-NLS-1$
    } catch (final Exception e) {
        /* Se deja la pagina tal y como esta */
    }

    // Nombre del subfiltro de firma en el diccionario PDF
    final String signatureSubFilter = extraParams.getProperty("signatureSubFilter"); //$NON-NLS-1$

    // Nivel de certificacion del PDF
    int certificationLevel;
    try {
        certificationLevel = extraParams.getProperty("certificationLevel") != null ? //$NON-NLS-1$
                Integer.parseInt(extraParams.getProperty("certificationLevel")) : //$NON-NLS-1$
                -1;
    } catch (final Exception e) {
        certificationLevel = UNDEFINED;
    }

    // *****************************
    // **** Texto firma visible ****

    // Texto en capa 4
    final String layer4Text = extraParams.getProperty("layer4Text"); //$NON-NLS-1$

    // Texto en capa 2
    final String layer2Text = extraParams.getProperty("layer2Text"); //$NON-NLS-1$

    // Tipo de letra en capa 2
    int layer2FontFamily;
    try {
        layer2FontFamily = extraParams.getProperty("layer2FontFamily") != null ? //$NON-NLS-1$
                Integer.parseInt(extraParams.getProperty("layer2FontFamily")) : //$NON-NLS-1$
                -1;
    } catch (final Exception e) {
        layer2FontFamily = UNDEFINED;
    }

    // Tamano del tipo de letra en capa 2
    int layer2FontSize;
    try {
        layer2FontSize = extraParams.getProperty("layer2FontSize") != null ? //$NON-NLS-1$
                Integer.parseInt(extraParams.getProperty("layer2FontSize")) : //$NON-NLS-1$
                -1;
    } catch (final Exception e) {
        layer2FontSize = UNDEFINED;
    }

    // Estilo del tipo de letra en capa 2
    int layer2FontStyle;
    try {
        layer2FontStyle = extraParams.getProperty("layer2FontStyle") != null ? //$NON-NLS-1$
                Integer.parseInt(extraParams.getProperty("layer2FontStyle")) : //$NON-NLS-1$
                -1;
    } catch (final Exception e) {
        layer2FontStyle = UNDEFINED;
    }

    // Color del tipo de letra en capa 2
    final String layer2FontColor = extraParams.getProperty("layer2FontColor"); //$NON-NLS-1$

    // ** Fin texto firma visible **
    // *****************************

    // *********************************************************************************************************************
    // **************** FIN LECTURA PARAMETROS ADICIONALES *****************************************************************
    // *********************************************************************************************************************

    final PdfReader pdfReader = PdfUtil.getPdfReader(inPDF, extraParams,
            Boolean.getBoolean(extraParams.getProperty("headLess")) //$NON-NLS-1$
    );

    //PdfUtil.checkPdfCertification(pdfReader.getCertificationLevel(), extraParams);
    //PdfUtil.checkUnregisteredSignatures(pdfReader, extraParams);

    // Los derechos van firmados por Adobe, y como desde iText se invalidan
    // es mejor quitarlos
    pdfReader.removeUsageRights();

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();

    // Activar el atributo de "agregar firma" (quinto parametro del metodo
    // "PdfStamper.createSignature") hace que se cree una nueva revision del
    // documento y evita que las firmas previas queden invalidadas.
    // Sin embargo, este procedimiento no funciona cuando los PDF contienen informacion
    // despues de la ultima marca %%EOF, aspecto no permitido en PDF 1.7 (ISO 32000-1:2008)
    // pero si en PDF 1.3 (Adobe) y que se da con frecuencia en PDF generados con bibliotetcas
    // de software libre como QPDF.
    //
    //  Especificacion PDF 1.3
    //   3.4.4, "File Trailer"
    //     Acrobat viewers require only that the %%EOF marker appear somewhere within
    //     the last 1024 bytes of the file.
    //
    //  Especificacion PDF 1.7
    //   7.5.5. File Trailer
    //     The trailer of a PDF file enables a conforming reader to quickly find the
    //     cross-reference table and certain special objects. Conforming readers should read a
    //     PDF file from its end. The last line of the file shall contain only the end-of-file
    //     marker, %%EOF.
    //
    // Para aceptar al menos en algunos casos PDF 1.3 (son aun muy frecuentes, especialmente
    // en archivos, lo mantendremos desactivado para la primera firma y activado para las
    // subsiguientes.
    //
    // No obstante, el integrador puede siempre forzar la creacion de revisiones mediante
    // el parametro "alwaysCreateRevision".
    final PdfStamper stp;
    try {
        stp = PdfStamper.createSignature(
                // PDF de entrada
                pdfReader,
                // Salida
                baos,
                // Mantener version
                '\0',
                // No crear temporal
                null,
                // Si hay mas firmas o asi me lo indican, creo una revision
                alwaysCreateRevision || pdfReader.getAcroFields().getSignatureNames().size() > 0,
                // Momento de la firma
                signTime);
    } catch (final BadPasswordException e) {
        // Comprobamos que el signer esta en modo interactivo, y si no lo
        // esta no pedimos contrasena por dialogo, principalmente para no interrumpir un firmado por lotes
        // desatendido
        if (Boolean.getBoolean(extraParams.getProperty("headLess"))) { //$NON-NLS-1$
            throw new BadPdfPasswordException(e);
        }
        // La contrasena que nos han proporcionada no es buena o no nos
        // proporcionaron ninguna
        final String userPwd = new String(AOUIFactory.getPassword(
                extraParams.getProperty("userPassword") == null ? CommonPdfMessages.getString("AOPDFSigner.0") //$NON-NLS-1$//$NON-NLS-2$
                        : CommonPdfMessages.getString("AOPDFSigner.1"), //$NON-NLS-1$
                null));
        if ("".equals(userPwd)) { //$NON-NLS-1$
            throw new AOCancelledOperationException("Entrada de contrasena de PDF cancelada por el usuario", e //$NON-NLS-1$
            );
        }
        extraParams.put("userPassword", userPwd); //$NON-NLS-1$
        return getSessionData(inPDF, certChain, signTime, extraParams);
    }

    // Aplicamos todos los atributos de firma
    final PdfSignatureAppearance sap = stp.getSignatureAppearance();
    stp.setFullCompression();
    sap.setAcro6Layers(true);

    PdfUtil.enableLtv(stp);

    // Adjuntos
    PdfPreProcessor.attachFile(extraParams, stp);

    // Imagenes
    PdfPreProcessor.addImage(extraParams, stp);

    // Establecemos el render segun iText antiguo, varia en versiones modernas
    sap.setRender(PdfSignatureAppearance.SignatureRenderDescription);

    // Razon de firma
    if (reason != null) {
        sap.setReason(reason);
    }

    sap.setSignDate(signTime);

    // Gestion de los cifrados
    PdfUtil.managePdfEncryption(stp, pdfReader, extraParams);

    // Pagina en donde se imprime la firma
    if (page == LAST_PAGE) {
        page = pdfReader.getNumberOfPages();
    }

    // Posicion de la firma
    final Rectangle signaturePositionOnPage = getSignaturePositionOnPage(extraParams);
    if (signaturePositionOnPage != null && signatureField == null) {
        sap.setVisibleSignature(signaturePositionOnPage, page, null);
    } else if (signatureField != null) {
        sap.setVisibleSignature(signatureField);
    }

    // Localizacion en donde se produce la firma
    if (signatureProductionCity != null) {
        sap.setLocation(signatureProductionCity);
    }

    // Contacto del firmante
    if (signerContact != null) {
        sap.setContact(signerContact);
    }

    // Rubrica de la firma
    if (rubric != null) {
        sap.setImage(rubric);
        sap.setLayer2Text(""); //$NON-NLS-1$
        sap.setLayer4Text(""); //$NON-NLS-1$
    }

    // **************************
    // ** Texto en las capas ****
    // **************************

    // Capa 2
    if (layer2Text != null) {

        sap.setLayer2Text(layer2Text);

        final int layer2FontColorR;
        final int layer2FontColorG;
        final int layer2FontColorB;

        if ("black".equalsIgnoreCase(layer2FontColor)) { //$NON-NLS-1$
            layer2FontColorR = 0;
            layer2FontColorG = 0;
            layer2FontColorB = 0;
        } else if ("white".equalsIgnoreCase(layer2FontColor)) { //$NON-NLS-1$
            layer2FontColorR = 255;
            layer2FontColorG = 255;
            layer2FontColorB = 255;
        } else if ("lightGray".equalsIgnoreCase(layer2FontColor)) { //$NON-NLS-1$
            layer2FontColorR = 192;
            layer2FontColorG = 192;
            layer2FontColorB = 192;
        } else if ("gray".equalsIgnoreCase(layer2FontColor)) { //$NON-NLS-1$
            layer2FontColorR = 128;
            layer2FontColorG = 128;
            layer2FontColorB = 128;
        } else if ("darkGray".equalsIgnoreCase(layer2FontColor)) { //$NON-NLS-1$
            layer2FontColorR = 64;
            layer2FontColorG = 64;
            layer2FontColorB = 64;
        } else if ("red".equalsIgnoreCase(layer2FontColor)) { //$NON-NLS-1$
            layer2FontColorR = 255;
            layer2FontColorG = 0;
            layer2FontColorB = 0;
        } else if ("pink".equalsIgnoreCase(layer2FontColor)) { //$NON-NLS-1$
            layer2FontColorR = 255;
            layer2FontColorG = 175;
            layer2FontColorB = 175;
        } else if (layer2FontColor == null) {
            layer2FontColorR = 0;
            layer2FontColorG = 0;
            layer2FontColorB = 0;
        } else {
            LOGGER.warning("No se soporta el color '" + layer2FontColor //$NON-NLS-1$
                    + "' para el texto de la capa 4, se usara negro"); //$NON-NLS-1$
            layer2FontColorR = 0;
            layer2FontColorG = 0;
            layer2FontColorB = 0;
        }

        com.lowagie.text.Font font;
        try {
            Class<?> colorClass;
            if (Platform.getOS() == OS.ANDROID) {
                colorClass = Class.forName("harmony.java.awt.Color"); //$NON-NLS-1$
            } else {
                colorClass = Class.forName("java.awt.Color"); //$NON-NLS-1$
            }
            final Object color = colorClass.getConstructor(Integer.TYPE, Integer.TYPE, Integer.TYPE)
                    .newInstance(Integer.valueOf(layer2FontColorR), Integer.valueOf(layer2FontColorG),
                            Integer.valueOf(layer2FontColorB));

            font = com.lowagie.text.Font.class
                    .getConstructor(Integer.TYPE, Float.TYPE, Integer.TYPE, colorClass).newInstance(
                            // Family (COURIER = 0, HELVETICA = 1, TIMES_ROMAN = 2, SYMBOL = 3, ZAPFDINGBATS = 4)
                            Integer.valueOf(layer2FontFamily == UNDEFINED ? COURIER : layer2FontFamily),
                            // Size (DEFAULTSIZE = 12)
                            Float.valueOf(
                                    layer2FontSize == UNDEFINED ? DEFAULT_LAYER_2_FONT_SIZE : layer2FontSize),
                            // Style (NORMAL = 0, BOLD = 1, ITALIC = 2, BOLDITALIC = 3, UNDERLINE = 4, STRIKETHRU = 8)
                            Integer.valueOf(layer2FontStyle == UNDEFINED ? com.lowagie.text.Font.NORMAL
                                    : layer2FontStyle),
                            // Color
                            color);
        } catch (final Exception e) {
            font = new com.lowagie.text.Font(
                    // Family (COURIER = 0, HELVETICA = 1, TIMES_ROMAN = 2, SYMBOL = 3, ZAPFDINGBATS = 4)
                    layer2FontFamily == UNDEFINED ? COURIER : layer2FontFamily,
                    // Size (DEFAULTSIZE = 12)
                    layer2FontSize == UNDEFINED ? DEFAULT_LAYER_2_FONT_SIZE : layer2FontSize,
                    // Style (NORMAL = 0, BOLD = 1, ITALIC = 2, BOLDITALIC = 3, UNDERLINE = 4, STRIKETHRU = 8)
                    layer2FontStyle == UNDEFINED ? com.lowagie.text.Font.NORMAL : layer2FontStyle,
                    // Color
                    null);
        }
        sap.setLayer2Font(font);
    }

    // Capa 4
    if (layer4Text != null) {
        sap.setLayer4Text(layer4Text);
    }

    // ***************************
    // ** Fin texto en las capas *
    // ***************************

    sap.setCrypto(null, certChain, null, null);

    final PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE,
            signatureSubFilter != null && !"".equals(signatureSubFilter) ? new PdfName(signatureSubFilter) //$NON-NLS-1$
                    : PdfName.ADBE_PKCS7_DETACHED);

    // Fecha de firma
    if (sap.getSignDate() != null) {
        dic.setDate(new PdfDate(sap.getSignDate()));
    }

    dic.setName(PdfPKCS7.getSubjectFields((X509Certificate) certChain[0]).getField("CN")); //$NON-NLS-1$

    if (sap.getReason() != null) {
        dic.setReason(sap.getReason());
    }

    // Lugar de la firma
    if (sap.getLocation() != null) {
        dic.setLocation(sap.getLocation());
    }

    // Contacto del firmante
    if (sap.getContact() != null) {
        dic.setContact(sap.getContact());
    }

    sap.setCryptoDictionary(dic);

    // Certificacion del PDF (NOT_CERTIFIED = 0, CERTIFIED_NO_CHANGES_ALLOWED = 1,
    // CERTIFIED_FORM_FILLING = 2, CERTIFIED_FORM_FILLING_AND_ANNOTATIONS = 3)
    if (certificationLevel != -1) {
        sap.setCertificationLevel(certificationLevel);
    }

    // Reservamos el espacio necesario en el PDF para insertar la firma
    final HashMap<PdfName, Integer> exc = new HashMap<PdfName, Integer>();
    exc.put(PdfName.CONTENTS, Integer.valueOf(CSIZE * 2 + 2));

    sap.preClose(exc, signTime);

    final PdfObject pdfObject = ((com.lowagie.text.pdf.PdfStamperImp) stp.getWriter()).getFileID();

    return new PdfTriPhaseSession(sap, baos, new String(pdfObject.getBytes()));
}

From source file:eu.europa.ec.markt.dss.signature.pades.PAdESProfileLTV.java

License:Open Source License

private PdfIndirectReference buildVRIDict(PdfStamper stamper, BasicOCSPResp crl) throws IOException {
    PdfIndirectReference ref = stamper.getWriter().getPdfIndirectReference();
    PdfDictionary ocspVriDictionary = new PdfDictionary();
    PdfDate vriDate = new PdfDate(Calendar.getInstance(TimeZone.getTimeZone("GMT")));
    ocspVriDictionary.put(new PdfName("TU"), vriDate);

    // Other objects?

    stamper.getWriter().addToBody(ocspVriDictionary, ref, false);
    return ref;//  www . j  a  v a 2s . co  m
}

From source file:eu.europa.ec.markt.dss.signature.pades.PAdESProfileLTV.java

License:Open Source License

private PdfIndirectReference buildVRIDict(PdfStamper stamper, X509CRL crl) throws IOException {
    PdfIndirectReference ref = stamper.getWriter().getPdfIndirectReference();
    PdfDictionary crlVriDictionary = new PdfDictionary();
    PdfDate vriDate = new PdfDate(Calendar.getInstance(TimeZone.getTimeZone("GMT")));
    crlVriDictionary.put(new PdfName("TU"), vriDate);

    // Other objects?

    stamper.getWriter().addToBody(crlVriDictionary, ref, false);
    return ref;/*www.j  a  v  a2s .c  o m*/
}

From source file:eu.europa.ec.markt.dss.signature.pades.PAdESProfileLTV.java

License:Open Source License

private void integrateCRL(LTVSignatureValidationCallback callback, PdfStamper stamper,
        PdfDictionary dssDictionary, PdfDictionary sigVriDictionary, PdfDictionary vriDictionary)
        throws IOException {
    if (callback.getCrlsArray().size() > 0) {
        // Reference in the DSS dictionary
        PdfIndirectReference crlsRef = stamper.getWriter().getPdfIndirectReference();
        stamper.getWriter().addToBody(callback.getCrlsArray(), crlsRef, false);
        dssDictionary.put(new PdfName("CRLs"), crlsRef);

        // Array in the signature's VRI dictionary
        PdfIndirectReference sigVriCrlRef = stamper.getWriter().getPdfIndirectReference();
        stamper.getWriter().addToBody(callback.getCrlsArray(), sigVriCrlRef, false);
        sigVriDictionary.put(new PdfName("CRL"), sigVriCrlRef);

        // Build and reference a VRI dictionary for each CRL
        for (X509CRL crl : crlRefs.keySet()) {
            try {
                PdfIndirectReference vriRef = buildVRIDict(stamper, crl);
                MessageDigest md = MessageDigest.getInstance(DigestAlgorithm.SHA1.getName());
                String hexHash = Hex.encodeHexString(md.digest(crl.getSignature())).toUpperCase();
                vriDictionary.put(new PdfName(hexHash), vriRef);
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException();
            }/*from   w  ww.  j  av a  2  s.c  o m*/
        }
    }
}

From source file:eu.europa.ec.markt.dss.signature.pades.PAdESProfileLTV.java

License:Open Source License

private void integrateOCSP(LTVSignatureValidationCallback callback, PdfStamper stamper,
        PdfDictionary dssDictionary, PdfDictionary sigVriDictionary, PdfDictionary vriDictionary)
        throws IOException {
    if (callback.getOcspsArray().size() > 0) {
        // Reference in the DSS dictionary
        PdfIndirectReference ocspsRef = stamper.getWriter().getPdfIndirectReference();
        stamper.getWriter().addToBody(callback.getOcspsArray(), ocspsRef, false);
        dssDictionary.put(new PdfName("OCSPs"), ocspsRef);

        // Array in the signature's VRI dictionary
        PdfIndirectReference sigVriOcspRef = stamper.getWriter().getPdfIndirectReference();
        stamper.getWriter().addToBody(callback.getOcspsArray(), sigVriOcspRef, false);
        sigVriDictionary.put(new PdfName("OCSP"), sigVriOcspRef);

        // Build and reference a VRI dictionary for each OCSP response
        for (BasicOCSPResp ocsp : ocspRefs.keySet()) {
            try {
                PdfIndirectReference vriRef = buildVRIDict(stamper, ocsp);
                MessageDigest md = MessageDigest.getInstance(DigestAlgorithm.SHA1.getName());
                String hexHash = Hex.encodeHexString(md.digest(ocsp.getSignature())).toUpperCase();
                vriDictionary.put(new PdfName(hexHash), vriRef);
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException();
            }/*w w w  .java 2 s . c om*/
        }
    }
}

From source file:eu.europa.ec.markt.dss.signature.pades.PAdESProfileLTV.java

License:Open Source License

@Override
public Document extendSignatures(Document document, Document originalData, SignatureParameters parameters)
        throws IOException {

    try {//from   www  .  j  a  va 2 s  .  c  o  m
        final PdfReader reader = new PdfReader(document.openStream());
        final ByteArrayOutputStream output = new ByteArrayOutputStream();
        final PdfStamper stamper = new PdfStamper(reader, output, '\0', true);

        LTVSignatureValidationCallback callback = new LTVSignatureValidationCallback(stamper);
        pdfSignatureService.validateSignatures(document.openStream(), callback);

        PdfIndirectReference certsRef = stamper.getWriter().getPdfIndirectReference();
        stamper.getWriter().addToBody(callback.getCertsArray(), certsRef, false);

        PdfDictionary dssDictionary = new PdfDictionary(new PdfName("DSS"));
        PdfDictionary vriDictionary = new PdfDictionary(new PdfName("VRI"));

        PdfDictionary sigVriDictionary = new PdfDictionary();

        integrateCRL(callback, stamper, dssDictionary, sigVriDictionary, sigVriDictionary);

        integrateOCSP(callback, stamper, dssDictionary, sigVriDictionary, sigVriDictionary);

        // Add the signature's VRI dictionary, hashing the signature block from the callback method
        MessageDigest _md = MessageDigest.getInstance(DigestAlgorithm.SHA1.getName());
        String hexHash = Hex.encodeHexString(_md.digest(callback.getSignatureBlock())).toUpperCase();

        PdfIndirectReference sigVriRef = stamper.getWriter().getPdfIndirectReference();
        stamper.getWriter().addToBody(sigVriDictionary, sigVriRef, false);
        vriDictionary.put(new PdfName(hexHash), sigVriRef);
        PdfIndirectReference vriRef = stamper.getWriter().getPdfIndirectReference();
        stamper.getWriter().addToBody(vriDictionary, vriRef, false);

        // Add final objects to DSS dictionary
        dssDictionary.put(new PdfName("VRI"), vriRef);
        dssDictionary.put(new PdfName("Certs"), certsRef);

        PdfIndirectReference dssRef = stamper.getWriter().getPdfIndirectReference();
        stamper.getWriter().addToBody(dssDictionary, dssRef, false);
        reader.getCatalog().put(new PdfName("DSS"), dssRef);

        // /Extensions<</ADBE<</BaseVersion/1.7/ExtensionLevel 5>>>>
        PdfDeveloperExtension etsiExtension = new PdfDeveloperExtension(PdfName.ADBE, new PdfName("1.7"), 5);
        stamper.getWriter().addDeveloperExtension(etsiExtension);
        stamper.getWriter().addToBody(reader.getCatalog(), reader.getCatalog().getIndRef(), false);

        stamper.close();
        output.close();

        Document extendedDocument = new InMemoryDocument(output.toByteArray());

        ByteArrayOutputStream ltvDoc = new ByteArrayOutputStream();

        ITextPDFDocTimeSampService service = new ITextPDFDocTimeSampService();
        byte[] digest = service.digest(extendedDocument.openStream(), parameters);
        TimeStampResponse tsToken = tspSource.getTimeStampResponse(parameters.getDigestAlgorithm(), digest);
        service.sign(extendedDocument.openStream(), tsToken.getTimeStampToken().getEncoded(), ltvDoc,
                parameters);

        return new InMemoryDocument(ltvDoc.toByteArray());

    } catch (DocumentException ex) {
        throw new RuntimeException(ex);
    } catch (SignatureException e) {
        throw new RuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }

}

From source file:org.pdfsam.console.business.pdf.handlers.interfaces.AbstractCmdExecutor.java

License:Open Source License

/**
 * Sets the compression settings on the pdf stamper depending on the inputCommand
 * //from w  w w  .  j av  a  2  s . c o m
 * @param inputCommand
 * @param pdfWriter
 */
protected void setCompressionSettingOnStamper(AbstractParsedCommand inputCommand, PdfStamper pdfStamper) {
    if (inputCommand.isCompress()) {
        pdfStamper.setFullCompression();
        pdfStamper.getWriter().setCompressionLevel(PdfStream.BEST_COMPRESSION);
    }
}

From source file:questions.forms.AddActionToField.java

public static void main(String[] args) {
    try {//w  ww. j av a2  s .c o  m
        PdfReader reader = new PdfReader(RESOURCE);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        AcroFields form = stamper.getAcroFields();
        Item fd = form.getFieldItem("Who");
        PdfDictionary dict = (PdfDictionary) PdfReader.getPdfObject((PdfObject) fd.getWidgetRef(0));
        PdfDictionary aa = dict.getAsDict(PdfName.AA);
        if (aa == null)
            aa = new PdfDictionary();
        aa.put(new PdfName("Fo"),
                PdfAction.javaScript("app.alert('Who has got the focus!?');", stamper.getWriter()));
        dict.put(PdfName.AA, aa);
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:questions.forms.AddFieldToExistingForm.java

public static void main(String[] args) {
    PdfReader reader;/*from ww w .  ja v a 2  s  .  c  o m*/
    try {
        reader = new PdfReader(FORM);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        TextField tf = new TextField(stamper.getWriter(), new Rectangle(100, 760, 400, 785), "added_field");
        tf.setText("\u00e4\u00f4\u00df");
        tf.setOptions(TextField.READ_ONLY);
        stamper.addAnnotation(tf.getTextField(), 1);
        AcroFields form = stamper.getAcroFields();
        form.setField("Who", "\u00e4\u00f3\u00df\u00f4");
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}