Pdf Component - PdfSignature

30 Sep 202619 minutes to read

‘PdfSignature’ class represents a digital signature used for signing a PDF document.

// Load the document
let document: PdfDocument = new PdfDocument(data);
// Gets the first page of the document
let page: PdfPage = document.getPage(0);
// Access the PDF form
let form: PdfForm = document.form;
// Create a new signature field
let field: PdfSignatureField = new PdfSignatureField(page, 'Signature', {x: 10, y: 10, width: 100, height: 50});
// Create a new signature using PFX data and private key
const sign: PdfSignature = PdfSignature.create({ cryptographicStandard: CryptographicStandard.cms, digestAlgorithm: DigestAlgorithm.sha256 }, certData, password);
// Sets the signature to the field
field.setSignature(sign);
// Add the field into PDF form
form.add(field);
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();

Properties

Get isValidationAppearanceEnabled boolean

Gets whether the signature has a validation appearance.

// Load an existing signed PDF document.
const document: PdfDocument = new PdfDocument(data);
// Get the signature field.
const signatureField: PdfSignatureField = document.form.fieldAt(0) as PdfSignatureField;
// Get the signature.
const signature: PdfSignature = signatureField.getSignature();
// Check whether the signature contains a validation appearance.
const hasValidationAppearance: boolean = signature.isValidationAppearanceEnabled;
// Destroy the document.
document.destroy();

Set isValidationAppearanceEnabled void

Sets whether the signature has a validation appearance.

// Load the document
let document: PdfDocument = new PdfDocument(data);
// Gets the first page of the document
let page: PdfPage = document.getPage(0);
// Access the PDF form
let form: PdfForm = document.form;
// Create a new signature field
let field: PdfSignatureField = new PdfSignatureField(page, 'Signature', {x: 10, y: 10, width: 100, height: 50});
// Create a new signature using PFX data and private key
const sign: PdfSignature = PdfSignature.create({ cryptographicStandard: CryptographicStandard.cms, digestAlgorithm: DigestAlgorithm.sha256 }, certData, password);
// Enable the validation appearance for the signature.
sign.isValidationAppearanceEnabled = true;
// Sets the signature to the field
field.setSignature(sign);
// Add the field into PDF form
form.add(field);
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();
Parameter Type Description
value boolean Specifies whether the signature should display the validation appearance.

Methods

enableLTV

Enables Long-Term Validation for the signature using a callback.

// Load the document
let document: PdfDocument = new PdfDocument(data);
// Access the first page of the document
let page: PdfPage = document.getPage(0);
// Access the PDF form
let form: PdfForm = document.form;
// Get an existing signature field
let field: PdfSignatureField = form.fieldAt(0) as PdfSignatureField;
//Create new signature
let signature: PdfSignature = PdfSignature.create(certData, password, { cryptographicStandard: CryptographicStandard.cms, digestAlgorithm: DigestAlgorithm.sha256 });
// Set signature to the field
field.setSignature(signature);
// Create an Long-Term Validation callback to fetch responses
async function longTermValidationCallback(url: string, requestBytes?: Uint8Array):
 Promise<{ response: Uint8Array }> {
         //  Implement Long-Term Validation response retrieval here
          return new Uint8Array(); // Placeholder return
}
//  Enable Long-Term Validation using the callback
let ltvEnabled: boolean = await signature.enableLTV(longTermValidationCallback);
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();
Parameter Type Description
ltvCallback LongTermValidationCallback Callback to retrieve OCSP/CRL responses.

Returns Promise

enableLTV

Enables Long-Term Validation using the certificate chain and callback.

