Skip to content

创建表单域

创建文本域

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

创建文本域的步骤如下:

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

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

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

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

java
CPDFDocument document = new CPDFDocument(context);
document.open(pdfPath);
int pageNumber = 0;
CPDFPage page = document.pageAtIndex(pageNumber);
RectF pageSize = document.getPageSize(pageNumber);
RectF singleLineTextRect = new RectF(28, 32, 237, 75);
singleLineTextRect = page.convertRectToPage(false, pageSize.width(), pageSize.height(), singleLineTextRect);
//Insert a single-line TextField.
CPDFTextWidget textField = (CPDFTextWidget) page.addFormWidget(CPDFWidget.WidgetType.Widget_TextField);
textField.setRect(singleLineTextRect);
textField.setFieldName("TextField1");
textField.setText("Basic Text Field");
textField.setFontColor(Color.BLACK);
textField.setFontSize(15);
textField.setFillColor(Color.RED);
textField.setBorderColor(Color.YELLOW);
textField.setBorderWidth(5F);
textField.updateAp();
kotlin
val document = CPDFDocument(context)
document.open(pdfPath)
val pageNumber = 0
val page = document.pageAtIndex(pageNumber)
val pageSize = document.getPageSize(pageNumber)
val singleLineTextRect = page.convertRectToPage(
  false,
  pageSize.width(),
  pageSize.height(),
  RectF(28F, 32F, 237F, 75F)
)
//Insert a single-line TextField.
(page.addFormWidget(WidgetType.Widget_TextField) as CPDFTextWidget).apply {
  rect = singleLineTextRect
  fieldName = "TextField1"
  text = "Basic Text Field"
  fontColor = Color.BLACK
  fontSize = 15f
  fillColor = Color.RED
  borderColor = Color.YELLOW
  borderWidth = 5F
  updateAp()
}

创建按钮

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

创建按钮的步骤如下:

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

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

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

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

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

  6. 更新按钮外观。

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

java
CPDFDocument document = CPDFDocument(context);
document.open(pdfPath);
RectF pageSize = document.getPageSize(0);
CPDFPage cpdfPage = document.pageAtIndex(0);

//创建按钮用于跳转到第二页
RectF pushButton1Rect = new RectF(267F, 203F, 401F, 235F);
pushButton1Rect = cpdfPage.convertRectToPage(false, pageSize.width(), pageSize.height(), pushButton1Rect);
CPDFPushbuttonWidget pushButtonWidget1 = (CPDFPushbuttonWidget) cpdfPage.addFormWidget(CPDFWidget.WidgetType.Widget_PushButton);
pushButtonWidget1.setRect(pushButton1Rect);
pushButtonWidget1.setFieldName("PushButton1");
pushButtonWidget1.setFontColor(Color.BLACK);
pushButtonWidget1.setFontSize(15);
pushButtonWidget1.setButtonTitle("PushButton");
CPDFGoToAction goToAction = new CPDFGoToAction();
CPDFDestination destination = new CPDFDestination(1, 0, 842, 0F);
goToAction.setDestination(document, destination);
pushButtonWidget1.setButtonAction(goToAction);
pushButtonWidget1.updateAp();

//创建按钮用于跳转到网页
RectF pushButton2Rect = new RectF(433F, 203F, 567F, 235F);
pushButton2Rect = cpdfPage.convertRectToPage(false, pageSize.width(), pageSize.height(), pushButton2Rect);
CPDFPushbuttonWidget pushButtonWidget2 = (CPDFPushbuttonWidget) cpdfPage.addFormWidget(CPDFWidget.WidgetType.Widget_PushButton);
pushButtonWidget2.setRect(pushButton2Rect);
pushButtonWidget2.setFieldName("PushButton2");
pushButtonWidget2.setFontColor(Color.BLACK);
pushButtonWidget2.setFontSize(15);
pushButtonWidget2.setButtonTitle("PushButton");
CPDFUriAction uriAction = new CPDFUriAction();
uriAction.setUri("https://www.compdf.com/");
pushButtonWidget2.setButtonAction(uriAction);
pushButtonWidget2.updateAp();
kotlin
val document = CPDFDocument(context)
document.open(pdfPath)
val pageSize = document.getPageSize(0)
val cpdfPage = document.pageAtIndex(0)

