Example usage for com.lowagie.text.pdf AcroFields getSignatureDictionary

List of usage examples for com.lowagie.text.pdf AcroFields getSignatureDictionary

Introduction

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

Prototype

public PdfDictionary getSignatureDictionary(String name) 

Source Link

Document

Gets the signature dictionary, the one keyed by /V.

Usage

From source file:androidGLUESigner.pdf.PDFSigExtractor.java

License:Open Source License

/**
 * extracts the signature field for previewing.
 * @throws IOException //from   w  w  w .j  av  a 2 s  .c  om
 */
public static ArrayList<SignatureInfo> getSignatureInfo(String inputPath) {
    PdfReader reader;
    try {
        reader = new PdfReader(inputPath);
    } catch (IOException e) {
        return new ArrayList<SignatureInfo>();
    }
    AcroFields af = reader.getAcroFields();
    ArrayList names = af.getSignatureNames();
    ArrayList<SignatureInfo> signatures = new ArrayList<SignatureInfo>();
    // For every signature :
    for (int k = 0; k < names.size(); ++k) {
        String name = (String) names.get(k);
        SignatureInfo sigInfo = new SignatureInfo();
        // get coordinates
        float[] position = af.getFieldPositions(name);
        // page number
        float page = position[0];
        // left
        float llx = position[1];
        // bottom
        float lly = position[2];
        // right
        float urx = position[3];
        // top
        float ury = position[4];

        // get size of pdf page
        Rectangle size = reader.getPageSize((int) page);
        float height = size.getHeight();
        // subtract height to translate to Android canvas coordinate system
        lly = height - lly;
        ury = height - ury;
        float ulx = llx;

        // create a Rectangle from obtained signature field coordinates
        Rect sigRect = new Rect((int) ulx, (int) ury, (int) urx, (int) lly);
        sigInfo.setGraphicRect(sigRect, 1.0f);
        // obtain additional information like reason, location, ...
        PdfDictionary sig = af.getSignatureDictionary(name);
        sigInfo.setSignatureName(sig.getAsString(PdfName.NAME).toString());
        sigInfo.setSignatureLocation(sig.getAsString(PdfName.LOCATION).toString());
        sigInfo.setSignatureReason(sig.getAsString(PdfName.REASON).toString());
        sigInfo.setSignatureType(SignatureType.NORMAL);
        sigInfo.setPageNumber((int) page);
        // add new signature information to signatures
        signatures.add(sigInfo);
    }
    return signatures;
}

From source file:eu.europa.ec.markt.dss.signature.pdf.itext.ITextPDFDocTimeSampService.java

License:Open Source License

@SuppressWarnings("unchecked")
private void validateSignatures(InputStream input, PdfDict outerCatalog, SignatureValidationCallback callback,
        List<String> alreadyLoadedRevisions) throws IOException, SignatureException {

    PdfReader reader = new PdfReader(input);
    AcroFields af = reader.getAcroFields();

    /*// w  w  w  .j av  a2 s  .  c o m
     * Search the whole document of a signature
     */
    ArrayList<String> names = af.getSignatureNames();

    LOG.info(names.size() + " signature(s)");
    // For every signature :
    for (String name : names) {

        // Affichage du nom
        LOG.info("Signature name: " + name);
        LOG.info("Signature covers whole document: " + af.signatureCoversWholeDocument(name));
        // Affichage sur les revision - version
        LOG.info("Document revision: " + af.getRevision(name) + " of " + af.getTotalRevisions());

        /*
         * We are only interested in the validation of signature that covers the whole document.
         */
        if (af.signatureCoversWholeDocument(name)) {

            PdfPKCS7 pk = af.verifySignature(name);
            Calendar cal = pk.getSignDate();
            Certificate pkc[] = pk.getCertificates();

            PdfDict signatureDictionary = new ITextPdfDict(af.getSignatureDictionary(name));
            String revisionName = Integer.toString(af.getRevision(name));
            if (!alreadyLoadedRevisions.contains(revisionName)) {
                callback.validate(new ITextPdfDict(reader.getCatalog()), outerCatalog,
                        pk.getSigningCertificate(), cal != null ? cal.getTime() : null, pkc,
                        signatureDictionary, new ITextPdfSignatureInfo(pk));
                alreadyLoadedRevisions.add(revisionName);
            }

        } else {

            PdfDict catalog = new ITextPdfDict(reader.getCatalog());

            /*
             * We open the version of the document that was protected by the signature
             */
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            InputStream ip = af.extractRevision(name);
            IOUtils.copy(ip, out);
            out.close();
            ip.close();

            /*
             * You can sign a PDF document with only one signature. So when we want the multiple signatures, the
             * signatures are appended sequentially to the end of the document. The recursive call helps to get the
             * signature from the original document.
             */
            validateSignatures(new ByteArrayInputStream(out.toByteArray()), catalog, callback,
                    alreadyLoadedRevisions);

        }
    }

}

