static void Main(string[] args) { // Initialize PDFNetC PDFNet.Initialize(); bool result = true; //////////////////// TEST 0: /* Create an approval signature field that we can sign after certifying. * (Must be done before calling CertifyOnNextSave/SignOnNextSave/WithCustomHandler.) */ try { using (PDFDoc doc = new PDFDoc(input_path + "tiger.pdf")) { DigitalSignatureField approval_signature_field = doc.CreateDigitalSignatureField("PDFTronApprovalSig"); SignatureWidget widgetAnnotApproval = SignatureWidget.Create(doc, new Rect(300, 300, 500, 200), approval_signature_field); Page page1 = doc.GetPage(1); page1.AnnotPushBack(widgetAnnotApproval); doc.Save(output_path + "tiger_withApprovalField_output.pdf", SDFDoc.SaveOptions.e_remove_unused); } } catch (Exception e) { Console.Error.WriteLine(e); result = false; } //////////////////// TEST 1: certify a PDF. try { CertifyPDF(input_path + "tiger_withApprovalField.pdf", "PDFTronCertificationSig", input_path + "pdftron.pfx", "password", input_path + "pdftron.bmp", output_path + "tiger_withApprovalField_certified_output.pdf"); PrintSignaturesInfo(output_path + "tiger_withApprovalField_certified_output.pdf"); } catch (Exception e) { Console.Error.WriteLine(e); result = false; } //////////////////// TEST 2: sign a PDF with a certification and an unsigned signature field in it. try { SignPDF(input_path + "tiger_withApprovalField_certified.pdf", "PDFTronApprovalSig", input_path + "pdftron.pfx", "password", input_path + "signature.jpg", output_path + "tiger_withApprovalField_certified_approved_output.pdf"); PrintSignaturesInfo(output_path + "tiger_withApprovalField_certified_approved_output.pdf"); } catch (Exception e) { Console.Error.WriteLine(e); result = false; } //////////////////// TEST 3: Clear a certification from a document that is certified and has two approval signatures. try { ClearSignature(input_path + "tiger_withApprovalField_certified_approved.pdf", "PDFTronCertificationSig", output_path + "tiger_withApprovalField_certified_approved_certcleared_output.pdf"); PrintSignaturesInfo(output_path + "tiger_withApprovalField_certified_approved_certcleared_output.pdf"); } catch (Exception e) { Console.Error.WriteLine(e); result = false; } //////////////////// End of tests. //////////////////// if (result) { Console.Out.WriteLine("Tests successful.\n=========="); } else { Console.Out.WriteLine("Tests FAILED!!!\n=========="); } }
static void CertifyPDF(string in_docpath, string in_cert_field_name, string in_private_key_file_path, string in_keyfile_password, string in_appearance_image_path, string in_outpath) { Console.Out.WriteLine("================================================================================"); Console.Out.WriteLine("Certifying PDF document"); // Open an existing PDF using (PDFDoc doc = new PDFDoc(in_docpath)) { Console.Out.WriteLine("PDFDoc has " + (doc.HasSignatures() ? "signatures" : "no signatures")); Page page1 = doc.GetPage(1); // Create a random text field that we can lock using the field permissions feature. TextWidget annot1 = TextWidget.Create(doc, new Rect(50, 550, 350, 600), "asdf_test_field"); page1.AnnotPushBack(annot1); /* Create new signature form field in the PDFDoc. The name argument is optional; * leaving it empty causes it to be auto-generated. However, you may need the name for later. * Acrobat doesn't show digsigfield in side panel if it's without a widget. Using a * Rect with 0 width and 0 height, or setting the NoPrint/Invisible flags makes it invisible. */ DigitalSignatureField certification_sig_field = doc.CreateDigitalSignatureField(in_cert_field_name); SignatureWidget widgetAnnot = SignatureWidget.Create(doc, new Rect(0, 100, 200, 150), certification_sig_field); page1.AnnotPushBack(widgetAnnot); // (OPTIONAL) Add an appearance. // Widget AP from image Image img = Image.Create(doc, in_appearance_image_path); widgetAnnot.CreateSignatureAppearance(img); // End of optional appearance-adding code. // Add permissions. Lock the random text field. Console.Out.WriteLine("Adding document permissions."); certification_sig_field.SetDocumentPermissions(DigitalSignatureField.DocumentPermissions.e_annotating_formfilling_signing_allowed); Console.Out.WriteLine("Adding field permissions."); string[] fields_to_lock = new string[1]; fields_to_lock[0] = "asdf_test_field"; certification_sig_field.SetFieldPermissions(DigitalSignatureField.FieldPermissions.e_include, fields_to_lock); #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.CertifyOnNextSaveWithCustomHandler(sigHandlerId); #else certification_sig_field.CertifyOnNextSave(in_private_key_file_path, in_keyfile_password); #endif ///// (OPTIONAL) Add more information to the signature dictionary. certification_sig_field.SetLocation("Vancouver, BC"); certification_sig_field.SetReason("Document certification."); certification_sig_field.SetContactInfo("www.pdftron.com"); ///// End of optional sig info code. // Save the PDFDoc. Once the method below is called, PDFNetC will also sign the document using the information provided. doc.Save(in_outpath, 0); } Console.Out.WriteLine("================================================================================"); }