Skip to content

Delete Form Fields

The ComPDFKit Flutter SDK supports deleting selected form fields via API. The steps to remove a form field are as follows:

  1. Obtain the document object (either from the CPDFReaderWidget controller or a CPDFDocument instance).
  2. Get the page object where the form field to be deleted is located.
  3. Retrieve the list of form fields on that page.
  4. Locate the form field you want to delete from the list.
  5. 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]);
}