Skip to content
DemoFAQ

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub
Flutter Guides Create digital signatures

Create Digital Signatures ​

Starting with 3.2.0, ComPDF Flutter SDK supports certificate-based signing of PDF signature fields. A digital signature provides document integrity and signer information; an image signature only adds a signature image.

Prepare the certificate path, password, and a writable output path, then open the target PDF. In these examples, document is the current CPDFDocument.

dart
import 'package:compdfkit_flutter/compdfkit.dart';
import 'package:compdfkit_flutter/annotation/form/cpdf_signature_widget.dart';

Get an Unsigned Signature Field ​

dart
final widgets = await document.pageAtIndex(0).getWidgets();
final unsignedFields = widgets
    .whereType<CPDFSignatureWidget>()
    .where((field) => !field.isSigned)
    .toList();

isSigned indicates whether the field already contains a digital signature. If the document has no signature field, create a form field first and read it again.

The field passed to the signing API must belong to the current document and have a valid native uuid. Use a field returned by getWidgets() or a reader callback. A newly constructed Dart object alone cannot be used directly for signing.

Sign and Write a Copy ​

dart
if (unsignedFields.isNotEmpty) {
  final success = await document.writeSignature(
    unsignedFields.first,
    '/path/to/signed.pdf',
    CPDFDigitalSignatureConfig(
      certificatePath: certificatePath,
      password: certificatePassword,
      reason: 'Approved',
      location: 'Office',
      permissions: CPDFSignaturePermission.fillAndSign,
      appearance: const CPDFDigitalSignatureAppearance(
        text: 'Signed by Document Signer',
        content: 'Approved',
        drawLogo: false,
      ),
    ),
  );
  if (success) {
    await document.open('/path/to/signed.pdf');
  }
}

The signature is written to a new file. Use an output path different from the source, with an existing parent directory. After success, open the output file to view or verify the signature.

Configure Signing ​

SettingPurpose
certificatePath, passwordPKCS#12 certificate and password
name, reason, locationSigner name, signing reason, and location
permissionsDocument changes allowed by the signature
appearanceSignature text, colors, images, and layout
timestampServerUrlSignature timestamp service; see Timestamps and Long-Term Validation

CPDFSignaturePermission defines the changes allowed after signing:

ValueDescription
forbidChangeDisallow changes to the document
fillAndSignAllow form filling and signing
annotationFillAndSignAllow annotation, form filling, and signing

permissions defaults to fillAndSign. It describes modification permissions associated with the signature, separately from document opening passwords and encryption permissions.

Configure the appearance with text, content, textColor, contentColor, imagePath, logoPath, drawLogo, drawOnlyContent, and contentAlignLeft. Image settings use local file paths.

Invalid arguments may throw ArgumentError. Native signing failures may return false or throw a platform exception. Open the output only after confirming success. See Read and Verify Digital Signatures.