Skip to content

Import and Export

The methods for importing and exporting XFDF form data allow users to save and restore PDF form data, making it easier to fill out and manage form inputs.

Import Form

Here is the code to import XFDF form data:

Using CPDFReaderWidget:

dart
CPDFReaderWidgetController? _controller;
// Initialize CPDFReaderWidget and get the controller in the onCreated callback
CPDFReaderWidget(
  document: documentPath,
  configuration: CPDFConfiguration(),
  onCreated: (controller) {
    setState(() {
      this._controller = controller;
    });
  },
)

// Import from file path
bool importResult = await _controller.document.importWidgets('data/your_package_name/files/xxx.xfdf');

// Or use a content URI on Android
const xfdfFile = 'content://media/external/file/1000045118';
const importResult = await _controller.document.importWidgets(xfdfFile);

Using CPDFDocument:

dart
// Create and open the document
CPDFDocument document = await CPDFDocument.createInstance();
var error = await document.open(pdfFilePath);
if (error == CPDFDocumentError.success) {
  // Import from file path
  bool importResult = await document.importWidgets('data/your_package_name/files/xxx.xfdf');

  // Or use a content URI on Android
  const xfdfFile = 'content://media/external/file/1000045118';
  const importResult = await document.importWidgets(xfdfFile);
}

Export Form

Here is the code to export form data:

Using CPDFReaderWidget:

dart
CPDFReaderWidgetController? _controller;
// Initialize CPDFReaderWidget and get the controller in the onCreated callback
CPDFReaderWidget(
  document: documentPath,
  configuration: CPDFConfiguration(),
  onCreated: (controller) {
    setState(() {
      this._controller = controller;
    });
  },
)

// Export to file path
const exportPath = await _controller.document.exportWidgets();

Using CPDFDocument:

dart
// Create and open the document
CPDFDocument document = await CPDFDocument.createInstance();
var error = await document.open(pdfFilePath);
if (error == CPDFDocumentError.success) {
  const exportPath = await document.exportWidgets();
}

What is XFDF

XFDF (XML Forms Data Format) is an XML-based format used to describe and transfer PDF form data. It is commonly used alongside PDF files to store and share values, states, and actions of form fields.

An XFDF file contains the data for the corresponding PDF form, including field names, values, options, and formats.

Note: XFDF is a data-only format—it does not contain the actual PDF file itself. It is used solely to represent and transmit form data for integration and interoperability across different systems and applications.