Package com.itextpdf.text.pdf

Examples of com.itextpdf.text.pdf.AcroFields


        // Open the document for reading.
        PdfReader reader = new PdfReader(documentFileName);

        // Get the signatures in the document.
        AcroFields fields = reader.getAcroFields();
        ArrayList<String> signatures = fields.getSignatureNames();
        Map<String, PdfPKCS7> result = new LinkedHashMap<String, PdfPKCS7>();

        // Check if the input document is signed.
        if (signatures.size() == 0) {
            throw new NoSignatureException(messages.getString(
View Full Code Here


        NoOCSPResponseException,
        NoCRLException,
        GeneralSecurityException
    {
        // Get all the information about the signature.
        AcroFields fields = reader.getAcroFields();
        PdfPKCS7 pkcs7 = fields.verifySignature(signature);

        // Use the signature to check the document integrity.
        if (!pkcs7.verify()) {
            throw new InvalidSignatureException(
                String.format(messages.getString(
View Full Code Here

        // Open the document for reading.
        PdfReader reader = new PdfReader(documentFileName);

        // Get the signatures in the document.
        AcroFields fields = reader.getAcroFields();
        ArrayList<String> signatures = fields.getSignatureNames();
        Map<String, PdfPKCS7> result = new LinkedHashMap<String, PdfPKCS7>();

        // Get signature data encoded in PKCS #7 format for each signature.
        for (String signature : signatures) {
            // Verify the signature.
            PdfPKCS7 pkcs7 = fields.verifySignature(signature);

            // Check if the signature is a document level timestamp.
            if (pkcs7.isTsp() && !includeTimestamps) {
                // Skip document level timestamps.
                continue;
View Full Code Here

        // Open input file for reading.
        PdfReader reader = new PdfReader(inputFileName);

        // Get the list of signatures in the document.
        AcroFields fields = reader.getAcroFields();
        ArrayList<String> signatures = fields.getSignatureNames();
        if (signatures.size() == 0) {
            throw new NoSignatureException(messages.getString(
                "Document_has_no_signature."));
        }

        // Check if the last signature is a timestamp.
        String lastSignature = signatures.get(signatures.size() - 1);
        PdfPKCS7 pkcs7 = fields.verifySignature(lastSignature);
        if (pkcs7.isTsp()) {
            // Prepare to store certificates, OCSP responses and CRLs only for the last timestamp.
            signatures.clear();
            signatures.add(lastSignature);
        }

        // Prepare structures to store all certificates and CRLs.
        ArrayList<X509Certificate> allCertificates = new ArrayList<X509Certificate>();
        ArrayList<String> allCRLDistributionPoints = new ArrayList<String>();
        HashMap<String, ArrayList<X509Certificate>> certMap = new HashMap<String, ArrayList<X509Certificate>>();
        HashMap<String, ArrayList<X509CRL>> crlMap = new HashMap<String, ArrayList<X509CRL>>();
        HashMap<String, ArrayList<BasicOCSPResp>> ocspMap = new HashMap<String, ArrayList<BasicOCSPResp>>();

        // Store certificates, OCSP responses and CRLs for each signature.
        for (String signature : signatures) {
            // Get signature data.
            pkcs7 = fields.verifySignature(signature);

            // Get certificate path.
            X509Certificate signingCertificate = (X509Certificate) factory.generateCertificate(
                new ByteArrayInputStream(pkcs7.getSigningCertificate().getEncoded()));
            Certificate[] chain = pkcs7.getSignCertificateChain();
View Full Code Here

        // Open the document for reading.
        PdfReader reader = new PdfReader(documentFileName);

        // Get the signatures in the document.
        AcroFields fields = reader.getAcroFields();
        ArrayList<String> signatures = fields.getSignatureNames();
        if (signatures.size() == 0) {
            throw new NoSignatureException(messages.getString(
                "Document_has_no_signature."));
        }

        // Check if the last signature is a timestamp.
        String lastSignature = signatures.get(signatures.size() - 1);
        PdfPKCS7 pkcs7 = fields.verifySignature(lastSignature);
        if (!pkcs7.isTsp()) {
            throw new NoTimestampException(messages.getString(
                "Document_has_no_document_level_timestamp."));
        }

        // Get the expiration date of the last timestamp.
        Calendar expiration = Calendar.getInstance();
        expiration.setTime(pkcs7.getSigningCertificate().getNotAfter());

        // Validate last timestamp on the current date.
        SignatureValidator.validate(lastSignature, reader, rootCertificates);

        // Get OCSP responses and CRLs from DSS to validate old signatures.
        PdfDictionary dictionary = reader.getCatalog().getAsDict(PdfName.DSS);
        if (dictionary == null) {
            throw new NoDSSException(messages.getString(
                "Document_has_no_DSS_(Document_Security_Store)."));
        }
        PdfArray ocspArray = dictionary.getAsArray(PdfName.OCSPS);
        ArrayList<BasicOCSPResp> ocsps = new ArrayList<BasicOCSPResp>();
        if (ocspArray != null) {
            for (int i = 0; i < ocspArray.size(); i++) {
                PRStream stream = (PRStream) ocspArray.getAsStream(i);
                OCSPResp response = new OCSPResp(PdfReader.getStreamBytes(stream));
                BasicOCSPResp basicResponse = (BasicOCSPResp) response.getResponseObject();
                ocsps.add(basicResponse);
            }
        }
        PdfArray crlArray = dictionary.getAsArray(PdfName.CRLS);
        ArrayList<X509CRL> crls = new ArrayList<X509CRL>();
        if (crlArray != null) {
            for (int i = 0; i < crlArray.size(); i++) {
                PRStream stream = (PRStream) crlArray.getAsStream(i);
                X509CRL crl = (X509CRL) factory.generateCRL(new ByteArrayInputStream(PdfReader.getStreamBytes(stream)));
                crls.add(crl);
            }
        }
        if (ocsps.size() == 0 && crls.size() == 0) {
            throw new NoRevocationStatusException(messages.getString(
                "Document_has_neither_OCSP_responses_nor_CRLs_for_offline_check_of_certificate_revocation_status."));
        }

        // Use date from last timestamp to validade next signature.
        Calendar date = pkcs7.getTimeStampDate();

        // Validate others signatures using date of the timestamp.
        for (int i = signatures.size() - 2; i >= 0; i--) {
            // Get next signature.
            pkcs7 = fields.verifySignature(signatures.get(i));

            // Validate next signature.
            SignatureValidator.validate(
                signatures.get(i), reader, rootCertificates, ocsps, crls, date);
View Full Code Here

    try {
      streamIn = new FileInputStream(templateFile);
      streamOut = new FileOutputStream(outputFile);
      PdfReader reader = new PdfReader(streamIn);
      PdfStamper stamp = new PdfStamper(reader, streamOut);
      AcroFields form = stamp.getAcroFields();

      if (fontfolder != null) {
        ArrayList<BaseFont> substitutionFonts = new ArrayList<BaseFont>();
        for (String font : UNICODE_FONTS) {
          substitutionFonts.add(BaseFont.createFont(fontfolder + File.separator + font, BaseFont.IDENTITY_H, BaseFont.EMBEDDED));
        }
        form.setSubstitutionFonts(substitutionFonts);
      }

      writeIndividualPdf(data, filename, form);

      stamp.setFormFlattening(true);
View Full Code Here

     * @throws DocumentException
     * @throws IOException
     * @throws GeneralSecurityException
     */
    public static void signDeferred(PdfReader reader, String fieldName, OutputStream outs, ExternalSignatureContainer externalSignatureContainer) throws DocumentException, IOException, GeneralSecurityException {
        AcroFields af = reader.getAcroFields();
        PdfDictionary v = af.getSignatureDictionary(fieldName);
        if (v == null)
            throw new DocumentException("No field");
        if (!af.signatureCoversWholeDocument(fieldName))
            throw new DocumentException("Not the last signature");
        PdfArray b = v.getAsArray(PdfName.BYTERANGE);
        long[] gaps = b.asLongArray();
        if (b.size() != 4 || gaps[0] != 0)
            throw new DocumentException("Single exclusion space supported");
View Full Code Here

    try {
      KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
      keystore.load(null, null);

      PdfReader reader = new PdfReader(data);
      AcroFields fields = reader.getAcroFields();
      Collection<Signature> signatures = new ArrayList<Signature>();
      if (fields != null) {
        List<String> list = fields.getSignatureNames();
        if ((list != null) && (!list.isEmpty())) {
          for (String str : list) {
            PdfPKCS7 pk = fields.verifySignature(str);

            PdfString string = fields.getSignatureDictionary(str).getAsString(PdfName.CONTENTS);
            byte[] content = string.getBytes();

            X509Certificate certificate = pk.getSigningCertificate();

            byte[] encoded = content;
View Full Code Here

  @Override
  public Signature[] getSignatures(final byte[] data) {
    try {
      PdfReader reader = new PdfReader(data);
      AcroFields fields = reader.getAcroFields();
      Collection<Signature> signatures = new ArrayList<Signature>();
      if (fields != null) {
        List<String> list = fields.getSignatureNames();
        if ((list != null) && (!list.isEmpty())) {
          for (String str : list) {
            PdfPKCS7 pk = fields.verifySignature(str);

            PdfString string = fields.getSignatureDictionary(str).getAsString(PdfName.CONTENTS);
            byte[] content = string.getBytes();

            X509Certificate certificate = pk.getSigningCertificate();
            Certificate[] chain = pk.getSignCertificateChain();
View Full Code Here

  @Override
  public Signature[] getSignatures(final byte[] data) {
    try {
      PdfReader reader = new PdfReader(data);
      AcroFields fields = reader.getAcroFields();
      Collection<Signature> signatures = new ArrayList<Signature>();
      if (fields != null) {
        List<String> list = fields.getSignatureNames();
        if ((list != null) && (!list.isEmpty())) {
          for (String str : list) {
            PdfPKCS7 pk = fields.verifySignature(str);

            PdfString string = fields.getSignatureDictionary(str).getAsString(PdfName.CONTENTS);
            byte[] content = string.getBytes();

            X509Certificate certificate = pk.getSigningCertificate();
            Certificate[] chain = pk.getSignCertificateChain();
View Full Code Here

TOP

Related Classes of com.itextpdf.text.pdf.AcroFields

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.