Create a custom signature handler to sign and verify PDF documents

Create a custom signature handler to sign and verify PDF documents

This code sample shows how to create your own signature handler. Full code sample is available on GitHub.

SignatureHandler

A custom signature handler is created by specializing TallComponents.PDF.DigitalSignatures.SignatureHandler and overriding its virtual and abstract methods. This is demonstrated by SampleSignatureHandler class in the attached code sample. The main reponsibilities of this specialized class is to override SignatureHandler.Sign and SignatureHandler.Verify. PDFKit.NET will call these methods internally and make sure to pass the correct byte streams. These byte streams constitute the PDF content that must be signed or verified according to the PDF specification. What must be signed and verified and how this result is stored internally in a PDF document is taken care of by PDFKit.NET and not the responsibility of the signature handler.

Sign PDF

When you sign a PDF document, you actually sign a signature form field. This is a type of form field just like you have text fields, checkbox fields, etc. Programmatically, you sign a signature field by assigning a SignatureHandler instance to the SignatureField.SignatureHandler property. When the PDF document is saved (by calling the Document.Write method), then during writing, PDFKit.NET will invoke the Sign method of the SignatureHandler instance to sign the PDF content and insert the result in the signature field. This is demonstrated in the GenerateDocument method in Program.cs in the attached code sample.

Verify PDF

Reversely, when you verify a signed PDF document, you actually verify a signature field. Programmatically this is done by calling the SignatureField.Verify and passing a SignatureHandler instance. PDFKit.NET will verify the signature field content (that was inserted when it was signed) and verify it against the PDF content. The actually verification of the byte stream is delegated to the Verify method of the SignatureHandler instance. This is demonstrated in the VerifyDocument method in Program.cs the attached code sample.