//创建按钮用于跳转到第二页
val pushButtonWidget1 = cpdfPage.addFormWidget(CPDFWidget.WidgetType.Widget_PushButton) as CPDFPushbuttonWidget
pushButtonWidget1.apply {
  rect = cpdfPage.convertRectToPage(false, pageSize.width(), pageSize.height(), RectF(267F, 203F, 401F, 235F))
  fieldName = "PushButton1"
  fontColor = Color.BLACK
  fontSize = 15F
  buttonTitle = "PushButton"
  buttonAction = CPDFGoToAction().apply {
    setDestination(document, CPDFDestination(1, 0F, 842F, 0F))
  }
  updateAp()
}


//创建按钮用于跳转到网页
val pushButtonWidget2 = cpdfPage.addFormWidget(CPDFWidget.WidgetType.Widget_PushButton) as CPDFPushbuttonWidget
pushButtonWidget2.apply {
  rect = cpdfPage.convertRectToPage(false, pageSize.width(), pageSize.height(), RectF(433F, 203F, 567F, 235F))
  fieldName = "PushButton2"
  fontColor = Color.BLACK
  fontSize = 15F
  buttonTitle = "PushButton"
  buttonAction = CPDFUriAction().apply {
    uri = "https://www.compdf.com/"
  }
  updateAp()
}

创建列表框

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

创建列表框的步骤如下:

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

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

java
CPDFDocument document = new CPDFDocument(context);
document.open(pdfPath);
CPDFPage cpdfPage = document.pageAtIndex(0);
RectF pageSize = document.getPageSize(0);

RectF listBoxRect = new RectF(267F, 32F, 567F, 138F);
listBoxRect = cpdfPage.convertRectToPage(false, pageSize.width(), pageSize.height(), listBoxRect);
CPDFListboxWidget listBoxWidget = (CPDFListboxWidget) cpdfPage.addFormWidget(CPDFWidget.WidgetType.Widget_ListBox);
listBoxWidget.setRect(listBoxRect);
listBoxWidget.setFieldName("ListBox1");
listBoxWidget.setFillColor(Color.WHITE);
listBoxWidget.setBorderColor(Color.BLACK);
listBoxWidget.setBorderWidth(2);
CPDFWidgetItem[] listBoxItems = new CPDFWidgetItem[]{
  new CPDFWidgetItem("List Box No.1", "List Box No.1"),
  new CPDFWidgetItem("List Box No.2", "List Box No.2"),
  new CPDFWidgetItem("List Box No.3", "List Box No.3"),
};
listBoxWidget.setOptionItems(listBoxItems);
listBoxWidget.setSelectedIndexes(new int[]{1});
listBoxWidget.updateAp();
kotlin
val document = new CPDFDocument(context)
document.open(pdfPath)
val cpdfPage = document.pageAtIndex(0)
val pageSize = document.getPageSize(0)

val listBoxWidget = cpdfPage.addFormWidget(WidgetType.Widget_ListBox) as CPDFListboxWidget
listBoxWidget.apply {
  rect = cpdfPage.convertRectToPage(false, pageSize.width(), pageSize.height(), RectF(267F, 32F, 567F, 138F))
  fieldName = "ListBox1"
  fillColor = Color.WHITE
  borderColor = Color.BLACK
  borderWidth = 2F
  val listBoxItems = arrayOf(
    CPDFWidgetItem("List Box No.1", "List Box No.1"),
    CPDFWidgetItem("List Box No.2", "List Box No.2"),
    CPDFWidgetItem("List Box No.3", "List Box No.3"))
  setOptionItems(listBoxItems)
  selectedIndexes = intArrayOf(1)
  updateAp()
}