// Load the document
const document: PdfDocument = new PdfDocument(data);
// Access the PDF form
const form: PdfForm = document.form;
// Get an existing signature field
const field: PdfSignatureField = form.fieldAt(0) as PdfSignatureField;
// Get the signature
const signature: PdfSignature = field.getSignature();
// Create an LTV callback to fetch OCSP/CRL responses
async function longTermValidationCallback(url: string, requestBytes?: Uint8Array):
 Promise<{ response: Uint8Array }> {
         //  Implement LTV response retrieval here
          return new Uint8Array(); // Placeholder return
}
// Enable LTV using the certificate chain and callback
const ltvEnabled: boolean = await signature.enableLTV(publicCertificates, longTermValidationCallback);
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();
Parameter Type Description
certificates Uint8Array[] Certificate chain for validation.
ltvCallback LongTermValidationCallback Callback to retrieve OCSP/CRL responses.

Returns Promise

enableLTV

Enables Long-Term validation using the public certificates, specified revocation mode and callback.

// Load the document
const document: PdfDocument = new PdfDocument(data);
// Access the PDF form
const form: PdfForm = document.form;
// Get an existing signature field
const field: PdfSignatureField = form.fieldAt(0) as PdfSignatureField;
// Get the signature
const signature: PdfSignature = field.getSignature();
// Create an LTV callback to fetch OCSP/CRL responses
async function longTermValidationCallback(url: string, requestBytes?: Uint8Array):
 Promise<{ response: Uint8Array }> {
         //  Implement LTV response retrieval here
          return new Uint8Array(); // Placeholder return
}
// Enable LTV using public certificates, revocation mode and callback
const ltvEnabled: boolean = await signature.enableLTV(publicCertificates, RevocationType.crl, longTermValidationCallback);
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();
Parameter Type Description
certificates Uint8Array[] Certificate chain for validation.
type RevocationType Revocation mode.
ltvCallback LongTermValidationCallback Callback to retrieve revocation responses.

Returns Promise

enableLTV

Enables Long-Term Validation using the certificate chain, revocation type,
and additional flag to include public certificates inside the document along with callback.

// Load the document
const document: PdfDocument = new PdfDocument(data);
// Gets the first page of the document
const page: PdfPage = document.getPage(0);
// Access the PDF form
const form: PdfForm = document.form;
// Get an existing signature field
const field: PdfSignatureField = form.fieldAt(0) as PdfSignatureField;
// Get the signature
const signature: PdfSignature = field.getSignature();
// Create an LTV callback to fetch OCSP/CRL responses
async function longTermValidationCallback(url: string, requestBytes?: Uint8Array):
  Promise<{ response: Uint8Array }> {
    // Implement LTV response retrieval here
    return { response: new Uint8Array() }; // Placeholder return
}
// Enable LTV and include public certificates
const ltvEnabled: boolean = await signature.enableLTV(
  [publicCertificates],
  RevocationType.crl,
  true,
  longTermValidationCallback
);
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();
Parameter Type Description
certificates Uint8Array[] Certificate chain for validation.
type RevocationType Revocation mode.
includePublicCertificates boolean Indicates whether to embed public certificates in the document.
ltvCallback LongTermValidationCallback Callback to retrieve revocation responses.

Returns Promise

getCertificateInformation

Gets the certificate information associated with the PDF signature.

// Load the document
let document: PdfDocument = new PdfDocument(data);
// Gets the first page of the document
let page: PdfPage = document.getPage(0);
// Access the PDF form
let form: PdfForm = document.form;
// Create a new signature field
let field: PdfSignatureField = new PdfSignatureField(page, 'Signature', {x: 10, y: 10, width: 100, height: 50});
// Create a new signature using PFX data and private key
const sign: PdfSignature = PdfSignature.create({ cryptographicStandard: CryptographicStandard.cms, digestAlgorithm: DigestAlgorithm.sha256 }, certData, password);
// Sets the signature to the field
field.setSignature(sign);
// Gets the certificate information of the signature
const certificateInfo: PdfCertificateInformation = sign.getCertificateInformation();
// Add the field into PDF form
form.add(field);
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();

Returns PdfCertificateInformation

getSignatureOptions

Gets the options for configuring a digital signature in a PDF document.

