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:

swift
var document = CPDFDocument(url: URL(string: "filePath"))
let page = document?.page(at: 0)

let textWidget = CPDFTextWidgetAnnotation(document: document)
textWidget?.setFieldName("TextField")
textWidget?.isMultiline = false
textWidget?.stringValue = "Basic Text Field"
textWidget?.fontColor = UIColor.black
textWidget?.font = UIFont.systemFont(ofSize: 15)
page?.addAnnotation(textWidget!)
objective-c
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:[NSURL fileURLWithPath:@"filePath"]];
CPDFPage *page = [document pageAtIndex:0];

CPDFTextWidgetAnnotation *textWidget = [[CPDFTextWidgetAnnotation alloc] initWithDocument:document];
textWidget.fieldName = @"TextField";
textWidget.isMultiline = NO;
textWidget.stringValue = @"Basic Text Field";
textWidget.fontColor = [UIColor blackColor];
textWidget.font = [UIFont systemFontOfSize:15];
[page addAnnotation:textWidget];

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:

swift
var document = CPDFDocument(url: URL(string: "filePath"))
let page = document?.page(at: 0)

//Create button to jump to the second page.
let pushButton1 = CPDFButtonWidgetAnnotation(document: document, controlType: .pushButtonControl)
pushButton1?.bounds = CGRect(x: 267, y: 300, width: 130, height: 80)
pushButton1?.setFieldName("PushButton1")
pushButton1?.setCaption("PushButton")
pushButton1?.fontColor = UIColor.black
pushButton1?.font = UIFont.systemFont(ofSize: 15)

let destination1 = CPDFDestination(document: document, pageIndex: 1)
let goToAction1 = CPDFGoToAction(destination: destination1)
pushButton1?.setAction(goToAction1)

page?.addAnnotation(pushButton1!)

//Create button to jump to the web page.
let pushButton2 = CPDFButtonWidgetAnnotation(document: document, controlType: .pushButtonControl)
pushButton2?.bounds = CGRect(x: 367, y: 303, width: 150, height: 80)
pushButton2?.setFieldName("PushButton2")
pushButton2?.setCaption("PushButton")
pushButton2?.fontColor = UIColor.black
pushButton2?.font = UIFont.systemFont(ofSize: 15)

let urlAction = CPDFURLAction(url: "https://www.compdf.com/")
pushButton2?.setAction(urlAction)

page?.addAnnotation(pushButton2)
objective-c
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:[NSURL fileURLWithPath:@"filePath"]];
CPDFPage *page = [document pageAtIndex:0];

//Create button to jump to the second page.
CPDFButtonWidgetAnnotation *pushButton1 = [[CPDFButtonWidgetAnnotation alloc] initWithDocument:document controlType:CPDFWidgetPushButtonControl];
pushButton1.bounds = CGRectMake(267, 300, 130, 80);
pushButton1.fieldName = @"PushButton1";
pushButton1.caption = @"PushButton";
pushButton1.fontColor = [UIColor blackColor];
pushButton1.font = [UIFont systemFontOfSize:15];

CPDFDestination * destination = [[CPDFDestination alloc] initWithDocument:document pageIndex:1];
CPDFGoToAction * goToAction = [[CPDFGoToAction alloc] initWithDestination:destination];
[pushButton1 setAction:goToAction];

[page addAnnotation:pushButton1];

//Create button to jump to the web page.
CPDFButtonWidgetAnnotation *pushButton2 = [[CPDFButtonWidgetAnnotation alloc] initWithDocument:document controlType:CPDFWidgetPushButtonControl];
pushButton2.bounds = CGRectMake(367, 303, 150, 80);
pushButton2.fieldName = @"PushButton2";
pushButton2.caption = @"PushButton";
pushButton2.fontColor = [UIColor blackColor];
pushButton2.font = [UIFont systemFontOfSize:15];

CPDFURLAction *urlAction = [[CPDFURLAction alloc] initWithURL:@"https://www.compdf.com/"];
[pushButton2 setAction:urlAction];

