Remove Form Fields from PDFs in C# .NET
GdPicture.NET allows you to delete either single form fields or all form fields in a specified PDF document.
Removing a Single Form Field
To remove a single form field, use the RemoveFormField
method. It requires only the field ID you want to delete as its parameter. To get the form field’s ID, refer to the get field ID guide.
To remove all text box form fields, use the following code:
using GdPicturePDF gdpicturePDF = new GdPicturePDF(); gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf"); // Get the form field count. int fieldCount = gdpicturePDF.GetFormFieldsCount(); // Loop through all form fields. for (int i = 0; i < fieldCount; 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(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Get the form field count. Dim fieldCount As Integer = gdpicturePDF.GetFormFieldsCount() ' Loop through all form fields. For i = 0 To fieldCount - 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) Is PdfFormFieldType.PdfFormFieldTypeText Then ' Remove the text box field. gdpicturePDF.RemoveFormField(fieldID) End If Next gdpicturePDF.SaveToFile("C:\temp\output.pdf") End Using
Used Methods
Related Topics
For 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(@"C:\temp\source.pdf"); gdpicturePDF.RemoveFormFields(); gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") gdpicturePDF.RemoveFormFields() gdpicturePDF.SaveToFile("C:\temp\output.pdf") End Using