In This Topic
    Sign & validate a PDF
    In This Topic

    Introduction

    The following example demonstrates how to sign & validate a PDF document. 

    It also briefly demonstrate how to add form fields to a PDF document.

    Sign a PDF document
    Copy Code
    public static void SignAndValidate()
    {
      Console.WriteLine( "=== SIGN AND VALIDATE ===" );
    
      var outputFileName = "Sign.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( "Sign 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 to use when signing the document.
        var signatureOptions = new SignatureOptions()
        {
          Date = DateTime.Now,
          PhysicalLocation = "Montreal, Canada",
          Reason = "Authenticate important document"
        };
    
        // Sign 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.
        // Modifying pdfoutput.Pages[ 0 ] after signing will invalidate the signing.
        pdfoutput.Sign( signatureField, credential, signatureOptions );
    
        Console.WriteLine( $"Info exported to path: {outputFileName}" );
      }
    
      // Reload the document to validate it's signature.
      using( var doc = PdfDocument.Load( outputPath ) )
      {
        // Validate all the signature for the document's SignatureFormFields.
        var result = doc.ValidateSignatures();
        // Make sure the first Signature is valid.
        if( result[ 0 ].CertificateTrusted )
        {
          Debug.Assert( SignatureValidationStatus.ValidAndCoversWholeDocument == result[ 0 ].Status );
        }
        else
        {
          Debug.Assert( SignatureValidationStatus.ValidButUntrusted == result[ 0 ].Status );
        }
      }
    }

    See Also

    Secure a PDF document