Example usage for com.lowagie.text.pdf PdfDictionary getAsString

List of usage examples for com.lowagie.text.pdf PdfDictionary getAsString

Introduction

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

Prototype

public PdfString getAsString(PdfName key) 

Source Link

Document

Returns a PdfObject as a PdfString, resolving indirect references.

Usage

From source file:androidGLUESigner.pdf.PDFSigExtractor.java

License:Open Source License

/**
 * extracts the signature field for previewing.
 * @throws IOException /*from w ww. ja v  a  2  s . c  o m*/
 */
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:es.gob.afirma.signers.pades.ltv.PdfDocumentSecurityStore.java

License:Open Source License

/** Constructor para inicializar la clase con una estructura DSS ya creada.
 * @param dss Diccionario DSS/* ww w. j  a v a 2  s.  com*/
 * @throws IOException En caso de errores de entrada / salida */
PdfDocumentSecurityStore(final PdfDictionary dss) throws IOException {
    int i = 0;
    PdfArray arrayCerts = dss.getAsArray(new PdfName(PDF_NAME_CERTS));
    if (arrayCerts != null) {
        for (final Iterator<PdfObject> iterator = arrayCerts.listIterator(); iterator.hasNext();) {
            final PdfIndirectReference reference = (PdfIndirectReference) iterator.next();
            this.certificates.put(Integer.valueOf(i),
                    getContentBytesFromContentObject(PdfReader.getPdfObject(reference)));
            i++;
        }

    }
    PdfArray arrayOcsps = dss.getAsArray(new PdfName(PDF_NAME_OCSPS));
    if (arrayOcsps != null) {
        i = 0;
        for (final Iterator<PdfObject> iterator = arrayOcsps.listIterator(); iterator.hasNext();) {
            final PdfIndirectReference reference = (PdfIndirectReference) iterator.next();
            this.ocsps.put(Integer.valueOf(i),
                    getContentBytesFromContentObject(PdfReader.getPdfObject(reference)));
            i++;
        }

    }
    PdfArray arrayCrls = dss.getAsArray(new PdfName(PDF_NAME_CRLS));
    if (arrayCrls != null) {
        i = 0;
        for (final Iterator<PdfObject> iterator = arrayCrls.listIterator(); iterator.hasNext();) {
            final PdfIndirectReference reference = (PdfIndirectReference) iterator.next();
            this.crls.put(Integer.valueOf(i),
                    getContentBytesFromContentObject(PdfReader.getPdfObject(reference)));
            i++;
        }

    }
    final PdfDictionary vri = dss.getAsDict(new PdfName(PDF_NAME_VRI));
    if (vri != null) {
        PdfName key;
        ValidationInformation val;
        for (final Iterator<PdfName> iterator = vri.getKeys().iterator(); iterator.hasNext(); this.signatures
                .put(key.toString().substring(1), val)) {
            key = iterator.next();
            final PdfDictionary vriEntry = vri.getAsDict(key);
            arrayCerts = vriEntry.getAsArray(new PdfName(PDF_NAME_CERT));
            int certId[];
            if (arrayCerts != null) {
                certId = new int[arrayCerts.size()];
                for (i = 0; i < arrayCerts.size(); i++) {
                    final PdfIndirectReference reference = (PdfIndirectReference) arrayCerts.getPdfObject(i);
                    final byte referenceBytes[] = getContentBytesFromContentObject(
                            PdfReader.getPdfObject(reference));
                    final Iterator<Integer> iteratorKeys = this.certificates.keySet().iterator();
                    do {
                        if (!iteratorKeys.hasNext()) {
                            break;
                        }
                        final int index = iteratorKeys.next().intValue();
                        if (Arrays.equals(referenceBytes, this.certificates.get(Integer.valueOf(index)))) {
                            certId[i] = index;
                        }
                    } while (true);
                }

            } else {
                certId = new int[0];
            }
            arrayOcsps = vriEntry.getAsArray(new PdfName(PDF_NAME_OCSP));
            int ocspId[];
            if (arrayOcsps != null) {
                ocspId = new int[arrayOcsps.size()];
                i = 0;
                for (final Iterator<PdfObject> iteratorOcsps = arrayOcsps.listIterator(); iteratorOcsps
                        .hasNext();) {
                    final PdfIndirectReference reference = (PdfIndirectReference) iteratorOcsps.next();
                    final byte referenceBytes[] = getContentBytesFromContentObject(
                            PdfReader.getPdfObject(reference));
                    final Iterator<Integer> iteratorKeys = this.ocsps.keySet().iterator();
                    do {
                        if (!iteratorKeys.hasNext()) {
                            break;
                        }
                        final int index = iteratorKeys.next().intValue();
                        if (Arrays.equals(referenceBytes, this.ocsps.get(Integer.valueOf(index)))) {
                            ocspId[i] = index;
                        }
                    } while (true);
                    i++;
                }

            } else {
                ocspId = new int[0];
            }
            arrayCrls = vriEntry.getAsArray(new PdfName(PDF_NAME_CRL));
            int crlId[];
            if (arrayCrls != null) {
                crlId = new int[arrayCrls.size()];
                i = 0;
                for (final Iterator<PdfObject> iteratorCRLs = arrayCrls.listIterator(); iteratorCRLs
                        .hasNext();) {
                    final PdfIndirectReference reference = (PdfIndirectReference) iteratorCRLs.next();
                    final byte referenceBytes[] = getContentBytesFromContentObject(
                            PdfReader.getPdfObject(reference));
                    final Iterator<Integer> iteratorKeys = this.crls.keySet().iterator();
                    do {
                        if (!iteratorKeys.hasNext()) {
                            break;
                        }
                        final int index = iteratorKeys.next().intValue();
                        if (Arrays.equals(referenceBytes, this.crls.get(Integer.valueOf(index)))) {
                            crlId[i] = index;
                        }
                    } while (true);
                    i++;
                }

            } else {
                crlId = new int[0];
            }
            Calendar date = null;
            if (vriEntry.get(PdfName.TU) != null) {
                if (vriEntry.get(PdfName.TU) instanceof PdfDate) {
                    date = PdfDate.decode(((PdfDate) vriEntry.get(PdfName.TU)).getEncoding());
                }
                if (vriEntry.get(PdfName.TU) instanceof PdfString) {
                    date = PdfDate.decode(vriEntry.getAsString(PdfName.TU).getEncoding());
                }
            }
            val = new ValidationInformation(key, certId, ocspId, crlId, date);
        }
    }
}

From source file:eu.europa.ec.markt.dss.validation.pades.PAdESSignature.java

License:Open Source License

/**
 * /* ww  w  . j a v a2 s  .  c o  m*/
 * The default constructor for PAdESSignature.
 * 
 * @param reader The PdfReader that enable to access the document that contains the PAdES signature.
 */
public PAdESSignature(PdfReader reader, PdfDictionary outerCatalog, PdfDictionary signatureDictionary,
        PdfPKCS7 pk) throws CMSException {
    this.pdfReader = reader;
    this.outerCatalog = outerCatalog;
    this.signatureDictionary = signatureDictionary;
    cadesSignature = new CAdESSignature(signatureDictionary.getAsString(PdfName.CONTENTS).getOriginalBytes());
    this.pk = pk;
}