Skip to content
ComPDF
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.

Programming Creation

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:

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()
}

ReaderView Synchronization

If the current PDF is being displayed via CPDFReaderView, and the target page is loaded or currently visible on screen, the annotation needs to be synchronously added to the corresponding CPDFPageView to be immediately displayed in the UI.

Only pages that are displayed or about to be displayed will have a CPDFPageView. Make sure to check for null.

java
CPDFPageView pageView =
        (CPDFPageView) readerView.getChild(pageIndex);

if (pageView != null) {
    boolean isFocused = false;
    pageView.addAnnotation(textField, isFocused);
}
kotlin
val pageView = readerView.getChild(pageIndex) as? CPDFPageView

pageView?.let {
    val isFocused = false
    it.addAnnotation(textField, isFocused)
}

Interactive Creation

Applicable to scenarios where users manually tap the PDF page to add a text field, commonly in reading or editing mode. In this mode, users can draw a rectangle on the screen to create a text field form.

Operation flow:

  1. Switch CPDFReaderView to form mode.
  2. Enable the touch mode for adding text field forms.
  3. Users draw a rectangle on the screen to create the text field form.
  4. Exit the annotation mode.

Example code:

java
// 1. Enter form mode
readerView.setViewMode(CPDFReaderView.ViewMode.FORM);

// 2. Enable touch mode for adding text field forms
readerView.setTouchMode(CPDFReaderView.TouchMode.ADD_ANNOT);
readerView.setCurrentFocusedType(Type.WIDGET);
readerView.setCurrentFocusedFormType(WidgetType.Widget_TextField);

// 3. Exit form creation mode
cPdfReaderView.setTouchMode(CPDFReaderView.TouchMode.BROWSE);
cPdfReaderView.setCurrentFocusedType(Type.UNKNOWN);
cPdfReaderView.setCurrentFocusedFormType(CPDFWidget.WidgetType.Widget_Unknown);

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:

Programming Creation

  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:

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

// Create a button for navigating to the second page.
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();

// Create a button for navigating to a web page.
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)

// Create a button for navigating to the second page.
val pushButtonWidget1 = cpdfPage.addFormWidget(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()
}


// Create a button for navigating to a web page.
val pushButtonWidget2 = cpdfPage.addFormWidget(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()
}

ReaderView Synchronization

If the current PDF is being displayed via CPDFReaderView, and the target page is loaded or currently visible on screen, the button needs to be synchronously added to the corresponding CPDFPageView to be immediately displayed in the UI.

Only pages that are displayed or about to be displayed will have a CPDFPageView. Make sure to check for null.

java
CPDFPageView pageView =
        (CPDFPageView) readerView.getChild(pageIndex);

if (pageView != null) {
    boolean isFocused = false;
    pageView.addAnnotation(pushButtonWidget, isFocused);
}
kotlin
val pageView = readerView.getChild(pageIndex) as? CPDFPageView

pageView?.let {
    val isFocused = false
    it.addAnnotation(pushButtonWidget, isFocused)
}

Interactive Creation

Applicable to scenarios where users manually tap the PDF page to add a button, commonly in form editing mode. In this mode, users can draw a rectangle on the screen to create a button form.

Operation flow:

  1. Switch CPDFReaderView to form mode.
  2. Enable the touch mode for adding button forms.
  3. Users draw a rectangle on the screen to create the button form.
  4. Exit the form creation mode.

Example code:

java
// 1. Enter form mode
readerView.setViewMode(CPDFReaderView.ViewMode.FORM);

// 2. Enable touch mode for adding button forms
readerView.setTouchMode(CPDFReaderView.TouchMode.ADD_ANNOT);
readerView.setCurrentFocusedType(Type.WIDGET);
readerView.setCurrentFocusedFormType(WidgetType.Widget_PushButton);

// 3. Exit form creation mode
cPdfReaderView.setTouchMode(CPDFReaderView.TouchMode.BROWSE);
cPdfReaderView.setCurrentFocusedType(Type.UNKNOWN);
cPdfReaderView.setCurrentFocusedFormType(CPDFWidget.WidgetType.Widget_Unknown);
kotlin
// 1. Enter form mode
readerView.setViewMode(CPDFReaderView.ViewMode.FORM)

// 2. Enable touch mode for adding button forms
readerView.setTouchMode(CPDFReaderView.TouchMode.ADD_ANNOT)
readerView.setCurrentFocusedType(Type.WIDGET)
readerView.setCurrentFocusedFormType(WidgetType.Widget_PushButton)

