How to sign PDF document

Description

I would like to sign newly created PDF document.

Solution

The DigitalSignature method is used to add a digital signature to a PDF document. Prior to digitally signing a document, the users or developers should have on their systems a digital certificate that can be used to sign documents.

Syntax

.DigitalSignature "Name of the signer", "Reason", "ImageFile", "Location", -1,0 ,0, 0 ,0 , 1 + 4 + 32

Parameters

SignerName This is the friendly name of the digital signature as it appears to the user when the digital certificate is installed on the system. In most cases, this is the full name of the person signing the document.

Reason Reason for signing the document. Can be an empty string if no reason is specified.

ImageFile Full path of the file containing the image that is associated with the signature. This is optional, a digital signature does not always contain an image.

Location Physical location of the person who signed the document. This parameter is optional.

PageNumber Page number on which to insert the signature. Page numbers start with 1, the value -1 indicates the last page in the document.

HorzPos, VertPos Horizontal and vertical position of the digital signature in Twips.

Width, Height Width and height of the digital signature in Twips.

Flags Combination of flags that determine how the digial signature appears on the page. A value of 0 indicates that the signature is invisible.

Return Value

This method returns 0 upon success, it returns one of the following exceptions upon failure:

E_NOTIMPL The license key that is provided does not enable digital signatures

E_ACCESSDENIED The document security settings do not allow the user to modify the document

E_INVALIDARG One of the arguments is invalid or the certificate does not allow signing documents

E_FAIL The signature already exists

Remarks

The Flags parameter can be a combination of one of the following values:

Signer name 1 Reason for signing 2 Location 4 Associated image 8 Date of signing 32 Signature type 64

Code Example

Sub Click(Source As Button)

Dim FilePathName As String Dim PDFApp As Variant Dim PDFDoc As Variant FilePathPDF = "C:\Temp\test.pdf" Set PDFDoc = CreateObject("cdintfex.document") With PDFDoc .Open FilePathPDF .SetLicenseKey SW_PDF_PRINTER_USER_NAME, SW_PDF_PRINTER_LICENSE_KEY .DigitalSignature "Name of the signer", "Reason", "ImageFile", "Location", -1,0 ,0, 0 ,0 , 1 + 4 + 32 .Save "C:\Temp\signed_PDF.pdf" End With End Sub

Notice: Signer should have certificate on his/her name in the Windows digital ID repository. This name should be set as the first parameter in the .DigitalSignature method.