The following example demonstrates how to certify a PDF document.
It also briefly demonstrate how to add form fields to a PDF document.
| Certify a PDF document |
Copy Code |
|---|---|
public static void Certify() { Console.WriteLine( "=== CERTIFY ===" ); var outputFileName = "Certify.pdf"; var outputPath = SignaturesSample.SignaturesSampleOutputDirectory + outputFileName; // Create a pdf document. using( var pdfoutput = PdfDocument.Create( outputPath ) ) { // Get the first page of the document. var page = pdfoutput.Pages.First(); // Add title. var titleStyle = TextStyle.WithFont( pdfoutput.Fonts.GetStandardFont( StandardFontType.Helvetica ), 15d ); page.AddParagraph( "Certify a document", titleStyle, new ParagraphStyle( ParagraphHorizontalAlignment.Center ) ); // Add a NumberFormField. page.AddText( "Number:", new Point( 50, 130 ) ); var numberField = new NumberFormField( 32.66d, new Rectangle( 125, 122, 120, 22 ) ); numberField.Border.Color = Color.Green; numberField.Border.Width = 1; page.FormFields.Add( numberField ); // Add a CheckBox page.AddText( "CheckBox:", new Point( 50, 190 ) ); var checkBoxField = new CheckBoxFormField( true, new Rectangle( 125, 182, 20, 20 ) ) { TextStyle = TextStyle.WithFontAndBrush( pdfoutput.Fonts.GetStandardFont( StandardFontType.CourierBoldOblique ), 12d, Brushes.Red ) }; page.FormFields.Add( checkBoxField ); // Add a SignatureFormField. page.AddText( "Signature:", new Point( 50, 250 ) ); var signatureField = new SignatureFormField( new Rectangle( 125, 242, 120, 22 ) ) { Border = new Border() { Color = Color.Blue, Width = 2 } }; page.FormFields.Add( signatureField ); // Create a credential from a Pfx file using password:12345678 and a SHA-512 Digest algorithm. var credential = new PfxSigningCredential( SignaturesSampleResourceDirectory + "TestSignature.pfx", "12345678", DigestAlgorithm.Sha512 ); // Create Signature options and set the IsCertified property, to use when signing the document. var signatureOptions = new SignatureOptions() { Date = DateTime.Now, IsCertified = true, PhysicalLocation = "Montreal, Canada", Reason = "Authenticate important document" }; // Certify the document from the signatureField, with the pfx credential and the defined options. // No need to save the document when signing, it is automatically saved. // The document can no longer be modified after certified. pdfoutput.Sign( signatureField, credential, signatureOptions ); Console.WriteLine( $"Info exported to path: {outputFileName}" ); } } | |