// 3. Exit form creation mode
cPdfReaderView.setTouchMode(CPDFReaderView.TouchMode.BROWSE)
cPdfReaderView.setCurrentFocusedType(Type.UNKNOWN)
cPdfReaderView.setCurrentFocusedFormType(CPDFWidget.WidgetType.Widget_Unknown)

Create List Boxes

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

Programming Creation

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:

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()
}

ReaderView Synchronization

If the current PDF is being displayed via CPDFReaderView, and the target page is loaded or currently visible on screen, the list box needs to be synchronously added to the corresponding CPDFPageView to be immediately displayed in the UI.

Only pages that are displayed or about to be displayed will have a CPDFPageView. Make sure to check for null.

java
CPDFPageView pageView =
        (CPDFPageView) readerView.getChild(pageIndex);

if (pageView != null) {
    boolean isFocused = false;
    pageView.addAnnotation(listBoxWidget, isFocused);
}
kotlin
val pageView = readerView.getChild(pageIndex) as? CPDFPageView

pageView?.let {
    val isFocused = false
    it.addAnnotation(listBoxWidget, isFocused)
}

Interactive Creation

Applicable to scenarios where users manually tap the PDF page to add a list box, commonly in form editing mode. In this mode, users can draw a rectangle on the screen to create a list box form.

Operation flow:

  1. Switch CPDFReaderView to form mode.
  2. Enable the touch mode for adding list box forms.
  3. Users draw a rectangle on the screen to create the list box form.
  4. Exit the form creation mode.

Example code:

java
// 1. Enter form mode
readerView.setViewMode(CPDFReaderView.ViewMode.FORM);

// 2. Enable touch mode for adding list box forms
readerView.setTouchMode(CPDFReaderView.TouchMode.ADD_ANNOT);
readerView.setCurrentFocusedType(Type.WIDGET);
readerView.setCurrentFocusedFormType(WidgetType.Widget_ListBox);

// 3. Exit form creation mode
cPdfReaderView.setTouchMode(CPDFReaderView.TouchMode.BROWSE);
cPdfReaderView.setCurrentFocusedType(Type.UNKNOWN);
cPdfReaderView.setCurrentFocusedFormType(CPDFWidget.WidgetType.Widget_Unknown);
kotlin
// 1. Enter form mode
readerView.setViewMode(CPDFReaderView.ViewMode.FORM)

// 2. Enable touch mode for adding list box forms
readerView.setTouchMode(CPDFReaderView.TouchMode.ADD_ANNOT)
readerView.setCurrentFocusedType(Type.WIDGET)
readerView.setCurrentFocusedFormType(WidgetType.Widget_ListBox)

// 3. Exit form creation mode
cPdfReaderView.setTouchMode(CPDFReaderView.TouchMode.BROWSE)
cPdfReaderView.setCurrentFocusedType(Type.UNKNOWN)
cPdfReaderView.setCurrentFocusedFormType(CPDFWidget.WidgetType.Widget_Unknown)

Create Signature Fields

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

Programming Creation

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:

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()
}

ReaderView Synchronization

If the current PDF is being displayed via CPDFReaderView, and the target page is loaded or currently visible on screen, the signature field needs to be synchronously added to the corresponding CPDFPageView to be immediately displayed in the UI.

Only pages that are displayed or about to be displayed will have a CPDFPageView. Make sure to check for null.

java
CPDFPageView pageView =
        (CPDFPageView) readerView.getChild(pageIndex);

if (pageView != null) {
    boolean isFocused = false;
    pageView.addAnnotation(signatureWidget, isFocused);
}
kotlin
val pageView = readerView.getChild(pageIndex) as? CPDFPageView

pageView?.let {
    val isFocused = false
    it.addAnnotation(signatureWidget, isFocused)
}

Interactive Creation

Applicable to scenarios where users manually tap the PDF page to add a signature field, commonly in form editing mode. In this mode, users can draw a rectangle on the screen to create a signature field form.

Operation flow:

  1. Switch CPDFReaderView to form mode.
  2. Enable the touch mode for adding signature field forms.
  3. Users draw a rectangle on the screen to create the signature field form.
  4. Exit the form creation mode.

Example code:

java
// 1. Enter form mode
readerView.setViewMode(CPDFReaderView.ViewMode.FORM);

// 2. Enable touch mode for adding signature field forms
readerView.setTouchMode(CPDFReaderView.TouchMode.ADD_ANNOT);
readerView.setCurrentFocusedType(Type.WIDGET);
readerView.setCurrentFocusedFormType(WidgetType.Widget_SignatureFields);

