Пример #1
0
        private SignedDocumentInfo CollectInfo(String documentPath)
        {
            SignedDocumentInfo docInfo  = new SignedDocumentInfo();
            PdfDocument        pdfDoc   = new PdfDocument(new PdfReader(documentPath));
            PdfAcroForm        form     = PdfAcroForm.GetAcroForm(pdfDoc, false);
            SignatureUtil      signUtil = new SignatureUtil(pdfDoc);
            IList <String>     names    = signUtil.GetSignatureNames();

            docInfo.SetNumberOfTotalRevisions(signUtil.GetTotalRevisions());
            SignaturePermissions  perms     = null;
            IList <SignatureInfo> signInfos = new List <SignatureInfo>();

            foreach (String name in names)
            {
                SignatureInfo sigInfo = new SignatureInfo();
                sigInfo.SetSignatureName(name);
                sigInfo.SetRevisionNumber(signUtil.GetRevision(name));
                sigInfo.SetSignatureCoversWholeDocument(signUtil.SignatureCoversWholeDocument(name));
                IList <PdfWidgetAnnotation> widgetAnnotationsList = form.GetField(name).GetWidgets();
                if (widgetAnnotationsList != null && widgetAnnotationsList.Count > 0)
                {
                    sigInfo.SetSignaturePosition(widgetAnnotationsList[0].GetRectangle().ToRectangle());
                }

                PdfPKCS7 pkcs7 = signUtil.ReadSignatureData(name);
                sigInfo.SetDigestAlgorithm(pkcs7.GetHashAlgorithm());
                sigInfo.SetEncryptionAlgorithm(pkcs7.GetEncryptionAlgorithm());
                PdfName filterSubtype = pkcs7.GetFilterSubtype();
                if (filterSubtype != null)
                {
                    sigInfo.SetFilterSubtype(filterSubtype.ToString());
                }

                X509Certificate signCert = pkcs7.GetSigningCertificate();
                sigInfo.SetSignerName(iText.Signatures.CertificateInfo.GetSubjectFields(signCert).GetField("CN"));
                sigInfo.SetAlternativeSignerName(pkcs7.GetSignName());
                sigInfo.SetSignDate(pkcs7.GetSignDate().ToUniversalTime());
                if (TimestampConstants.UNDEFINED_TIMESTAMP_DATE != pkcs7.GetTimeStampDate())
                {
                    sigInfo.SetTimeStamp(pkcs7.GetTimeStampDate().ToUniversalTime());
                    TimeStampToken ts = pkcs7.GetTimeStampToken();
                    sigInfo.SetTimeStampService(ts.TimeStampInfo.Tsa.ToString());
                }

                sigInfo.SetLocation(pkcs7.GetLocation());
                sigInfo.SetReason(pkcs7.GetReason());
                PdfDictionary sigDict     = signUtil.GetSignatureDictionary(name);
                PdfString     contactInfo = sigDict.GetAsString(PdfName.ContactInfo);
                if (contactInfo != null)
                {
                    sigInfo.SetContactInfo(contactInfo.ToString());
                }

                perms = new SignaturePermissions(sigDict, perms);
                sigInfo.SetIsCertifiaction(perms.IsCertification());
                sigInfo.SetIsFieldsFillAllowed(perms.IsFillInAllowed());
                sigInfo.SetIsAddingAnnotationsAllowed(perms.IsAnnotationsAllowed());
                IList <String> fieldLocks = new List <String>();
                foreach (SignaturePermissions.FieldLock Lock in perms.GetFieldLocks())
                {
                    fieldLocks.Add(Lock.ToString());
                }

                sigInfo.SetFieldsLocks(fieldLocks);
                X509Certificate[]       certs     = pkcs7.GetSignCertificateChain();
                IList <CertificateInfo> certInfos = new List <CertificateInfo>();
                for (int i = 0; i < certs.Length; i++)
                {
                    X509Certificate cert     = (X509Certificate)certs[i];
                    CertificateInfo certInfo = new CertificateInfo();
                    certInfo.SetIssuer(cert.IssuerDN);
                    certInfo.SetSubject(cert.SubjectDN);
                    certInfo.SetValidFrom(cert.NotBefore);
                    certInfo.SetValidTo(cert.NotAfter);
                    certInfos.Add(certInfo);
                }

                sigInfo.SetCertificateInfos(certInfos);
                signInfos.Add(sigInfo);
            }

            docInfo.SetSignatureInfos(signInfos);
            return(docInfo);
        }