Remove form fields from PDFs in C# .NET
Nutrient .NET SDK (formerly GdPicture.NET) enables you to delete either single form fields or all form fields in a specified PDF document.
Download sampleRemoving all text form fields
To remove individual form fields, use the RemoveFormField method. It requires only the field ID you want to delete as its parameter. To get a form field’s ID, refer to the get field ID guide.
The following example removes every text form field in the document:
Register the SDK license before running SDK operations. For setup details, refer to the getting started with .NET SDK guide.
using GdPicture14;
LicenseManager license = new LicenseManager();license.RegisterKEY("");{using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"input_forms.pdf");// Get the form field count.int fieldCount = gdpicturePDF.GetFormFieldsCount();// Loop backward through all form fields.for (int i = fieldCount - 1; i >= 0; i--){ // Get the field ID of each form field. int fieldID = gdpicturePDF.GetFormFieldId(i); // Check if the current field is of the text box type. if (gdpicturePDF.GetFormFieldType(fieldID) == PdfFormFieldType.PdfFormFieldTypeText) { // Remove the text box field. gdpicturePDF.RemoveFormField(fieldID); }}gdpicturePDF.SaveToFile(@"output.pdf");}Register the SDK license before running SDK operations. For setup details, refer to the getting started with .NET SDK guide.
Imports GdPicture14
Dim license As LicenseManager = New LicenseManager()license.RegisterKEY("")Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("input_forms.pdf") ' Get the form field count. Dim fieldCount As Integer = gdpicturePDF.GetFormFieldsCount() ' Loop backward through all form fields. For i = fieldCount - 1 To 0 Step -1 ' Get the field ID of each form field. Dim fieldID As Integer = gdpicturePDF.GetFormFieldId(i) ' Check if the current field is of the text box type. If gdpicturePDF.GetFormFieldType(fieldID) = PdfFormFieldType.PdfFormFieldTypeText Then ' Remove the text box field. gdpicturePDF.RemoveFormField(fieldID) End If Next
gdpicturePDF.SaveToFile("output.pdf")End UsingFor detailed information about how to get the form field ID, refer to the get field ID guide.
Removing all form fields
To remove all form fields in a PDF document, use the RemoveFormFields method:
{using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"input_forms.pdf");gdpicturePDF.RemoveFormFields();gdpicturePDF.SaveToFile(@"output.pdf");}Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("input_forms.pdf") gdpicturePDF.RemoveFormFields() gdpicturePDF.SaveToFile("output.pdf")End Using