// 3. Exit form creation mode
cPdfReaderView.setTouchMode(CPDFReaderView.TouchMode.BROWSE);
cPdfReaderView.setCurrentFocusedType(Type.UNKNOWN);
cPdfReaderView.setCurrentFocusedFormType(CPDFWidget.WidgetType.Widget_Unknown);
kotlin
// 1. Enter form mode
readerView.setViewMode(CPDFReaderView.ViewMode.FORM)

// 2. Enable touch mode for adding signature field forms
readerView.setTouchMode(CPDFReaderView.TouchMode.ADD_ANNOT)
readerView.setCurrentFocusedType(Type.WIDGET)
readerView.setCurrentFocusedFormType(WidgetType.Widget_SignatureFields)

// 3. Exit form creation mode
cPdfReaderView.setTouchMode(CPDFReaderView.TouchMode.BROWSE)
cPdfReaderView.setCurrentFocusedType(Type.UNKNOWN)
cPdfReaderView.setCurrentFocusedFormType(CPDFWidget.WidgetType.Widget_Unknown)

Create Check Boxes

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

Programming Creation

The steps to create a check box 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:

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(checkBoxRect);
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()
}

ReaderView Synchronization

If the current PDF is being displayed via CPDFReaderView, and the target page is loaded or currently visible on screen, the checkbox needs to be synchronously added to the corresponding CPDFPageView to be immediately displayed in the UI.

Only pages that are displayed or about to be displayed will have a CPDFPageView. Make sure to check for null.

java
CPDFPageView pageView =
        (CPDFPageView) readerView.getChild(pageIndex);

if (pageView != null) {
    boolean isFocused = false;
    pageView.addAnnotation(checkboxWidget, isFocused);
}
kotlin
val pageView = readerView.getChild(pageIndex) as? CPDFPageView

pageView?.let {
    val isFocused = false
    it.addAnnotation(checkboxWidget, isFocused)
}

Interactive Creation

Applicable to scenarios where users manually tap the PDF page to add a checkbox, commonly in form editing mode. In this mode, users can draw a rectangle on the screen to create a checkbox form.

Operation flow:

  1. Switch CPDFReaderView to form mode.
  2. Enable the touch mode for adding checkbox forms.
  3. Users draw a rectangle on the screen to create the checkbox form.
  4. Exit the form creation mode.

Example code:

java
// 1. Enter form mode
readerView.setViewMode(CPDFReaderView.ViewMode.FORM);

// 2. Enable touch mode for adding checkbox forms
readerView.setTouchMode(CPDFReaderView.TouchMode.ADD_ANNOT);
readerView.setCurrentFocusedType(Type.WIDGET);
readerView.setCurrentFocusedFormType(WidgetType.Widget_CheckBox);

// 3. Exit form creation mode
cPdfReaderView.setTouchMode(CPDFReaderView.TouchMode.BROWSE);
cPdfReaderView.setCurrentFocusedType(Type.UNKNOWN);
cPdfReaderView.setCurrentFocusedFormType(CPDFWidget.WidgetType.Widget_Unknown);
kotlin
// 1. Enter form mode
readerView.setViewMode(CPDFReaderView.ViewMode.FORM)

// 2. Enable touch mode for adding checkbox forms
readerView.setTouchMode(CPDFReaderView.TouchMode.ADD_ANNOT)
readerView.setCurrentFocusedType(Type.WIDGET)
readerView.setCurrentFocusedFormType(WidgetType.Widget_CheckBox)

// 3. Exit form creation mode
cPdfReaderView.setTouchMode(CPDFReaderView.TouchMode.BROWSE)
cPdfReaderView.setCurrentFocusedType(Type.UNKNOWN)
cPdfReaderView.setCurrentFocusedFormType(CPDFWidget.WidgetType.Widget_Unknown)

Create Radio Buttons

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

Programming Creation

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:

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()
}

ReaderView Synchronization

If the current PDF is being displayed via CPDFReaderView, and the target page is loaded or currently visible on screen, the radio button needs to be synchronously added to the corresponding CPDFPageView to be immediately displayed in the UI.

Only pages that are displayed or about to be displayed will have a CPDFPageView. Make sure to check for null.

java
CPDFPageView pageView =(CPDFPageView) readerView.getChild(pageIndex);

if (pageView != null) {
    boolean isFocused = false;
    pageView.addAnnotation(radiobuttonWidget, isFocused);
}
kotlin
val pageView = readerView.getChild(pageIndex) as? CPDFPageView

pageView?.let {
    val isFocused = false
    it.addAnnotation(radiobuttonWidget, isFocused)
}

