Skip to main content

How to Add a Digital Signature to a PDF Document

  • 4 minutes to read

This topic describes how to edit document metadata and automatically apply a digital signature.

Load a PDF document

Create a PDF document container and load the document you want to sign. Then, use the Information property to edit the loaded document’s metadata.

var
  ADocument: TdxPDFDocument;
  ACertificate: TdxX509Certificate;
  ADataPath: string;
begin
  ADataPath := TPath.GetDirectoryName(Application.ExeName) + '\Data\';
  ADocument := TdxPDFDocument.Create;  // Creates a PDF document container
  ADocument.LoadFromFile(ADataPath + 'source.pdf');  // Loads a source document
  // Specifies document metadata
  ADocument.Information.Application := 'DevExpress PDF Demo';
  ADocument.Information.Author := 'DevExpress';
  ADocument.Information.Keywords := 'VCL,Digital Signature,DevExpress';
  ADocument.Information.Producer := 'DevExpress VCL Digital Signature Demo';
  //...

Add a Digital Signature

Use the document container’s SignatureOptions property set to configure the digital signature before a save operation.

Load a Signature Certificate

A digital signature verifies the authenticity of digital documents. You need a valid X.509 certificate with public and private keys. Call the dxX509IsUsableForDigitalSignature global function to check if it is possible to use an X.509 certificate to sign documents. Then, call the SignatureOptions.Signature.LoadCertificate procedure to load a required certificate.

Configure a Digital Signature

A valid digital signature should contain information on the signer’s identity, location, and reason to create the signature. Set the SignatureOptions.Enabled property to True to add the configured signature during a subsequent document save operation.

The following code example configures a digital signature, signs the document, and saves it as a new file:

//...
  try
    // Loads an X.509 certificate
    ACertificate := TdxX509Certificate.Create(ADataPath + '123.pfx', '123456');
    // Checks if it is possible to use the loaded X.509 certificate to generate digital signatures
    if not dxX509IsUsableForDigitalSignature(ACertificate) then
    begin
      ACertificate.Free;
      ADocument.Free;
      exit;
    end;
    // The X.509 certificate used to create the digital signature
    ADocument.SignatureOptions.Signature.Certificate := ACertificate;
    // The signer
    ADocument.SignatureOptions.Signature.ContactInfo := 'DevExpress';
    // The signer's location
    ADocument.SignatureOptions.Signature.Location := '505 N. Brand Blvd Suite 1450 Glendale CA 91203 USA';
    // The reason for the digital signature
    ADocument.SignatureOptions.Signature.Reason := 'Approved';
    // Enables the digital signature
    ADocument.SignatureOptions.Enabled := True;
    // Saves the signed document to a different file
    ADocument.SaveToFile(ADataPath +'signed.pdf', True);
  finally
    ADocument.Free;  // Releases the document container to free up reserved memory
    ACertificate.Free; // Releases the loaded certificate
 end;

Add a Visual Signature

A digital signature’s Appearance property stores visual signature settings. Call the Appearance.Image.LoadFromFile procedure to load an image you want to use as a visual signature. Then, use the Appearance.Bounds.PageIndex and Appearance.Bounds.Rect properties to select the target document page and position the signature area. The origin is in the upper-left corner of a document page. The FitMode property value determines how the loaded image occupies the signature area.

The following code example adds an image to the previously configured digital signature:

var
  ASignature: TdxPDFSignatureFieldInfo;
  ADataPath: string;
begin
  ADataPath := TPath.GetDirectoryName(Application.ExeName) + '\Data\';
  ASignature := ADocument.SignatureOptions.Signature;
  // Loads a digital signature image
  ASignature.Appearance.Image.LoadFromFile('/Data/Signature.png');
  // Stretches the image to occupy the target area
  ASignature.Appearance.FitMode := ifmProportionalStretch;
  // Places the signature on the first page
  ASignature.Appearance.Bounds.PageIndex := 0;
  // Defines the signature size and position on the page
  ASignature.Appearance.Bounds.Rect := TdxRectF.Create(200, 550, 500, 750);
  ADocument.SaveToFile(ADataPath +'signed.pdf', True);
end;

Digital Signature Example

Limitations