[page addAnnotation:pushButton2];

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:

swift
var document = CPDFDocument(url: URL(string: "filePath"))
let page = document?.page(at: 0)

var items = [CPDFChoiceWidgetItem]()
let item1 = CPDFChoiceWidgetItem()
item1.value = "List Box No.1"
item1.string = "List Box No.1"
items.append(item1)

let item2 = CPDFChoiceWidgetItem()
item2.value = "List Box No.2"
item2.string = "List Box No.2"
items.append(item2)

let item3 = CPDFChoiceWidgetItem()
item3.value = "List Box No.3"
item3.string = "List Box No.3"
items.append(item3)

let choiceWidget = CPDFChoiceWidgetAnnotation(document: document, listChoice: true)
choiceWidget?.setFieldName("ListBox1")
choiceWidget?.bounds = CGRect(x: 267, y: 100, width: 200, height: 100)
choiceWidget?.items = items
choiceWidget?.selectItemAtIndex = 2

page?.addAnnotation(choiceWidget!)
objective-c
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:[NSURL fileURLWithPath:@"filePath"]];
CPDFPage *page = [document pageAtIndex:0];

NSMutableArray *items = [NSMutableArray array];
CPDFChoiceWidgetItem *item1 = [[CPDFChoiceWidgetItem alloc] init];
item1.value = @"List Box No.1";
item1.string = @"List Box No.1";
[items addObject:item1];
CPDFChoiceWidgetItem *item2 = [[CPDFChoiceWidgetItem alloc] init];
item2.value = @"List Box No.2";
item2.string = @"List Box No.2";
[items addObject:item2];
CPDFChoiceWidgetItem *item3 = [[CPDFChoiceWidgetItem alloc] init];
item3.value = @"List Box No.3";
item3.string = @"List Box No.3";
[items addObject:item3];

CPDFChoiceWidgetAnnotation *choiceWidget = [[CPDFChoiceWidgetAnnotation alloc] initWithDocument:document listChoice:YES];
choiceWidget.fieldName = @"ListBox1";
choiceWidget.bounds = CGRectMake(267, 100, 200, 100);
choiceWidget.items = items;
choiceWidget.selectItemAtIndex = 2;
[page addAnnotation:choiceWidget];

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:

swift
var document = CPDFDocument(url: URL(string: "filePath"))
let page = document?.page(at: 0)

let signatureWidget = CPDFSignatureWidgetAnnotation(document: document)
signatureWidget?.bounds = CGRect(x: 28, y: 206, width: 80, height: 101)
signatureWidget?.setFieldName("Signature1")
page?.addAnnotation(signatureWidget)
objective-c
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:[NSURL fileURLWithPath:@"filePath"]];
CPDFPage *page = [document pageAtIndex:0];

PDFSignatureWidgetAnnotation *signatureWidget = [[CPDFSignatureWidgetAnnotation alloc] initWithDocument:document];
signatureWidget.bounds = CGRectMake(28, 206, 80, 101);
signatureWidget.fieldName = @"Signature1";
[page addAnnotation:signatureWidget];

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:

swift
var document = CPDFDocument(url: URL(string: "filePath"))
let page = document?.page(at: 0)

let checkBox = CPDFButtonWidgetAnnotation(document: document, controlType: .checkBoxControl)
checkBox?.bounds = CGRect(x: 167, y: 351, width: 100, height: 90)
checkBox?.setFieldName("CheckBox2")
checkBox?.borderColor = UIColor.black
checkBox?.backgroundColor = UIColor.green
checkBox?.borderWidth = 2.0
checkBox?.setState(1)
checkBox?.font = UIFont.systemFont(ofSize: 15)
page?.addAnnotation(checkBox)
objective-c
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:[NSURL fileURLWithPath:@"filePath"]];
CPDFPage *page = [document pageAtIndex:0];