Interactive Creation

Applicable to scenarios where users manually tap the PDF page to add a radio button, commonly in form editing mode. In this mode, users can draw a rectangle on the screen to create a radio button form.

Operation flow:

  1. Switch CPDFReaderView to form mode.
  2. Enable the touch mode for adding radio button forms.
  3. Users draw a rectangle on the screen to create the radio button form.
  4. Exit the form creation mode.

Example code:

java
// 1. Enter form mode
readerView.setViewMode(CPDFReaderView.ViewMode.FORM);

// 2. Enable touch mode for adding radio button forms
readerView.setTouchMode(CPDFReaderView.TouchMode.ADD_ANNOT);
readerView.setCurrentFocusedType(Type.WIDGET);
readerView.setCurrentFocusedFormType(WidgetType.Widget_RadioButton);

// 3. Exit form creation mode
cPdfReaderView.setTouchMode(CPDFReaderView.TouchMode.BROWSE);
cPdfReaderView.setCurrentFocusedType(Type.UNKNOWN);
cPdfReaderView.setCurrentFocusedFormType(CPDFWidget.WidgetType.Widget_Unknown);
kotlin
// 1. Enter form mode
readerView.setViewMode(CPDFReaderView.ViewMode.FORM)

// 2. Enable touch mode for adding radio button forms
readerView.setTouchMode(CPDFReaderView.TouchMode.ADD_ANNOT)
readerView.setCurrentFocusedType(Type.WIDGET)
readerView.setCurrentFocusedFormType(WidgetType.Widget_RadioButton)

// 3. Exit form creation mode
cPdfReaderView.setTouchMode(CPDFReaderView.TouchMode.BROWSE)
cPdfReaderView.setCurrentFocusedType(Type.UNKNOWN)
cPdfReaderView.setCurrentFocusedFormType(CPDFWidget.WidgetType.Widget_Unknown)

Create Combo Boxes

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

Programming Creation

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:

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()
}

ReaderView Synchronization

If the current PDF is being displayed via CPDFReaderView, and the target page is loaded or currently visible on screen, the combo box needs to be synchronously added to the corresponding CPDFPageView to be immediately displayed in the UI.

Only pages that are displayed or about to be displayed will have a CPDFPageView. Make sure to check for null.

java
CPDFPageView pageView =
        (CPDFPageView) readerView.getChild(pageIndex);

if (pageView != null) {
    boolean isFocused = false;
    pageView.addAnnotation(comboBoxWidget, isFocused);
}
kotlin
val pageView = readerView.getChild(pageIndex) as? CPDFPageView

pageView?.let {
    val isFocused = false
    it.addAnnotation(comboBoxWidget, isFocused)
}

Interactive Creation

Applicable to scenarios where users manually tap the PDF page to add a combo box, commonly in form editing mode. In this mode, users can draw a rectangle on the screen to create the combo box form.

Operation flow:

  1. Switch CPDFReaderView to form mode.
  2. Enable the touch mode for adding combo box forms.
  3. Users draw a rectangle on the screen to create the combo box form.
  4. Exit the form creation mode.

Example code:

java
// 1. Enter form mode
readerView.setViewMode(CPDFReaderView.ViewMode.FORM);

// 2. Enable touch mode for adding combo box forms
readerView.setTouchMode(CPDFReaderView.TouchMode.ADD_ANNOT);
readerView.setCurrentFocusedType(Type.WIDGET);
readerView.setCurrentFocusedFormType(WidgetType.Widget_ComboBox);

// 3. Exit form creation mode
cPdfReaderView.setTouchMode(CPDFReaderView.TouchMode.BROWSE);
cPdfReaderView.setCurrentFocusedType(Type.UNKNOWN);
cPdfReaderView.setCurrentFocusedFormType(CPDFWidget.WidgetType.Widget_Unknown);
kotlin
// 1. Enter form mode
readerView.setViewMode(CPDFReaderView.ViewMode.FORM)

// 2. Enable touch mode for adding combo box forms
readerView.setTouchMode(CPDFReaderView.TouchMode.ADD_ANNOT)
readerView.setCurrentFocusedType(Type.WIDGET)
readerView.setCurrentFocusedFormType(WidgetType.Widget_ComboBox)

// 3. Exit form creation mode
cPdfReaderView.setTouchMode(CPDFReaderView.TouchMode.BROWSE)
cPdfReaderView.setCurrentFocusedType(Type.UNKNOWN)
cPdfReaderView.setCurrentFocusedFormType(CPDFWidget.WidgetType.Widget_Unknown)