// Load the document
let document: PdfDocument = new PdfDocument(data);
// Gets the first page of the document
let page: PdfPage = document.getPage(0);
// Access the PDF form
let form: PdfForm = document.form;
// Gets the signature field
let field: PdfSignatureField = form.fieldAt(0) as PdfSignatureField;
// Gets the PDF signature
let signature: PdfSignature = field.getSignature();
// Gets the signature options
let options: PdfSignatureOptions = signature.getSignatureOptions();
// Gets the cryptographic standard of the signature
let cryptographicStandard: CryptographicStandard = options.cryptographicStandard;
// Destroy the document
document.destroy();

Returns PdfSignatureOptions

getSignedDate

Gets the date when the PDF was signed.

// Load the document
let document: PdfDocument = new PdfDocument(data);
// Gets the first page of the document
let page: PdfPage = document.getPage(0);
// Access the PDF form
let form: PdfForm = document.form;
// Create a new signature field
let field: PdfSignatureField = new PdfSignatureField(page, 'Signature', {x: 10, y: 10, width: 100, height: 50});
// Create a new signature using PFX data and private key
const sign: PdfSignature = PdfSignature.create({ cryptographicStandard: CryptographicStandard.cms, digestAlgorithm: DigestAlgorithm.sha256 }, certData, password);
// Sets the signature to the field
field.setSignature(sign);
// Gets the signed date
sign.getSignedDate();
// Add the field into PDF form
form.add(field);
// Save the document
document.save('output.pdf');
// Destroy the document
document.destroy();

Returns Date

create

Creates a new PDF signature using a callback function for external signing.

Parameter Type Description
callBack ExternalSignatureCallback A callback function that computes the signed document hash for external signature.
options PdfSignatureOptions Configuration options for the signature.

Returns PdfSignature

create

Creates a new PDF signature using a callback function for external signing.

Parameter Type Description
callBack ExternalSignatureCallback A callback function that computes the signed document hash for external signature.
publicCertificates Uint8Array[] An array of public certificates.
options PdfSignatureOptions Configuration options for the signature.

Returns PdfSignature

create

Creates a new PDF signature using PFX certificate data and a password.

Parameter Type Description
pfxData Uint8Array | string The PFX certificate data.
password string The password for the certificate.
options PdfSignatureOptions Configuration options for the signature.

Returns PdfSignature

create

Creates a new PDF signature with timestamp using a PFX certificate and timestamp callback.

Parameter Type Description
pfxData Uint8Array | string The PFX certificate data.
password string The password for the certificate.
options PdfSignatureOptions Configuration options for the signature.
timestamp TimestampCallback Callback function that accesses TSA server and returns timestamp response for the request bytes.

Returns PdfSignature

create

Creates a new PDF timestamp signature using the provided signature and timestamp callback.

Parameter Type Description
options PdfSignatureOptions Configuration options for the signature.
timestampCallback TimestampCallback Callback function that accesses TSA server and returns timestamp response for the request bytes.

Returns PdfSignature

replaceEmptySignature

Replaces an empty signature field in a PDF document with externally signed data.

Parameter Type Description
inputPdfData Uint8Array The PDF document data.
signatureName string The name of the signature field to replace.
signedData Uint8Array The externally signed content to embed.
algorithm DigestAlgorithm The digest algorithm used to hash the PDF content.
publicCertificates Uint8Array[] Optional array of public certificate data used for signing.
options (optional) Object Configuration options for signature replacement.

Returns Uint8Array

replaceEmptySignature

Replaces an empty signature field in a PDF document with externally signed data.

Parameter Type Description
inputPdfData Uint8Array The PDF document data.
signatureName string The name of the signature field to replace.
signedData Uint8Array The externally signed content to embed.
algorithm DigestAlgorithm The digest algorithm used to hash the PDF content.
publicCertificates Uint8Array[] Optional array of public certificate data used for signing.
outputPdfName string The name of the output file where the signed PDF will be saved.
options (optional) Object Configuration options for signature replacement.

Returns void