Skip to content

创建表单域

创建文本域

文本域允许用户在指定区域输入文本,常用于收集用户信息或填写表格。

创建文本域的步骤如下:

  1. 从 CPDFDocument 上获取到需要创建文本域的页面对象。

  2. 在页面对象上创建文本域。

  3. 设置文本域的位置和其它属性。

以下是创建文本域的示例代码:

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];

创建按钮

按钮允许用户在 PDF 页面上执行操作,如页面跳转和超链接跳转。

创建按钮的步骤如下:

  1. 从 CPDFDocument 中获取页面对象。

  2. 在页面对象上创建按钮。

  3. 设置按钮位置,外观属性。

  4. 创建并设置按钮的文本属性。

  5. 创建并设置按钮的行为。

  6. 更新按钮外观。

以下是创建按钮的示例代码:

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

//创建按钮用于跳转到第二页
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!)

//创建按钮用于跳转到网页
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];

//创建按钮用于跳转到第二页
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];

//创建按钮用于跳转到网页
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];

创建列表框

列表框允许用户从预定义的选项列表中选择一个或多个项目,提供便捷的数据选择功能。

创建列表框的步骤如下:

  1. 从 CPDFDocument 上获取到需要创建列表框的页面对象。
  2. 在页面对象上创建列表框。
  3. 设置列表框位置。
  4. 在列表框中添加列表项。
  5. 设置列表框属性。

以下是创建列表框的示例代码:

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];

创建签名域

签名域允许用户在文档中插入数字签名或电子签名,用于验证文档的真实性和完整性。

创建签名域的步骤如下:

  1. 从 CPDFDocument 上获取需要添加签名域的页面对象。

  2. 在页面对象上创建签名域。

  3. 设置签名域位置。

  4. 设置签名域属性。

以下是创建签名域的示例代码:

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];

创建复选框

复选框允许用户通过勾选或取消勾选来表示某个选项的状态。

创建复选框的步骤如下:

  1. 从 CPDFDocument 中获取需要添加复选框的页面对象。

  2. 在页面对象上创建复选框。

  3. 设置复选框位置。

  4. 设置复选框属性。

以下是创建复选框的示例代码:

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];

创建单选框

单选框允许用户在一组预定义选项中选择唯一的单个选项。

创建单选框的步骤如下:

  1. 从 CPDFDocument 中获取需要添加单选框的页面对象。

  2. 在页面对象上创建单选框。

  3. 设置单选框位置。

  4. 设置单选框属性。

以下是创建单选框的示例代码:

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];

创建下拉菜单

可以下拉的区域,选中下拉的一项将显示在该区域中。

创建下拉菜单的步骤如下:

  1. 从 CPDFDocument 中获取需要添加下拉菜单的页面对象。

  2. 在页面对象中创建下拉菜单。

  3. 在下拉菜单中添加列表项。

  4. 设置下拉菜单属性。

以下是创建下拉菜单的示例代码:

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];