You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PDFTron Flutter导出注释异常:仅随机导出部分高亮注释求助

PDFTron Flutter导出高亮注释异常:仅随机导出部分注释

问题概述

使用PDFTron Flutter开发时,尝试导出用户添加的高亮注释,调用var xfdf1 = await PdftronFlutter.exportAnnotations(null);本应导出文档中所有注释,但实际仅随机导出部分内容:

  • Case 1:多段高亮场景下,导出的XFDF仅包含第二段高亮
  • Case 2:多段高亮场景下,仅导出第二段内容
  • Case 3:高亮多个单词时,仅导出第一个单词的高亮

代码问题分析

从提供的代码来看,核心问题出在注释导出时机错误和文档实例冲突:

  1. 导出时机过早:在showViewer方法中,打开文档后立刻调用saveDocument和exportAnnotations,此时用户还未在阅读器中添加任何注释,导出的内容并非用户新增的注释。
  2. 文档实例冲突:同时使用了全局的PdftronFlutter.openDocument和DocumentViewController.openDocument,两者属于不同的文档实例,用户在DocumentView中添加的注释不会被全局方法捕获。
  3. 状态逻辑混乱:_onDocumentViewCreated中切换_showViewer状态并重新打开文档,进一步导致实例和注释状态不一致。

解决方案建议

1. 修正导出触发逻辑

添加用户交互按钮,在用户完成注释编辑后手动触发导出,示例代码:

// 在Viewer的build方法中添加导出按钮
floatingActionButton: FloatingActionButton(
  onPressed: () async {
    // 使用DocumentViewController保存并导出注释
    await _controller.saveDocument();
    String xfdf = await _controller.exportAnnotations(null);
    print("导出的注释XFDF:");
    print(xfdf);
  },
  child: Icon(Icons.download),
),

2. 统一使用DocumentViewController操作文档

避免混用全局方法和控制器方法,所有文档操作(打开、保存、导出注释)都通过DocumentViewController执行:

DocumentViewController? _controller;

void _onDocumentViewCreated(DocumentViewController controller) {
  setState(() {
    _controller = controller;
  });
  // 仅通过控制器打开文档
  controller.openDocument(_document);
}

3. 移除冗余的全局文档操作

删除showViewer中的PdftronFlutter.openDocument调用,避免实例冲突:

void showViewer() async {
  result = await FilePicker.platform.pickFiles(
    type: FileType.custom,
    allowedExtensions: ['pdf'],
  );
  if (result != null && result!.files.single.path != null) {
    _document = result!.files.single.path!;
    // 移除全局openDocument调用,改为在DocumentView创建后通过控制器打开
    setState(() {});
  }
}

4. 升级PDFTron Flutter版本

如果上述调整后问题仍存在,检查当前使用的PDFTron版本,尝试升级到最新稳定版,部分旧版本存在注释导出的兼容性问题。

内容的提问来源于stack exchange,提问作者JOEGREEN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 02:43:24