Skip to content
DemoFAQ

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub
Flutter Guides Access forms

Access Forms ​

The steps to retrieve the form field list and form field objects are as follows:

  1. Obtain the document object (either from the CPDFReaderWidgetcontroller or a CPDFDocument instance).
  2. Get the page object.
  3. Retrieve the list of form fields from each page.

Here’s the corresponding code:

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;
    });
  },
)
// Retrieve all form fields in the document
List<CPDFWidget> widgets = [];
int pageCount = await _controller.document.getPageCount();
for (int i = 0; i < pageCount; i++) {
  CPDFPage page = _controller.document.pageAtIndex(i);
  var pageWidgets = await page.getWidgets();
  widgets.addAll(pageWidgets);
}

Using CPDFDocument:

dart
// Create and open the document
CPDFDocument document = await CPDFDocument.createInstance();
var error = await document.open(pdfFilePath);
if (error == CPDFDocumentError.success) {
  // Retrieve all form fields in the document
  List<CPDFWidget> widgets = [];
  int pageCount = await document.getPageCount();
  for (int i = 0; i < pageCount; i++) {
    CPDFPage page = document.pageAtIndex(i);
    var pageWidgets = await page.getWidgets();
    widgets.addAll(pageWidgets);
  }
}

Check Required Fields ​

Starting with 3.2.1, the read-only CPDFWidget.isRequired property reports whether the PDF marks a field as required. It returns false if the flag is not set. The property does not validate field values or prevent submission.

Before submitting, retrieve the fields again. This example finds empty required text fields on the first page (index 0):

dart
import 'package:compdfkit_flutter/annotation/form/cpdf_text_widget.dart';

// In the submit handler, document is the open PDF.
final widgets = await document.pageAtIndex(0).getWidgets();
final missing = widgets.whereType<CPDFTextWidget>().where(
  (field) => field.isRequired && field.text.trim().isEmpty,
).toList();

If missing is not empty, ask the user to complete those fields and stop submission. For multi-page forms, check every relevant page. Check boxes, lists, and signature fields according to whether they are checked, have a selection, or are signed, respectively.

Read Signature Field Status ​

Starting with 3.2.0, CPDFSignatureWidget.isSigned indicates whether a field contains a digital signature:

dart
final widgets = await document.pageAtIndex(0).getWidgets();
for (final signature in widgets.whereType<CPDFSignatureWidget>()) {
  print('${signature.title}: ${signature.isSigned}');
}

Import package:compdfkit_flutter/annotation/form/cpdf_signature_widget.dart before using this type. isSigned does not indicate successful verification. See Read and Verify Digital Signatures for integrity and certificate trust checks.