A PDF document can be signed with a digital signature. Digital signatures allow for the detection and prevention of unauthorized changes to a document, as well as authentication of the signer. Also, digital signatures allow you to define the permissions to changes that are to be permitted, such as adding annotations or filling in and signing forms.
There are three types of signatures in PDF: certification signatures, approval signatures and usage rights signature.
ImageGear makes working with signatures simple. It allows for both signing and verification. After signing and saving a document, it will contain the signature(s) inside the file. After loading a signed PDF document, its signatures may be verified and the result will indicate whether the document was changed after signing and whether the signatures can be authenticated.
Certification and approval signatures are associated with a SignatureField through the field's Signature property. You can assign a signature to this property in order to prepare the actual signing or read it for verification purposes. If the field has not been signed, the Signature property is null.
A signature handler is used to specify the algorithm that is used to encode the signature. ImageGear provides a readily available implementation of the most common type of algorithm: PKCS7SignatureHandler. This implementation uses the PKCS#7 detached method (SubFilter adbe.pkcs7.detached) and takes a digital certificate from file or directly as a parameter. Although this sounds complicated, it is really easy to use (see the sample in Signing a Document below).
Signing a document is subject to the following restrictions:
In order to sign a PDF document, create an instance of an ApprovalSignature and fill out the properties. The most important and required property is the Handler property, which defines the type of algorithm used when signing the document.
When the signature is created, it must be assigned to the SignatureField that is to be signed. The document will not actually be signed until the document is saved. At that moment, the signature's handler will be called to calculate the actual signature data, which then is stored directly into the file to obtain the signed PDF document.
The following example illustrates the functionality of signing a PDF document with an approval signature.
Before verifying signatures, it is necessary to tell ImageGear.NET which certificates are trusted by the user via the ImGearPDF.TrustedCertificates property. Because certificate validity and document integrity are checked, if the certificate used to sign the PDF is not trusted, an error will occur upon verification.
Signatures in a document can be verified quickly by using the ImGearPDFDocument.VerifySignatures method. It will try to verify all signatures present in the document, and will return either SignatureVerificatonResult.OK or the result for the first signature that fails verification.
To verify each signature separately, it it possible to loop through all the signatures listed in ImGearPDFDocument.Signatures. This is illustrated in the example below.
A signature only covers the part of the document that exists at the moment it is signed. Any additions or modifications that are added to the document by incremental saving at a later time are not covered by that signature.
In order to detect any additions to the document after the signature was added, you can use the signature's property CoversWholeDocument.