Skip to content
Guides

Create Form Fields

Create Text Fields

Text fields allow users to input text in a designated area, commonly used for collecting user information or filling out forms.

The steps to create a text field are as follows:

  1. Obtain the page object from CPDFDocument where the text field needs to be created.

  2. Create a text field on the page object.

  3. Set the position and other properties of the text field.

This example shows how to create a text field:

C#
CPDFDocument document = CPDFDocument.InitWithFilePath("File Path");
CPDFPage page = document.PageAtIndex(0);
CPDFTextWidget textField = page.CreateWidget(C_WIDGET_TYPE.WIDGET_TEXTFIELD) as CPDFTextWidget;
textField.SetRect(new CRect( 28, 32, 235, 75));
textField.SetWidgetBorderRGBColor(new byte[] { 0, 0, 0 });
textField.SetWidgetBgRGBColor(new byte[] {240,255,240});

Create Buttons

Buttons allow users to perform actions on PDF pages, such as page navigation and hyperlink jumps.

The steps to create a button are as follows:

  1. Obtain the page object from CPDFDocument.

  2. Create a button on the page object.

  3. Set the button's position and appearance properties.

  4. Create and set the text properties of the button.

  5. Create and set the actions of the button.

  6. Update the appearance of the button.

This example shows how to create a button:

C#
CPDFDocument document = CPDFDocument.InitWithFilePath("File Path");
CPDFPage page = document.PageAtIndex(0);

// Create button to jump to the second page.
CPDFPushButtonWidget pushButton1 = page.CreateWidget(C_WIDGET_TYPE.WIDGET_PUSHBUTTON) as CPDFPushButtonWidget;
pushButton1.SetRect(new CRect(28, 150, 150, 100));
pushButton1.SetWidgetBorderRGBColor(new byte[] { 0, 0, 0 });
pushButton1.SetWidgetBgRGBColor(new byte[] { 180, 180, 220 });
pushButton1.SetButtonTitle("Go To Page 2");
CTextAttribute attribute = new CTextAttribute();
attribute.FontColor = new byte[] { 0, 0, 0 };
attribute.FontSize = 14;
attribute.FontName = "Helvetica";
pushButton1.SetTextAttribute(attribute);
CPDFGoToAction gotoAction = new CPDFGoToAction();
CPDFDestination dest = new CPDFDestination();
dest.PageIndex = 1;
gotoAction.SetDestination(document, dest);
pushButton1.SetButtonAction(gotoAction);
pushButton1.UpdateFormAp();

// Create button to jump to the web page.
CPDFPushButtonWidget pushButton2 = page.CreateWidget(C_WIDGET_TYPE.WIDGET_PUSHBUTTON) as CPDFPushButtonWidget;
pushButton2.SetRect(new CRect(168, 150, 290, 100));
pushButton2.SetWidgetBorderRGBColor(new byte[] { 0, 0, 0 });
pushButton2.SetWidgetBgRGBColor(new byte[] { 180, 180, 220 });
pushButton2.SetButtonTitle("Go To ComPDFKit");
CTextAttribute attribute2 = new CTextAttribute();
attribute2.FontColor = new byte[] { 0, 0, 0 };
attribute2.FontSize = 14;
attribute2.FontName = "Helvetica";
pushButton2.SetTextAttribute(attribute);

CPDFUriAction uriAction = new CPDFUriAction();
uriAction.SetUri("https://www.compdf.com/");

pushButton2.SetButtonAction(uriAction);
pushButton2.UpdateFormAp();

Create List Boxes

List boxes enable users to choose one or multiple items from a predefined list, providing a convenient data selection feature.

The steps to create a list box are as follows:

  1. Obtain the page object from CPDFDocument where the list box needs to be created.
  2. Create a list box on the page object.
  3. Set the position of the list box.
  4. Add list items to the list box.
  5. Set the properties of the list box.

This example shows how to create a list box:

