Example usage for com.itextpdf.text.pdf PdfSignatureAppearance setAcro6Layers

List of usage examples for com.itextpdf.text.pdf PdfSignatureAppearance setAcro6Layers

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfSignatureAppearance setAcro6Layers.

Prototype

public void setAcro6Layers(boolean acro6Layers) 

Source Link

Document

Acrobat 6.0 and higher recommends that only layer n0 and n2 be present.

Usage

From source file:SigningProcess.java

public static String sign(String base64, HashMap map) {
    String base64string = null;/*  w ww  . j ava2s  . c o  m*/
    try {
        System.out.println("map :" + map);
        // Getting a set of the entries
        Set set = map.entrySet();
        System.out.println("set :" + set);
        // Get an iterator
        Iterator it = set.iterator();
        // Display elements
        while (it.hasNext()) {
            Entry me = (Entry) it.next();
            String key = (String) me.getKey();
            if ("privateKey".equalsIgnoreCase(key)) {
                privateKey = (PrivateKey) me.getValue();
            }
            if ("certificateChain".equalsIgnoreCase(key)) {
                certificateChain = (X509Certificate[]) me.getValue();
            }
        }

        OcspClient ocspClient = new OcspClientBouncyCastle();
        TSAClient tsaClient = null;
        for (int i = 0; i < certificateChain.length; i++) {
            X509Certificate cert = (X509Certificate) certificateChain[i];
            String tsaUrl = CertificateUtil.getTSAURL(cert);
            if (tsaUrl != null) {
                tsaClient = new TSAClientBouncyCastle(tsaUrl);
                break;
            }
        }
        List<CrlClient> crlList = new ArrayList<CrlClient>();
        crlList.add(new CrlClientOnline(certificateChain));

        String property = System.getProperty("java.io.tmpdir");
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] FileByte = decoder.decodeBuffer(base64);
        writeByteArraysToFile(property + "_unsigned.pdf", FileByte);

        // Creating the reader and the stamper
        PdfReader reader = new PdfReader(property + "_unsigned.pdf");
        FileOutputStream os = new FileOutputStream(property + "_signed.pdf");
        PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
        // Creating the appearance
        PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
        //            appearance.setReason(reason);
        //            appearance.setLocation(location);
        appearance.setAcro6Layers(false);
        appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig1");
        // Creating the signature
        ExternalSignature pks = new PrivateKeySignature((PrivateKey) privateKey, DigestAlgorithms.SHA256,
                providerMSCAPI.getName());
        ExternalDigest digest = new BouncyCastleDigest();
        MakeSignature.signDetached(appearance, digest, pks, certificateChain, crlList, ocspClient, tsaClient, 0,
                MakeSignature.CryptoStandard.CMS);

        InputStream docStream = new FileInputStream(property + "_signed.pdf");
        byte[] encodeBase64 = Base64.encodeBase64(IOUtils.toByteArray(docStream));
        base64string = new String(encodeBase64);
    } catch (IOException ex) {
        System.out.println("Exception :" + ex.getLocalizedMessage());
    } catch (DocumentException ex) {
        System.out.println("Exception :" + ex.getLocalizedMessage());
    } catch (GeneralSecurityException ex) {
        System.out.println("Exception :" + ex.getLocalizedMessage());
    }
    return base64string;
}

From source file:de.rub.dez6a3.jpdfsigner.control.ITextSigner.java

License:Open Source License