CPDFButtonWidgetAnnotation *checkBox = [[CPDFButtonWidgetAnnotation alloc] initWithDocument:document controlType:CPDFWidgetCheckBoxControl];
checkBox.bounds = CGRectMake(167, 351, 100, 90);
checkBox.fieldName = @"CheckBox2";
checkBox.borderColor = [UIColor blackColor];
checkBox.backgroundColor = [UIColor greenColor];
checkBox.borderWidth = 2.0;
checkBox.state = 1;
checkBox.font = [UIFont systemFontOfSize:15];
[page addAnnotation:checkBox];

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:

swift
var document = CPDFDocument(url: URL(string: "filePath"))
let page = document?.page(at: 0)

let radioButton = CPDFButtonWidgetAnnotation(document: document, controlType: .radioButtonControl)
radioButton?.bounds = CGRect(x: 167, y: 451, width: 100, height: 90)
radioButton?.setFieldName("RadioButton1")
radioButton?.borderColor = UIColor.black
radioButton?.backgroundColor = UIColor.green
radioButton?.borderWidth = 2.0
radioButton?.setState(0)
radioButton?.font = UIFont.systemFont(ofSize: 15)
page?.addAnnotation(radioButton)
objective-c
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:[NSURL fileURLWithPath:@"filePath"]];
CPDFPage *page = [document pageAtIndex:0];

CPDFButtonWidgetAnnotation *radioButton = [[CPDFButtonWidgetAnnotation alloc] initWithDocument:document controlType:CPDFWidgetRadioButtonControl];
radioButton.bounds = CGRectMake(167, 451, 100, 90);
radioButton.fieldName = @"RadioButton1";
radioButton.borderColor = [UIColor blackColor];
radioButton.backgroundColor = [UIColor greenColor];
radioButton.borderWidth = 2.0;
radioButton.state = 0;
radioButton.font = [UIFont systemFontOfSize:15];
[page addAnnotation:radioButton];

Create Combo Boxes

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:

swift
var document = CPDFDocument(url: URL(string: "filePath"))
let page = document?.page(at: 0)

var items = [CPDFChoiceWidgetItem]()
let item1 = CPDFChoiceWidgetItem()
item1.value = "Combo Box No.1"
item1.string = "Combo Box No.1"
items.append(item1)

let item2 = CPDFChoiceWidgetItem()
item2.value = "Combo Box No.2"
item2.string = "Combo Box No.2"
items.append(item2)

let item3 = CPDFChoiceWidgetItem()
item3.value = "Combo Box No.3"
item3.string = "Combo Box No.3"
items.append(item3)

let choiceWidget = CPDFChoiceWidgetAnnotation(document: document, listChoice: false)
choiceWidget?.setFieldName("ComboBox1")
choiceWidget?.bounds = CGRect(x: 267, y: 100, width: 200, height: 100)
choiceWidget?.items = items
choiceWidget?.selectItemAtIndex = 2

page?.addAnnotation(choiceWidget!)
objective-c
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:[NSURL fileURLWithPath:@"filePath"]];
CPDFPage *page = [document pageAtIndex:0];

NSMutableArray *items = [NSMutableArray array];
CPDFChoiceWidgetItem *item1 = [[CPDFChoiceWidgetItem alloc] init];
item1.value = @"Combo Box No.1";
item1.string = @"Combo Box No.1";
[items addObject:item1];
CPDFChoiceWidgetItem *item2 = [[CPDFChoiceWidgetItem alloc] init];
item2.value = @"Combo Box No.2";
item2.string = @"Combo Box No.2";
[items addObject:item2];
CPDFChoiceWidgetItem *item3 = [[CPDFChoiceWidgetItem alloc] init];
item3.value = @"Combo Box No.3";
item3.string = @"Combo Box No.3";
[items addObject:item3];

CPDFChoiceWidgetAnnotation *choiceWidget = [[CPDFChoiceWidgetAnnotation alloc] initWithDocument:document listChoice:NO];
choiceWidget.fieldName = @"ComboBox1";
choiceWidget.bounds = CGRectMake(267, 100, 200, 100);
choiceWidget.items = items;
choiceWidget.selectItemAtIndex = 2;
[page addAnnotation:choiceWidget];