C#
CPDFDocument document = CPDFDocument.InitWithFilePath("File Path");
CPDFPage page = document.PageAtIndex(0);
CPDFListBoxWidget listbox = page.CreateWidget(C_WIDGET_TYPE.WIDGET_LISTBOX) as CPDFListBoxWidget; 
listbox.SetRect(new CRect(28, 330, 150, 230));
listbox.AddOptionItem(0, "1", "ComPDFKit1");
listbox.AddOptionItem(1, "2", "ComPDFKit2");
listbox.SetWidgetBorderRGBColor(new byte[] { 0, 0, 0 });
listbox.SetWidgetBgRGBColor(new byte[] { 200, 180, 180 });

Create Signature Fields

Signature fields allow users to insert digital or electronic signatures into a document, verifying the authenticity and integrity of the document.

The steps to create a signature field are as follows:

  1. Obtain the page object from CPDFDocument where the signature field needs to be added.

  2. Create a signature field on the page object.

  3. Set the position of the signature field.

  4. Set the properties of the signature field.

This example shows how to create a signature field:

C#
CPDFPage page = document.PageAtIndex(0);
CPDFSignatureWidget signatureField = page.CreateWidget(C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS) as CPDFSignatureWidget;
signatureField.SetRect(new CRect(28, 420, 150, 370));

signatureField.SetWidgetBorderRGBColor(new byte[] { 0, 0, 0 });
signatureField.SetWidgetBgRGBColor(new byte[] { 150, 180, 210 });

Create Check Boxes

Checkboxes allow users to indicate the state of an option by checking or unchecking it.

The steps to create a checkbox are as follows:

  1. Obtain the page object from CPDFDocument where the checkbox needs to be added.

  2. Create a checkbox on the page object.

  3. Set the position of the checkbox.

  4. Set the properties of the checkbox.

This example shows how to create a checkbox:

C#
CPDFDocument document = CPDFDocument.InitWithFilePath("File Path");
CPDFPage page = document.PageAtIndex(0);
CPDFCheckBoxWidget checkBox = page.CreateWidget(C_WIDGET_TYPE.WIDGET_CHECKBOX) as CPDFCheckBoxWidget;
checkBox.SetRect(new CRect(28, 470, 48, 450));
checkBox.SetWidgetBorderRGBColor(new byte[] { 0, 0, 0 });
checkBox.SetWidgetBgRGBColor(new byte[] { 150, 180, 210 });

Create Radio Buttons

Radio buttons allow users to select a unique option from a predefined group of options.

The steps to create a radio button are as follows:

  1. Obtain the page object from CPDFDocument where the radio button needs to be added.

  2. Create a radio button on the page object.

  3. Set the position of the radio button.

  4. Set the properties of the radio button.

This example shows how to create a radio button:

C#
CPDFDocument document = CPDFDocument.InitWithFilePath("File Path");
CPDFPage page = document.PageAtIndex(0);
CPDFRadioButtonWidget radioButton = page.CreateWidget(C_WIDGET_TYPE.WIDGET_RADIOBUTTON) as CPDFRadioButtonWidget;
radioButton.SetRect(new CRect(28, 500, 48, 480));
radioButton.SetWidgetBorderRGBColor(new byte[] { 0, 0, 0 });
radioButton.SetWidgetBgRGBColor(new byte[] { 210, 180, 150 });
radioButton.SetWidgetCheckStyle(C_CHECK_STYLE.CK_CIRCLE);

Create ComboBoxes

A combo box is an area where a selected item from the dropdown will be displayed.

The steps to create a combo box are as follows:

  1. Obtain the page object from CPDFDocument where the combo box needs to be added.

  2. Create a combo box on the page object.

  3. Add list items to the combo box.

  4. Set the properties of the combo box.

This example shows how to create a combo box:

C#
CPDFDocument document = CPDFDocument.InitWithFilePath("File Path");
CPDFPage page = document.PageAtIndex(0);
CPDFComboBoxWidget comboBox = page.CreateWidget(C_WIDGET_TYPE.WIDGET_COMBOBOX) as CPDFComboBoxWidget; 
comboBox.SetRect(new CRect(28, 330, 150, 230));
comboBox.AddOptionItem(0, "1", "ComPDFKit1");
comboBox.AddOptionItem(1, "2", "ComPDFKit2");
comboBox.SetWidgetBorderRGBColor(new byte[] { 0, 0, 0 });
comboBox.SetWidgetBgRGBColor(new byte[] { 200, 180, 180 });