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 Custom annotation creation workflows

Custom Annotation Creation Workflows ​

Several APIs relate to annotation events but serve different purposes:

GoalAPIEffect on SDK Default Behavior
Observe that an annotation was createdCPDFEvent.annotationsCreatedNo change.
Replace the default Signature, Image, Stamp, Link, or Note creation UIonAnnotationCreationPreparedCallbackAfter the matching automatic UI is disabled, the app must complete or cancel the creation workflow.
Handle taps on existing Note or Link annotationsonInterceptAnnotationActionCallbackAfter interception is enabled, the SDK no longer edits the Note or follows the Link.
Handle taps on selected form field typesonInterceptWidgetActionCallbackAfter interception is enabled, the SDK no longer performs its default action for those field types.

Customize Annotation Creation ​

Annotation TypeConfiguration to Disableannotation ParameterRequired App Action
CPDFAnnotationType.signatureautoShowSignPicker: falsenullSelect an image, then call prepareNextSignature().
CPDFAnnotationType.stampautoShowStampPicker: falsenullSelect a stamp, then call prepareNextStamp().
CPDFAnnotationType.picturesautoShowPicPicker: falsenullSelect an image, then call prepareNextImage().
CPDFAnnotationType.linkautoShowLinkDialog: falseCreated CPDFLinkAnnotationSet action, then call document.updateAnnotation(). Remove the annotation if the user cancels.
CPDFAnnotationType.noteautoShowNoteEditDialog: falseCreated CPDFNoteAnnotationSet content, then call document.updateAnnotation(). Remove the annotation if the user cancels.
dart
final configuration = CPDFConfiguration(
  annotationsConfig: const CPDFAnnotationsConfig(
    autoShowSignPicker: false,
    autoShowStampPicker: false,
    autoShowPicPicker: false,
    autoShowLinkDialog: false,
    autoShowNoteEditDialog: false,
  ),
);

Signatures, stamps, and images have not yet been placed on the page when the callback runs:

dart
await controller.prepareNextSignature(signaturePath);
await controller.prepareNextImage(imagePath);
await controller.prepareNextStamp(
  standardStamp: CPDFStandardStamp.approved,
);

Links and Notes already exist when the callback runs. Changing the Dart object alone does not write the change back to the PDF; update or remove it explicitly:

dart
if (annotation is CPDFNoteAnnotation) {
  annotation.content = 'Updated note content';
  await controller.document.updateAnnotation(annotation);
}

// Remove the object if the user cancels the custom dialog.
if (annotation != null) {
  await controller.document.removeAnnotation(annotation);
}

For a complete Note create, confirm, and cancel workflow, see Create Annotations.

Intercept Actions on Existing Annotations ​

Starting with 3.2.1, use CPDFAnnotationsConfig.interceptAnnotationActions to select which annotation types to handle. Set interceptAllAnnotationActions: true to intercept every supported default action regardless of the list. This example intercepts only Link actions and displays the URL instead of opening it.

Import CPDFLinkAnnotation from package:compdfkit_flutter/annotation/cpdf_link_annotation.dart and CPDFUriAction from package:compdfkit_flutter/document/action/cpdf_uri_action.dart.

dart
CPDFReaderWidget(
  document: documentPath,
  configuration: CPDFConfiguration(
    annotationsConfig: const CPDFAnnotationsConfig(
      interceptAnnotationActions: [CPDFAnnotationType.link],
    ),
  ),
  onCreated: (controller) {},
  onInterceptAnnotationActionCallback: (annotation) {
    if (annotation is! CPDFLinkAnnotation) return;
    final action = annotation.action;
    if (action is CPDFUriAction) {
      showDialog<void>(
        context: context,
        builder: (context) => AlertDialog(content: Text(action.uri)),
      );
    }
  },
)

Use the host widget's context. Handle CPDFGoToAction in the same callback for links to a page in the document. The SDK does not fall back to the default action if your callback leaves an intercepted action unhandled.

Only Note and Link default actions are currently supported. Other annotations retain their normal selection and editing behavior. Test Note interception in annotation mode: Notes have no default edit action in viewer mode. The SDK migrates the legacy interceptNoteAction and interceptLinkAction switches only when neither new field is set. An explicit false or an empty list counts as a setting. When using a native version that supports only the legacy switches, omit both new fields.

Intercept Form-Field Actions ​

List only the fields you want to handle in CPDFFormsConfig.interceptFormWidgetActions. Here, editText is your app's input dialog; it returns null if the user cancels. Import CPDFTextWidget from package:compdfkit_flutter/annotation/form/cpdf_text_widget.dart.

dart
CPDFReaderWidget(
  document: documentPath,
  configuration: CPDFConfiguration(
    formsConfig: const CPDFFormsConfig(
      interceptFormWidgetActions: [CPDFFormType.textField],
    ),
  ),
  onCreated: (readerController) {
    controller = readerController;
  },
  onInterceptWidgetActionCallback: (widget) async {
    if (widget is! CPDFTextWidget) return;
    final value = await editText(widget.text);
    if (value == null) return;
    widget.text = value;
    if (!await controller.document.updateWidget(widget)) {
      throw StateError('Failed to update the text field');
    }
  },
)

updateWidget() updates the current document. Save the PDF to persist the change. Set interceptAllFormWidgetActions: true only if your app handles every supported default form action. If you only need to observe taps, leave interception disabled.

Use with a Creation Policy ​

CPDFCreationConfig controls whether an interactive tool stays active and whether the new object is selected. It does not replace the preparation callback or completion and cancellation operations on this page. Custom creation must still finish normally when continuous creation is disabled. See Annotation and Form Creation Policy.