From source file:eu.europa.ec.markt.dss.signature.pdf.itext.ITextPDFSignatureService.java

License:Open Source License

@SuppressWarnings("unchecked")
private void validateSignatures(InputStream input, PdfDict outerCatalog, SignatureValidationCallback callback,
        List<String> alreadyLoadedRevisions) throws IOException, SignatureException {

    PdfReader reader = new PdfReader(input);
    AcroFields af = reader.getAcroFields();

    /*//w  w w . j ava 2  s .c o m
     * Search the whole document of a signature
     */
    ArrayList<String> names = af.getSignatureNames();

    LOG.info(names.size() + " signature(s)");
    // For every signature :
    for (String name : names) {

        // Affichage du nom
        LOG.info("Signature name: " + name);
        LOG.info("Signature covers whole document: " + af.signatureCoversWholeDocument(name));
        // Affichage sur les revision - version
        LOG.info("Document revision: " + af.getRevision(name) + " of " + af.getTotalRevisions());

        /*
         * We are only interested in the validation of signature that covers the whole document.
         */
        if (af.signatureCoversWholeDocument(name)) {

            PdfPKCS7 pk = af.verifySignature(name);
            Calendar cal = pk.getSignDate();
            Certificate pkc[] = pk.getCertificates();

            PdfDict signatureDictionary = new ITextPdfDict(af.getSignatureDictionary(name));
            String revisionName = Integer.toString(af.getRevision(name));
            if (!alreadyLoadedRevisions.contains(revisionName)) {
                callback.validate(new ITextPdfDict(reader.getCatalog()), outerCatalog,
                        pk.getSigningCertificate(), cal != null ? cal.getTime() : null, pkc,
                        signatureDictionary, new ITextPdfSignatureInfo(pk));
                alreadyLoadedRevisions.add(revisionName);
            }

        } else {

            PdfDict catalog = new ITextPdfDict(reader.getCatalog());

            /*
             * We open the version of the document that was protected by the signature
             */
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            InputStream ip = af.extractRevision(name);
            IOUtils.copy(ip, out);
            out.close();
            ip.close();

            /*
             * You can sign a PDF document with only one signature. So when we want multiple signature, signatures are
             * appended sequentially to the end of the document. The recursive call help to get the signature from the
             * original document.
             */
            validateSignatures(new ByteArrayInputStream(out.toByteArray()), catalog, callback,
                    alreadyLoadedRevisions);
        }
    }
}

From source file:eu.europa.ec.markt.dss.signature.pdf.ITextPDFDocTimeSampService.java

License:Open Source License

