Access Forms
The steps to retrieve the form field list and form field objects are as follows:
- Obtain the document object (either from the
CPDFReaderWidget
controller or aCPDFDocument
instance). - Get the page object.
- 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);
}
}