Пример #1
0
        static void SignPDF(string in_docpath,
                            string in_approval_field_name,
                            string in_private_key_file_path,
                            string in_keyfile_password,
                            string in_appearance_img_path,
                            string in_outpath)
        {
            Console.Out.WriteLine("================================================================================");
            Console.Out.WriteLine("Signing PDF document");

            // Open an existing PDF
            using (PDFDoc doc = new PDFDoc(in_docpath))
            {
                // Sign the approval signatures.
                Field found_approval_field = doc.GetField(in_approval_field_name);
                DigitalSignatureField found_approval_signature_digsig_field = new DigitalSignatureField(found_approval_field);
                Image           img2 = Image.Create(doc, in_appearance_img_path);
                SignatureWidget found_approval_signature_widget = new SignatureWidget(found_approval_field.GetSDFObj());
                found_approval_signature_widget.CreateSignatureAppearance(img2);

                        #if USE_DOTNET_CRYPTO
                DotNetCryptoSignatureHandler sigHandler   = new DotNetCryptoSignatureHandler(in_private_key_file_path, in_keyfile_password);
                SDF.SignatureHandlerId       sigHandlerId = doc.AddSignatureHandler(sigHandler);
                found_approval_signature_digsig_field.SignOnNextSaveWithCustomHandler(sigHandlerId);
                        #else
                found_approval_signature_digsig_field.SignOnNextSave(in_private_key_file_path, in_keyfile_password);
                        #endif

                doc.Save(in_outpath, SDFDoc.SaveOptions.e_incremental);
            }
            Console.Out.WriteLine("================================================================================");
        }
Пример #2
0
        public void AdhocTest()
        {
            PDFDoc doc = new PDFDoc(GetTestPdf(SamplePdf));

            // Ad-hoc field added for signing the PDF
            var signatureField = doc.FieldCreate("sample-field-name", Field.Type.e_signature, "signer name");

            signatureField.SetValue("Signature Name");

            var digitalSignatureField = new DigitalSignatureField(signatureField);

            // Before the rest of the lines or else it fails due to dictionary being empty
            digitalSignatureField.SignOnNextSave(GetCertificatePath("pdf-signing.pfx"), CertPassword);

            digitalSignatureField.SetReason("reason");
            digitalSignatureField.SetContactInfo("*****@*****.**");
            digitalSignatureField.SetLocation("location");
            digitalSignatureField.SetFieldPermissions(DigitalSignatureField.FieldPermissions.e_include, new string[0]);
            digitalSignatureField.SetDocumentPermissions(DigitalSignatureField.DocumentPermissions
                                                         .e_formfilling_signing_allowed);

            // Save file
            var temporaryFile = Path.GetTempFileName();

            doc.Save(temporaryFile, pdftron.SDF.SDFDoc.SaveOptions.e_incremental);

            // VALIDATE
            var result = new PDFDoc(temporaryFile);

            var verificationOptions = new VerificationOptions(VerificationOptions.SignatureVerificationSecurityLevel
                                                              .e_compatibility_and_archiving);
            // Using filepath/password directly makes it fail on adding trusted cert
            // THIS ONE FAILS:
            // verificationOptions.AddTrustedCertificate(GetCertificatePath("pdf-signing.crt"));
            var x509 = new X509Certificate(GetCertificatePath("pdf-signing.pfx"), CertPassword);

            verificationOptions.AddTrustedCertificate(x509.GetRawCertData());

            DigitalSignatureFieldIterator signatureFieldIterator = result.GetDigitalSignatureFieldIterator();

            for (; signatureFieldIterator.HasNext(); signatureFieldIterator.Next())
            {
                var dsField            = signatureFieldIterator.Current();
                var verificationResult = dsField.Verify(verificationOptions);

                var status    = verificationResult.GetTrustStatus();
                var certCount = dsField.GetCertCount();

                Console.WriteLine($"Verification status {status}");
                Console.WriteLine($"Digest status {verificationResult.GetDigestStatus()}");
                Console.WriteLine($"Digest document status {verificationResult.GetDocumentStatus()}");
                Console.WriteLine($"Verification status {verificationResult.GetVerificationStatus()}");

                Console.WriteLine($"Cert count: {certCount}");
                Console.WriteLine($"Signature Name{dsField.GetSignatureName()}");

                var sigTime = dsField.GetSigningTime();
                Console.WriteLine($"Signing Time: {sigTime.day}/{sigTime.month}/{sigTime.year} {sigTime.hour}:{sigTime.minute}.{sigTime.second}");

                Assert.True(status != VerificationResult.TrustStatus.e_untrusted, "Unexpected status e_untrusted");
                Assert.True(certCount > 0, "DigitalSignatureField should have a certificate");
            }
        }