Delete Form Fields
The ComPDFKit Flutter SDK supports deleting selected form fields via API. The steps to remove a form field are as follows:
- Obtain the document object (either from the
CPDFReaderWidget
controller or aCPDFDocument
instance). - Get the page object where the form field to be deleted is located.
- Retrieve the list of form fields on that page.
- Locate the form field you want to delete from the list.
- Delete the form field.
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;
});
},
)
// Remove the first form field on the first page
CPDFPage page = _controller.document.pageAtIndex(0);
var pageWidgets = await page.getWidgets();
await page.removeWidget(pageWidgets[0]);
Using CPDFDocument:
dart
// Create and open the document
CPDFDocument document = await CPDFDocument.createInstance();
var error = await document.open(pdfFilePath);
if (error == CPDFDocumentError.success) {
// Remove the first form field on the first page
CPDFPage page = document.pageAtIndex(0);
var pageWidgets = await page.getWidgets();
await page.removeWidget(pageWidgets[0]);
}