@SuppressWarnings("unchecked")
private void validateSignatures(InputStream input, PdfDictionary outerCatalog,
        SignatureValidationCallback callback, List<String> alreadyLoadedRevisions)
        throws IOException, SignatureException {

    PdfReader reader = new PdfReader(input);
    AcroFields af = reader.getAcroFields();

    /*// w  ww. ja v  a2  s  . co m
     * Search the whole document of a signature
     */
    ArrayList<String> names = af.getSignatureNames();

    LOG.info(names.size() + " signature(s)");
    // For every signature :
    for (String name : names) {

        // Affichage du nom
        LOG.info("Signature name: " + name);
        LOG.info("Signature covers whole document: " + af.signatureCoversWholeDocument(name));
        // Affichage sur les revision - version
        LOG.info("Document revision: " + af.getRevision(name) + " of " + af.getTotalRevisions());

        /*
         * We are only interrested in the validation of signature that covers the whole document.
         */
        if (af.signatureCoversWholeDocument(name)) {

            PdfPKCS7 pk = af.verifySignature(name);
            Calendar cal = pk.getSignDate();
            Certificate pkc[] = pk.getCertificates();

            PdfDictionary signatureDictionary = af.getSignatureDictionary(name);
            String revisionName = Integer.toString(af.getRevision(name));
            if (!alreadyLoadedRevisions.contains(revisionName)) {
                callback.validate(reader, outerCatalog, pk.getSigningCertificate(),
                        cal != null ? cal.getTime() : null, pkc, signatureDictionary, pk);
                alreadyLoadedRevisions.add(revisionName);
            }

        } else {

            PdfDictionary catalog = reader.getCatalog();

            /*
             * We open the version of the document that was protected by the signature
             */
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            InputStream ip = af.extractRevision(name);
            IOUtils.copy(ip, out);
            out.close();
            ip.close();

            /*
             * You can sign a PDF document with only one signature. So when we want multiple signature, signatures
             * are appended sequentially to the end of the document. The recursive call help to get the signature
             * from the original document.
             */
            validateSignatures(new ByteArrayInputStream(out.toByteArray()), catalog, callback,
                    alreadyLoadedRevisions);

        }
    }

}

From source file:org.opensignature.opensignpdf.tools.Pkcs7Extractor.java

License:Open Source License

/**
 * @param args/*from   ww  w .  j  a va2  s. co m*/
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    try {
        if (args.length < 1) {
            System.out.println("Usage: EstraiPkcs7 <pdf file relative to current dir>");
            System.exit(1);
        }
        String filename = args[0];

        PdfReader reader = new PdfReader(filename);
        AcroFields af = reader.getAcroFields();
        ArrayList names = af.getSignatureNames();
        for (int k = 0; k < names.size(); ++k) {
            String name = (String) names.get(k);
            System.out.println("Signature name: " + name);
            System.out.println("Signature covers whole document: " + af.signatureCoversWholeDocument(name));
            System.out.println("Document revision: " + af.getRevision(name) + " of " + af.getTotalRevisions());
            // Start revision extraction
            // FileOutputStream out = new FileOutputStream("revision_" +
            // af.getRevision(name) + ".pdf");
            // byte bb[] = new byte[8192];
            // InputStream ip = af.extractRevision(name);
            // int n = 0;
            // while ((n = ip.read(bb)) > 0)
            // out.write(bb, 0, n);
            // out.close();
            // ip.close();
            // End revision extraction

            // PdfPKCS7 pk = af.verifySignature(name);

            PdfDictionary v = af.getSignatureDictionary(name);

            PdfString contents = (PdfString) PdfReader.getPdfObject(v.get(PdfName.CONTENTS));

            // Start pkcs7 extraction
            FileOutputStream fos = new FileOutputStream(filename + "_signeddata_" + name + ".pk7");
            System.out.println(k + ") Estrazione pkcs7: " + filename + "_signeddata_" + name + ".pk7");
            fos.write(contents.getOriginalBytes());
            fos.flush();
            fos.close();
            // End pkcs7 extraction

            /* Commentato per evitare dipendenze da BC
            Security.insertProviderAt(new BouncyCastleProvider(), 3);
                    
            // nota: dipendenza da provider BC per "SHA1withRSA"
            PdfPKCS7 pk = new PdfPKCS7(contents.getOriginalBytes(), "BC");
                    
                    
                    
            Calendar cal = pk.getSignDate();
            Certificate pkc[] = pk.getCertificates();
            System.out.println("Got " + pkc.length
                + " certificates from pdf");
            System.out
                .println("Subject of signer: "
                        + PdfPKCS7.getSubjectFields(pk
                                .getSigningCertificate()));
            // System.out.println("Document modified: " + !pk.verify());
            // Object fails[] = PdfPKCS7.verifyCertificates(pkc, kall, null,
            // cal);
            // if (fails == null)
            // System.out.println("Certificates verified against the
            // KeyStore");
            // else
            // System.out.println("Certificate failed: " + fails[1]);
                     
            */
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    }

    /* decommentare se si riabilita la parte relativa a PdfPKCS7 nel main
            
    catch (InvalidKeyException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SecurityException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (CRLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (CertificateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (NoSuchProviderException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    */

}