Skip to content

页面导航

在使用 CPDFReaderWidget 组件展示 PDF 时,可以通过 CPDFReaderWidgetController 执行以下操作:

跳转到指定页码

dart
await _controller.setDisplayPageIndex(pageIndex);

当需要跳转到指定页码并同时突出显示某个内容(例如点击注释时高亮该注释),可以通过 setDisplayPageIndex 的 rectList 参数在页面上绘制矩形框。

下面示例展示了在点击注释后跳转到注释所在页,并用矩形框高亮注释区域:

dart
final pageIndex = 0;
CPDFPage page = controller.document.pageAtIndex(pageIndex);
final pageAnnotations = await page.getAnnotations();
final annotation = pageAnnotations[0];
await controller.setDisplayPageIndex(pageIndex: annotation.page, rectList: [annotation.rect]);

// 取消高亮
await controller.clearDisplayRect();

效果示例:

AndroidiOS
guides_flutter_3.2.6_1guides_flutter_3.2.6_2

获取当前页码

dart
int currentPageIndex = await _controller.getCurrentPageIndex();

您还可以在使用 CPDFReaderWidget 时设置页面监听器,实时获取当前滑动的页码。

dart
CPDFReaderWidgetController? _controller;

CPDFReaderWidget(
  document: widget.documentPath,
  configuration: CPDFConfiguration(),
  onCreated: (controller) {
    setState(() {
      this._controller = controller;
    });
  },
  onPageChanged: (pageIndex){
    debugPrint('当前页码: ${pageIndex}');
  },
)