Import and Export
XFDF is an XML-based standard derived from Adobe XFDF, used for encoding annotations and form field values. It is compatible with Adobe Acrobat and various third-party frameworks.
The ComPDFKit Flutter SDK supports reading and writing annotations in XFDF format, enabling import and export of annotation data. The following guide demonstrates how to use XFDF to import and export annotations.
Importing Annotations
You can import annotations from an XFDF file into a PDF document by calling the importAnnotations(xfdfFile)
method. This allows you to apply XFDF-formatted annotation data to an existing PDF.
xfdfFile: The path of the XFDF file to import.
On Android, xfdfFile can be:
- A file URI (e.g., content:// format)
- A file path (e.g., /storage/emulated/0/Download/annotations.xfdf)
- A file located in the assets directory
Example:
Using CPDFReaderWidget
:
CPDFReaderWidgetController? _controller;
// Initialize CPDFReaderWidget and obtain the controller in the onCreated callback
CPDFReaderWidget(
document: documentPath,
configuration: CPDFConfiguration(),
onCreated: (controller) {
setState(() {
this._controller = controller;
});
},
)
// Import annotations
bool result = await _controller.document.importAnnotations(xfdfFile);
Using CPDFDocument
:
// Create and open the document
CPDFDocument document = await CPDFDocument.createInstance();
var error = await document.open(pdfFilePath);
if (error == CPDFDocumentError.success) {
// Import annotations
bool result = await document.importAnnotations(xfdfFile);
}
Exporting Annotations
To export annotations from a PDF document in XFDF format, use the exportAnnotations() method. This is useful for saving annotation data or sharing it with other systems.
- This method does not require any path parameters. The system will automatically generate and return the file path of the exported XFDF file.
Example:
Using CPDFReaderWidget
:
CPDFReaderWidgetController? _controller;
// Initialize CPDFReaderWidget and obtain the controller in the onCreated callback
CPDFReaderWidget(
document: documentPath,
configuration: CPDFConfiguration(),
onCreated: (controller) {
setState(() {
this._controller = controller;
});
},
)
// Export annotations
String xfdfFilePath = await _controller.document.exportAnnotations();
Using CPDFDocument:
// Create and open the document
CPDFDocument document = await CPDFDocument.createInstance();
CPDFDocumentError error = await document.open(pdfFilePath);
if (error == CPDFDocumentError.success) {
// Export annotations
String xfdfFilePath = await document.exportAnnotations();
}