Skip to content
ComPDF
Guides

导入和导出

XFDF 是一种类似 XML 的标准,来自 Adobe XFDF,用于编码注释和表单字段值。它与 Adobe Acrobat 以及其他一些第三方框架兼容。

ComPDF Flutter SDK 支持读取和写入 XFDF 格式来导入和导出注释。以下指南展示了如何使用 XFDF 格式导入和导出注释。

导入注释

您可以通过调用 importAnnotations(xfdfFile)方法将 XFDF 文件导入到文档中。这将允许您将 XFDF 格式的注释数据应用到现有的 PDF 文档。

  • xfdfFile:指定要导入的 XFDF 文件路径。

    在 Android 平台上,xfdfFile 可以是:

    • 文件 URI(例如,content:// 格式)
    • 文件路径(如 /storage/emulated/0/Download/annotations.xfdf)
    • 位于 assets 目录下的文件

示例代码:

使用 CPDFReaderWidget

dart
CPDFReaderWidgetController? _controller;
// 初始化 CPDFReaderWidget,并在 onCreated 回调中获取 controller
CPDFReaderWidget(
  document: documentPath,
  configuration: CPDFConfiguration(),
  onCreated: (controller) {
    setState(() {
      this._controller = controller;
    });
  },
)
// 导入注释
bool result = await _controller.document.importAnnotations(xfdfFile);

使用CPDFDocument:

dart
// 创建并打开文档
CPDFDocument document = await CPDFDocument.createInstance();
var error = await document.open(pdfFilePath);
if (error == CPDFDocumentError.success) {
	// 导入注释
	bool result = await document.importAnnotations(xfdfFile);
}

导出注释

您可以通过调用 exportAnnotations() 方法将当前文档中的注释导出为 XFDF 格式文件。这对于将注释数据保存为文件或与其他系统共享注释数据非常有用。

  • 此方法不需要路径参数。系统会自动生成并返回导出的 XFDF 文件路径。

示例代码:

使用CPDFReaderWidget:

dart
CPDFReaderWidgetController? _controller;
// 初始化 CPDFReaderWidget,并在 onCreated 回调中获取 controller
CPDFReaderWidget(
  document: documentPath,
  configuration: CPDFConfiguration(),
  onCreated: (controller) {
    setState(() {
      this._controller = controller;
    });
  },
)
// 导出注释
String xfdfFilePath = await _controller.document.exportAnnotations();

使用CPDFDocument:

dart
// 创建并打开文档
CPDFDocument document = await CPDFDocument.createInstance();
CPDFDocumentError error = await document.open(pdfFilePath);
if (error == CPDFDocumentError.success) {
	// 导出注释
	String xfdfFilePath = await document.exportAnnotations();
}