创建签名域

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

创建签名域的步骤如下:

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

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

  3. 设置签名域位置。

  4. 设置签名域属性。

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

java
CPDFDocument document = new CPDFDocument(context);
document.open(pdfPath);
CPDFPage cpdfPage = document.pageAtIndex(0);
RectF pageSize = document.getPageSize(0);

RectF signatureRect = new RectF(28, 206, 237, 301);
signatureRect = cpdfPage.convertRectToPage(false, pageSize.width(), pageSize.height(), signatureRect);
CPDFSignatureWidget signatureWidget = (CPDFSignatureWidget) cpdfPage.addFormWidget(CPDFWidget.WidgetType.Widget_SignatureFields);
signatureWidget.setFieldName("Signature1");
signatureWidget.setFillColor(Color.WHITE);
signatureWidget.setBorderColor(Color.BLACK);
signatureWidget.setBorderWidth(2F);
signatureWidget.setRect(signatureRect);
signatureWidget.updateAp();
kotlin
val document = new CPDFDocument(context)
document.open(pdfPath)
val cpdfPage = document.pageAtIndex(0)
val pageSize = document.getPageSize(0)

val signatureWidget = cpdfPage.addFormWidget(WidgetType.Widget_SignatureFields) as CPDFSignatureWidget
signatureWidget.apply {
  rect = cpdfPage.convertRectToPage(false, pageSize.width(), pageSize.height(), RectF(28F, 206F, 237F, 301F))
  fieldName = "Signature1"
  fillColor = Color.WHITE
  borderColor = Color.BLACK
  borderWidth = 2F
  updateAp()
}

创建复选框

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

创建复选框的步骤如下:

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

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

  3. 设置复选框位置。

  4. 设置复选框属性。

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

java
CPDFDocument document = new CPDFDocument(context);
document.open(pdfPath);
CPDFPage cpdfPage = document.pageAtIndex(0);
RectF pageSize = document.getPageSize(0);

RectF checkBoxRect = new RectF(267, 251, 299, 283);
checkBoxRect = cpdfPage.convertRectToPage(false, pageSize.width(), pageSize.height(), checkBoxRect);
CPDFCheckboxWidget checkboxWidget = (CPDFCheckboxWidget) cpdfPage.addFormWidget(CPDFWidget.WidgetType.Widget_CheckBox);
checkboxWidget.setRect(checkBox1);
checkboxWidget.setFieldName("CheckBox1");
checkboxWidget.setFillColor(Color.parseColor("#CCE5E5FF"));
checkboxWidget.setBorderColor(Color.BLACK);
checkboxWidget.setBorderWidth(2F);
checkboxWidget.setChecked(false);
checkboxWidget.updateAp();
kotlin
val document = new CPDFDocument(context)
document.open(pdfPath)
val cpdfPage = document.pageAtIndex(0)
val pageSize = document.getPageSize(0)

val checkboxWidget = cpdfPage.addFormWidget(WidgetType.Widget_CheckBox) as CPDFCheckboxWidget
checkboxWidget.apply {
  rect = cpdfPage.convertRectToPage(false, pageSize.width(), pageSize.height(), RectF(267F, 251F, 299F, 283F))
  fieldName = "CheckBox1"
  fillColor = Color.parseColor("#CCE5E5FF")
  borderColor = Color.BLACK
  borderWidth = 2F
  isChecked = false
  updateAp()
}

创建单选框

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

创建单选框的步骤如下:

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

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

  3. 设置单选框位置。

  4. 设置单选框属性。

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

java
CPDFDocument document = new CPDFDocument(context);
document.open(pdfPath);
CPDFPage cpdfPage = document.pageAtIndex(0);
RectF pageSize = document.getPageSize(0);

