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

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

Introduction

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

Prototype

public PdfStamper(PdfReader reader, OutputStream os, char pdfVersion, boolean append)
        throws DocumentException, IOException 

Source Link

Document

Starts the process of adding extra content to an existing PDF document, possibly as a new revision.

Usage

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  w  w  w .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:eu.europa.ec.markt.dss.signature.pdf.itext.ITextPdfWriter.java

License:Open Source License

ITextPdfWriter(PdfReader reader, OutputStream output) throws IOException {
    try {// w ww .jav a  2s .  c  o  m
        this.wrapped = new PdfStamper(((ITextPdfReader) reader).wrapped, output, '\0', true);
    } catch (DocumentException e) {
        throw new IOException(e);
    }
}

From source file:questions.forms.FillEnabledForm.java

public static void main(String[] args) {
    try {/*from   w ww .  j  a v  a 2s  . c om*/
        PdfReader reader = new PdfReader(ENABLED_FORM);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT), '\0', true);
        AcroFields form = stamper.getAcroFields();
        form.setField("form1[0].#subform[0].Body[0].EmployeeName[0]", "Bruno Lowagie");
        form.setField("form1[0].#subform[0].Body[0].Address[0]", "Ad. Baeyensstraat 121");
        form.setField("form1[0].#subform[0].Body[0].ZipCode[0]", "9040");
        form.setField("form1[0].#subform[0].Body[0].Comments[0]",
                "The example FillEnabledForm shows how to prefill a Reader Enabled form preserving the user permissions.");
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}