Tutorials

PDF Form Fields on Android

By ComPDFKit | Fri. 17 May. 2024
FormsJavaAndroid

ComPDFKit SDK provides you with a wide range of features that allow you to create and edit PDF forms. We will introduce ComPDFKit through the following parts:

 

         - How to integrate ComPDFKit.

         - The differences and scenarios between different form fields.

         - The methods to create, edit, delete, fill out, or save forms by Javascript.

         - Extended user permissions.



Integration

 

If you want to create and edit PDF form fields on the Android platform, you could choose to integrate the SDK which has the functionalities to help with the PDF forms like ComPDFKit. Here are the steps to integrate our ComPDFKit PDF SDK on Android platform.



Form Fields: Differences and Scenarios

 

         - Text Fields: Text Fields are used to capture free-form text from users and may be single or multi-line. Any text entered into a Text Field is automatically saved in one line per field entry when the application is first launched, even if the user does not close or save their document. This allows you to collect the free descriptional text.

         - Check Boxes: Check Boxes are used for yes/no responses from users or multiple-choice answers from categories. The color, style, and the name can be set according to your program.

         - Radio Buttons: Radio Buttons are similar to Check Boxes except that they allow only one selection at a time (unlike Check Boxes where multiple selections are allowed). So, Radio Buttons always are used to let others choose one option among groups of two or more options.

         - List Boxes: List box is a list of choices that can be used for multiple selections. The options in this list can be edited, deleted, and added to as needed.

         - Combo Boxes: Combo Box is a combination of drop-down lists and text boxes. It can be used for selecting a right selection. You can select one option or type the option you want to choose.

         - Push Button: A Push Button is a button that can be clicked to trigger an action. The action can be jumping to specific page or opening a web link.

         - Signature: A Signature Field is a small box where you can enter text or draw a line with your mouse to show your signature. This field is used for legal documents and agreements.



Creating & Deleting Form Fields programmatically

 

You can create, edit, and delete form fields in JavaScript with ComPDFKit using the  addAnnotation or deleteAnnotation methods of the  CPDFPageView object. The following example shows how to add a text field, edit, and delete programmatically.

 

CPDFTextWidget textWidget = (CPDFTextWidget) tpdfPage.addFormWidget(CPDFWidget.WidgetType.Widget_TextField);
if (textWidget == null || !textWidget.isValid()) {
    return false;
}


textWidget.setFontName(CPDFTextAttribute.FontNameHelper.Font_Default_Type);
textWidget.setFontSize(12);
textWidget.setMultiLine(false);
textWidget.setFontRGBColor(Color.BLACK);
textWidget.setWidgetBgRGBColor(Color.WHITE);
textWidget.setWidgetBorderRGBColor(Color.BLACK);
textWidget.setJustification(CPDFTextAlignment.ALIGNMENT_LEFT);
textWidget.setTextFieldSpecial(false);
textWidget.setBorderWidth(0);
textWidget.setWidgetBorderStyle(CPDFWidget.BorderStyle.BS_Solid);


textWidget.setFieldName(String.format("ComPDFKit-%d", System.currentTimeMillis()));
RectF drawRect = new RectF(0,0,100,100);
RectF rect = new RectF();
TMathUtils.scaleRectF(drawRect, rect, 1 / pageView.getScaleValue());
RectF size = readerView.getPageNoZoomSize(pageView.getPageNum());
if (size.isEmpty()) {
    return false;
}
rect.set(tpdfPage.convertRectToPage(readerView.isCropMode(), size.width(), size.height(), rect));
textWidget.setRect(rect);
textWidget.updateFormAp();

                
//add an annotation of text box form
CPDFBaseAnnotImpl annotImpl = pageView.addAnnotation(textWidget, true);


//edit the annotation
pageView.createInputBox(annotImpl, ContextMenuType.Edit);


//delete the annotation
pageView.deleteAnnotation(annotImpl);



Conclusion


ComPDFKit is a powerful, lightweight PDF library that allows you to create, edit and manipulate PDF files using JavaScript. If you are looking for a simple and easy-to-use toolkit to perform tasks such as adding or removing form fields on an Android device, then contact the ComPDFKit team.