Skip to content
Guides

Delete Annotations

The ComPDFKit Flutter SDK supports deleting selected annotations through APIs. The steps to delete annotations are as follows:

  1. Obtain the document object (either the controller of CPDFReaderWidget or an instance of CPDFDocument).
  2. Get the page object that contains the annotation to delete.
  3. Retrieve the list of annotations on that page.
  4. Locate the annotation you want to delete from the list.
  5. Delete the annotation.

Example:

Using CPDFReaderWidget:

dart
CPDFReaderWidgetController? _controller;
// Initialize CPDFReaderWidget and obtain the controller in the onCreated callback
CPDFReaderWidget(
  document: documentPath,
  configuration: CPDFConfiguration(),
  onCreated: (controller) {
    setState(() {
      this._controller = controller;
    });
  },
)
// Delete the first annotation on the first page
CPDFPage page = _controller.document.pageAtIndex(0);
var pageAnnotations = await page.getAnnotations();
await page.removeAnnotation(pageAnnotations[0]);

Using CPDFDocument:

dart
// Create and open the document
CPDFDocument document = await CPDFDocument.createInstance();
var error = await document.open(pdfFilePath);
if (error == CPDFDocumentError.success) {
  // Delete the first annotation on the first page
  CPDFPage page = document.pageAtIndex(0);
  var pageAnnotations = await page.getAnnotations();
  await page.removeAnnotation(pageAnnotations[0]);
}

Alternatively, you can delete all annotations in the current document by calling the removeAllAnnotations() method.

  • The return value is a boolean indicating whether the operation was successful.

Example:

Using CPDFReaderWidget:

dart
CPDFReaderWidgetController? _controller;
// Initialize CPDFReaderWidget and obtain the controller in the onCreated callback
CPDFReaderWidget(
  document: documentPath,
  configuration: CPDFConfiguration(),
  onCreated: (controller) {
    setState(() {
      this._controller = controller;
    });
  },
)
// Delete all annotations
bool deleteResult = await _controller.document.removeAllAnnotations();

Using CPDFDocument:

dart
// Create and open the document
CPDFDocument document = await CPDFDocument.createInstance();
var error = await document.open(pdfFilePath);
if (error == CPDFDocumentError.success) {
  // Delete all annotations
  bool deleteResult = await document.removeAllAnnotations();
}

Note: This method does not remove link annotations.