PDF form field items in C# .NET
Combo box and list form fields can contain items. Nutrient .NET SDK (formerly GdPicture.NET) enables you to create, edit, and reorganize these field items.
Adding a form field item
To add an item to a form field, follow the steps below:
- Create a
GdPicturePDFobject. - Load the PDF file with the
LoadFromFilemethod. - Set the origin of the coordinate system with the
SetOriginmethod. This method requires thePDFOriginenumeration. - Set the measurement unit with the
SetMeasurementUnitmethod to specify the form field’s dimensions and position. This method uses thePdfMeasurementUnitenumeration. - Select the PDF page where you want to place the form field with the
SelectPagemethod. - Add either a combo box or a list form field. Save its field ID to a variable.
- Add field items with the
AddFormFieldItemmethod. It uses the following parameters:FieldID— The form field ID where the item is added.Text— The text displayed in the form field item.Value— Optional: The export value used when exporting the form field’s data.
- Optional: Set the default selected item with the
SetFormFieldValuemethod. It uses the following parameters:FieldId— The form field ID.Value— The item’sTextproperty.
- Optional: Set the
CommitOnSelChangeflag totruewith theSetFormFieldItemCommitmethod. Doing so enables applications to perform an action once an item is selected, without having to exit the form field. This method works only for combo boxes and list form fields with theMultiselectparameter set tofalse. - Optional: Use the
SetFormFieldItemEditmethod to enable adding custom items inside the form field. This method is applicable only for a combo box form field.
To add three items to a combo box, use the following code:
using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");// Set the origin of the coordinate system.gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft);// Set the measurement unit to centimeters.gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter);// Select the second PDF page.gdpicturePDF.SelectPage(2);string fontResName = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica);// Add a combo box form field.int fieldID = gdpicturePDF.AddComboFormField(2, 2, 5, 0.5f, "combobox1", fontResName, 12, 0, 0, 200, false);// Add three items to the combobox.gdpicturePDF.AddFormFieldItem(fieldID, "Option 1", "1");gdpicturePDF.AddFormFieldItem(fieldID, "Option 2", "2");gdpicturePDF.AddFormFieldItem(fieldID, "Option 3", "3");gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");Private Sub SurroundingSub() Dim gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Set the origin of the coordinate system. gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft) ' Set the measurement unit to centimeters. gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter) ' Select the second PDF page. gdpicturePDF.SelectPage(2) Dim fontResName As String = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontHelvetica) ' Add a combo box form field. Dim fieldID As Integer = gdpicturePDF.AddComboFormField(2, 2, 5, 0.5F, "combobox1", fontResName, 12, 0, 0, 200, False) ' Add three items to the combobox. gdpicturePDF.AddFormFieldItem(fieldID, "Option 1", "1") gdpicturePDF.AddFormFieldItem(fieldID, "Option 2", "2") gdpicturePDF.AddFormFieldItem(fieldID, "Option 3", "3") gdpicturePDF.SaveToFile("C:\temp\output.pdf")End SubUsed methods
Related topics
Deleting a form field item
To delete a form field item, use the DeleteFormFieldItem method. It requires the following parameters:
FieldId— The form field ID.ItemIdx— The position index of the item.
To delete a form field item, use the following code:
using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");// Get form field count.int fieldCount = gdpicturePDF.GetFormFieldsCount();// Loop through all form fields.for(int i = 0; i < fieldCount; i++){ // Get form field ID. int fieldID = gdpicturePDF.GetFormFieldId(i); // Check if the current form field has the "combobox1" title. if (gdpicturePDF.GetFormFieldTitle(fieldID) == "combobox1") { // Get the form field item count. int itemCount = gdpicturePDF.GetFormFieldItemCount(fieldID); // Delete the last form field item. gdpicturePDF.DeleteFormFieldItem(fieldID, itemCount - 1); }}gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Get form field count. Dim fieldCount As Integer = gdpicturePDF.GetFormFieldsCount() ' Loop through all form fields. For i = 0 To fieldCount - 1 ' Get form field ID. Dim fieldID As Integer = gdpicturePDF.GetFormFieldId(i) ' Check if the current form field has the "combobox1" title. If gdpicturePDF.GetFormFieldTitle(fieldID) Is "combobox1" Then ' Get the form field item count. Dim itemCount As Integer = gdpicturePDF.GetFormFieldItemCount(fieldID) ' Delete the last form field item. gdpicturePDF.DeleteFormFieldItem(fieldID, itemCount - 1) End If Next gdpicturePDF.SaveToFile("C:\temp\output.pdf")End UsingUsed methods
Related topics