public ByteArrayOutputStream doSign(byte[] pdf, Rectangle stampPos, int pageNmbrForStamp) throws IOException,
        DocumentException, NoSuchAlgorithmException, InvalidKeyException, SignatureException {
    Certificate[] chain = signCert.toArray(new Certificate[0]);

    PdfReader reader = new PdfReader(pdf);
    ByteArrayOutputStream byteOS = new ByteArrayOutputStream();
    PdfStamper stp = PdfStamper.createSignature(reader, byteOS, '\0', null, true);
    PdfSignatureAppearance sap = stp.getSignatureAppearance();
    if (stampPos != null) {
        sap.setVisibleSignature(//w  w  w  .  j ava2s  .  com
                new com.itextpdf.text.Rectangle(stampPos.x, stampPos.y, stampPos.width, stampPos.height),
                pageNmbrForStamp, null);
        sap.setRenderingMode(PdfSignatureAppearance.RenderingMode.NAME_AND_DESCRIPTION);
        sap.setAcro6Layers(true);
    }
    //        Siganture Appearance

    PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE, new PdfName("adbe.pkcs7.detached"));
    log.info("Creating signature with reason: " + ParamValidator.getInstance().getSignatureReason());
    sap.setReason(ParamValidator.getInstance().getSignatureReason());
    sap.setLocation("Ruhr-Universitt Bochum");
    Image i = Image.getInstance(getClass().getResource("/de/rub/dez6a3/jpdfsigner/resources/images/sign.png"));
    sap.setImage(i);
    sap.setCrypto((PrivateKey) signPrivKey, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
    dic.setReason(ParamValidator.getInstance().getSignatureReason());
    dic.setLocation("Ruhr-Universitt Bochum");
    sap.setCryptoDictionary(dic);
    // preserve some space for the contents
    int contentEstimated = 15000;
    HashMap<PdfName, Integer> exc = new HashMap<PdfName, Integer>();
    exc.put(PdfName.CONTENTS, new Integer(contentEstimated * 2 + 2));
    sap.preClose(exc);
    // make the digest
    InputStream data = sap.getRangeStream();
    MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
    byte buf[] = new byte[8192];
    int n;
    while ((n = data.read(buf)) > 0) {
        messageDigest.update(buf, 0, n);
    }
    byte hash[] = messageDigest.digest();
    Calendar cal = Calendar.getInstance();
    // If we add a time stamp:
    TSAClient tsc = new TSAClientBouncyCastle("http://zeitstempel.dfn.de/");
    // Create the signature

    PdfPKCS7 sgn;
    try {
        sgn = new PdfPKCS7((PrivateKey) signPrivKey, chain, null, "SHA1", null, false);
        byte sh[] = sgn.getAuthenticatedAttributeBytes(hash, cal, null);
        sgn.update(sh, 0, sh.length);
        byte[] encodedSig = sgn.getEncodedPKCS7(hash, cal, tsc, null);

        if (contentEstimated + 2 < encodedSig.length) {
            throw new DocumentException("Not enough space");
        }

        byte[] paddedSig = new byte[contentEstimated];
        System.arraycopy(encodedSig, 0, paddedSig, 0, encodedSig.length);
        // Replace the contents
        PdfDictionary dic2 = new PdfDictionary();
        dic2.put(PdfName.CONTENTS, new PdfString(paddedSig).setHexWriting(true));
        sap.close(dic2);
    } catch (NoSuchProviderException ex) {
        ex.printStackTrace();
    }
    return byteOS;
}

From source file:de.sign.SignMain.java

License:Open Source License

public void sign() throws DocumentException, IOException, GeneralSecurityException {

    PdfReader reader = new PdfReader(this.orgFile);
    OutputStream os = new FileOutputStream(this.orgFile.replace(".pdf", "SIGN.pdf"));
    PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');

    // Create appearance
    PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
    Rectangle cropBox = reader.getCropBox(1);
    float width = 50;
    float height = 50;
    Rectangle rectangle = new Rectangle(cropBox.getRight(width) - 20, cropBox.getTop(height) - 20,
            cropBox.getRight() - 20, cropBox.getTop() - 20);
    appearance.setVisibleSignature(rectangle, 1, "sig");
    appearance.setLocation(getHostname());
    appearance.setReason("Evidence of document integrity");
    appearance.setCertificationLevel(1); // 1 = CERTIFIED_NO_CHANGES_ALLOWED
    appearance.setAcro6Layers(false);
    appearance.setLayer2Text("");

    //Sign/* w w  w. jav a2s . c om*/
    Security.addProvider(new BouncyCastleProvider());
    TSAClient tsc = new TSAClientBouncyCastle(this.tsa_URL);
    ExternalDigest digest = new BouncyCastleDigest();
    ExternalSignature signature = new PrivateKeySignature(getPrivateKey(), "SHA-1", "BC");
    MakeSignature.signDetached(appearance, digest, signature, getCertificateChain(), null, null, tsc, 0,
            CryptoStandard.CMS);
}

From source file:gov.nih.nci.firebird.service.signing.DigitalSigningServiceBean.java

License:Open Source License

private PdfSignatureAppearance createPdfSigAppearance(PdfStamper stp,
        DigitalSigningAttributes signingAttributes, Certificate[] chain)
        throws IOException, BadElementException {
    PdfSignatureAppearance appearance = stp.getSignatureAppearance();
    appearance.setVisibleSignature(signingAttributes.getSigningFieldName());
    appearance.setReason(signingAttributes.getSigningReason());
    appearance.setSignDate(new GregorianCalendar());
    appearance.setCrypto(null, chain, null, null);
    appearance.setAcro6Layers(true);
    appearance.setRenderingMode(PdfSignatureAppearance.RenderingMode.DESCRIPTION);

    return appearance;
}