PDF Generation Template Editor
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
After opening the target PDF, use a PKCS#12 certificate to sign a signature field. The field must belong to the current document and have a valid native UUID. A newly constructed TypeScript object cannot be signed directly.
The function accepts a reader whose document has finished loading, a certificate path, a password, and an output path. If no signature field exists, create a form field first, then read it again.
import { CPDFReaderView, CPDFSignatureWidget, CPDFSignaturePermission }
from '@compdfkit_pdf_sdk/react_native';
async function signDocument(reader: CPDFReaderView,
certificatePath: string, password: string, outputPath: string) {
const document = reader._pdfDocument;
const widgets = await document.pageAtIndex(0).getWidgets();
const field = widgets.find(
(item): item is CPDFSignatureWidget =>
item instanceof CPDFSignatureWidget && item.isSigned === false,
);
if (!field || !field.uuid) throw new Error('No unsigned signature field is available');
const success = await document.writeSignature(0, field.uuid, outputPath, {
certificatePath, password, reason: 'Approved', location: 'Office',
permissions: CPDFSignaturePermission.FILL_AND_SIGN,
appearance: { text: 'Signed by Document Signer',
content: 'Approved', drawLogo: false },
});
if (!success) throw new Error('Signing failed');
if (!await document.open(outputPath)) throw new Error('Failed to open the signed file');
}isSigned indicates whether the field already contains a digital signature. It is optional: treat an absent value as unknown, not unsigned. The first two arguments of writeSignature() are the zero-based page index and the uuid of a signature field on that page.
Signing writes a new file. The output path must differ from the source, and its parent directory must exist and be writable. Open the output for viewing and verification after the method returns true. Failures may also reject the Promise.
| Option | Description |
|---|---|
certificatePath, password | PKCS#12 certificate and password |
name, reason, location | Signer name, signing reason, and location |
permissions | Document modifications allowed after signing |
appearance | Signature text, images, colors, and layout |
timestampServerUrl | RFC 3161 timestamp service URL |
CPDFSignaturePermission provides FORBID_CHANGE (no changes), FILL_AND_SIGN (form filling and signing), and ANNOTATION_FILL_AND_SIGN (annotations, form filling, and signing). Configure these separately from document-open passwords and encryption permissions.
appearance supports text, content, textColor, contentColor, logoPath, imagePath, drawLogo, drawOnlyContent, and contentAlignLeft. content accepts a multiline string or an array of { key, value } items. Image options use local file paths.
Continue with Read and Verify Digital Signatures and Timestamps and Long-Term Validation.