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("================================================================================"); }
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("================================================================================"); }