Skip to content

删除注释

删除指定注释

ComPDFKit Flutter SDK支持通过api删除选定的注释,删除注释步骤如下:

  1. 获取文档对象(CPDFReaderWidget 的控制器或 CPDFDocument 实例)。

  2. 获取需要删除注释的页面对象。

  3. 获取该页面的注释列表。

  4. 在注释列表中寻找想要删除的注释。

  5. 删除该注释。

示例代码:

使用CPDFReaderWidget

dart
CPDFReaderWidgetController? _controller;
// 初始化 CPDFReaderWidget,并在 onCreated 回调中获取 controller
CPDFReaderWidget(
  document: documentPath,
  configuration: CPDFConfiguration(),
  onCreated: (controller) {
    setState(() {
      this._controller = controller;
    });
  },
)
// 删除第一页第一个注释
CPDFPage page = _controller.document.pageAtIndex(0);
var pageAnnotations = await page.getAnnotations();
await page.removeAnnotation(pageAnnotations[0]);

使用CPDFDocument

dart
// 创建并打开文档
CPDFDocument document = await CPDFDocument.createInstance();
var error = await document.open(pdfFilePath);
if (error == CPDFDocumentError.success) {
  // 删除第一页第一个注释
  CPDFPage page = document.pageAtIndex(0);
  var pageAnnotations = await page.getAnnotations();
  await page.removeAnnotation(pageAnnotations[0]);
}

删除所有注释

您也可以通过调用 removeAllAnnotations() 方法删除当前文档中的所有注释。

  • 返回值为boolean,表示操作是否成功。

示例代码:

使用CPDFReaderWidget:

dart
CPDFReaderWidgetController? _controller;
// 初始化 CPDFReaderWidget,并在 onCreated 回调中获取 controller
CPDFReaderWidget(
  document: documentPath,
  configuration: CPDFConfiguration(),
  onCreated: (controller) {
    setState(() {
      this._controller = controller;
    });
  },
)
// 删除所有注释
bool deleteResult = await _controller.document.removeAllAnnotations();

使用CPDFDocument:

dart
// 创建并打开文档
CPDFDocument document = await CPDFDocument.createInstance();
var error = await document.open(pdfFilePath);
if (error == CPDFDocumentError.success) {
  // 删除所有注释
	bool deleteResult = await document.removeAllAnnotations();
}

注意:此方法不会删除超链接注释。