RectF radioButtonRect = new RectF(385F, 251F, 424F, 290F);
radioButtonRect = cpdfPage.convertRectToPage(false, pageSize.width(), pageSize.height(), radioButtonRect);
CPDFRadiobuttonWidget radiobuttonWidget = (CPDFRadiobuttonWidget) cpdfPage.addFormWidget(CPDFWidget.WidgetType.Widget_RadioButton);
radiobuttonWidget.setRect(radioButtonRect);
radiobuttonWidget.setFieldName("RadioButton");
radiobuttonWidget.setCheckStyle(CPDFWidget.CheckStyle.CK_Circle);
radiobuttonWidget.setFillColor(Color.parseColor("#CCE5E5FF"));
radiobuttonWidget.setBorderColor(Color.BLACK);
radiobuttonWidget.setBorderWidth(2F);
radiobuttonWidget.setChecked(false);
radiobuttonWidget.setFieldName("RadioButton1");
radiobuttonWidget.updateAp();
kotlin
val document = new CPDFDocument(context)
document.open(pdfPath)
val cpdfPage = document.pageAtIndex(0)
val pageSize = document.getPageSize(0)

val radiobuttonWidget = cpdfPage.addFormWidget(WidgetType.Widget_RadioButton) as CPDFRadiobuttonWidget
radiobuttonWidget.apply { 
  rect = cpdfPage.convertRectToPage(false, pageSize.width(), pageSize.height(), RectF(385F, 251F, 424F, 290F))
  fieldName = "RadioButton"
  checkStyle = CPDFWidget.CheckStyle.CK_Circle
  fillColor = Color.parseColor("#CCE5E5FF")
  borderColor = Color.BLACK
  borderWidth = 2F
  isChecked = false
  fieldName = "RadioButton1"
  updateAp()
}

创建下拉菜单

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

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

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

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

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

  4. 设置下拉菜单属性。

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

java
CPDFDocument document = new CPDFDocument(context);
document.open(pdfPath);
CPDFPage cpdfPage = document.pageAtIndex(0);
RectF pageSize = document.getPageSize(0);

RectF comboBoxRect = new RectF(267F, 143F, 567F, 189F);
comboBoxRect = cpdfPage.convertRectToPage(false, pageSize.width(), pageSize.height(), comboBoxRect);
CPDFComboboxWidget comboBoxWidget = (CPDFComboboxWidget) cpdfPage.addFormWidget(CPDFWidget.WidgetType.Widget_ComboBox);
comboBoxWidget.setRect(comboBoxRect);
comboBoxWidget.setFieldName("ComboBox1");
CPDFWidgetItem[] comboBoxItems = new CPDFWidgetItem[]{
  new CPDFWidgetItem("Combo Box No.1", "Combo Box No.1"),
  new CPDFWidgetItem("Combo Box No.2", "Combo Box No.2"),
  new CPDFWidgetItem("Combo Box No.3", "Combo Box No.3"),
};
comboBoxWidget.setOptionItems(comboBoxItems, new int[]{1});
comboBoxWidget.updateAp();
kotlin
val document = new CPDFDocument(context)
document.open(pdfPath)
val cpdfPage = document.pageAtIndex(0)
val pageSize = document.getPageSize(0)

val comboBoxWidget = cpdfPage.addFormWidget(WidgetType.Widget_ComboBox) as CPDFComboboxWidget
comboBoxWidget.apply {
  rect = cpdfPage.convertRectToPage(false, pageSize.width(), pageSize.height(), RectF(267F, 143F, 567F, 189F))
  fieldName = "ComboBox1"
  val comboBoxItems = arrayOf(
    CPDFWidgetItem("Combo Box No.1", "Combo Box No.1"),
    CPDFWidgetItem("Combo Box No.2", "Combo Box No.2"),
    CPDFWidgetItem("Combo Box No.3", "Combo Box No.3"))
  setOptionItems(comboBoxItems, intArrayOf(1))
  updateAp()
}