Creating and Filling Out Forms in Objective-C

Tutorials | Forms · Objective-C · How To Fri. 12 Aug. 2022

Forms, as we know, it’s an important tool for people to gather pieces of information like personal information or preferences about some products. PDF forms also have certain advantages. Limit input data to the needed input format. Unlike Word, distributed PDF documents with forms can also be supported on many different platforms and still behave in the same way.

 

This article will introduce how to create and fill out forms in Objective-C. When you finish creating the form fields, ComPDFKit PDF SDK allows you to delete your form fields as your changing ideas. At last, you will find flattening PDF forms helpful if you want to ensure that the forms you created are non-editable. Let’s get started.

 

 

Supported Form Fields

 

There are kinds of form types that have different functions. If you need to sign a document, use a field type for signatures. But if your need is to create a field for others to fill some text like name, address, etc., a text field is proper. 

 

ComPDFKit PDF SDK supports all form types specified by the PDF specification (Text Field, Check Box, Radio Buttons, Drop-down List, Combo Box, Push Button, and Signature Field). Like we said above, every field type has its using occasion. Let’s see when you can use them. 

 

  • Text Field: Let users type some text like name, address, number, or other details.
  • Check Box: Give options to choose, usually the options are “Yes” & “No”.
  • Radio button: Create a set of mutually exclusive radio buttons for others to choose from. Only one can be selected at a time.
  • Drop-down List: Display a list of options for others to choose from.
  • Combo Box: Let users either select an item from a pop-up menu or type in a value.
  • Push Button: Create a button for users to initiate certain predefined actions like submitting or resetting the information.
  • Signature Field: Provide a blank field for users to sign this document.

 

CPDFWidgetAnnotation is the base class for all form fields, and CPDFWidgetAnnotation is a subclass for CPDFAnnotation. A CPDFWidgetAnnotation object by itself is not useful, only subclasses (CPDFButtonWidgetAnnotationCPDFChoiceWidgetAnnotation, CPDFTxtWidgetAnnotation, CPDFSignatureWidgetAnnotation) are interesting. In parsing a PDF, however, any unknown or unsupported form fields will be represented as this base class.

 

We have to differentiate between annotation objects and field types:

 

  • CPDFButtonWidgetAnnotation: Check, Radio, and Push Button
  • CPDFChoiceWidgetAnnotation: List and Combo Box
  • CPDFTxtWidgetAnnotation: Text Field
  • CPDFSignatureWidgetAnnotation: Signature Field

 

 

Create & Edit Form Fields

 

Creating fillable form fields works the same as adding any other annotation. We can find them in the guides for programmatically creating annotations. Let's learn to add types of form fields to a document by the following code.

 

NSURL *url = [NSURL fileURLWithPath:pdfPath];
CPDFDocument *document = [[[CPDFDocument alloc] initWithURL:url] autorelease];
CPDFPage *page = [document pageAtIndex:0];

NSMutableArray *items = [NSMutableArray array];
CPDFChoiceWidgetItem *item1 = [[[CPDFChoiceWidgetItem alloc] init] autorelease];
item1.value = @"1";
item1.string = @"a";
[items addObject:item1];
CPDFChoiceWidgetItem *item2 = [[[CPDFChoiceWidgetItem alloc] init] autorelease];
item2.value = @"2";
item2.string = @"b";
[items addObject:item2];

CPDFChoiceWidgetAnnotation *widget = [[[CPDFChoiceWidgetAnnotation alloc] initWithDocument:document listChoice:YES] autorelease];
widget.items = items;
[page addAnnotation:widget];

 

 

Fill Form Fields

 

ComPDFKit PDF SDK fully supports the AcroForm standard, and forms can be viewed and filled inside the CPDFView.

 

To fill in a text form element, tap it and type text using your keyboard. Then tap either the Done button above the keyboard or any blank area on the page to deselect the form element, which will commit the changes.

 

Text Form Fields of ComPDFKit

 

To set the value of a choice form element (a list or combo box), tap the element. And then select an item from the list, or type in a custom item.

 

List or Combo Box of ComPDFKit

 

To enable or disable a checkbox form element, tap it to toggle its state. And you can set the selection of a radio button form element by tapping the desired item.

 

Checkbox & Radio Button Form of ComPDFKit

 

While a form element is selected (focused), the left and right arrows on the keyboard can be used to move the focus sequentially between all the form elements on the page.

 

The following example demonstrates how form fields can be queried and filled with code:

NSURL *url = [NSURL fileURLWithPath:pdfPath];
CPDFDocument *document = [[[CPDFDocument alloc] initWithURL:url] autorelease];
CPDFPage *page = [document pageAtIndex:0];

NSArray *annotations = [page annotations];
for (CPDFAnnotation *annotation in annotations) {
    if ([annotation isKindOfClass:[CPDFTextWidgetAnnotation class]]) {
        [(CPDFTextWidgetAnnotation *)annotation setStringValue:@""];
    } else if ([annotation isKindOfClass:[CPDFButtonWidgetAnnotation class]]) {
        if (CPDFWidgetRadioButtonControl == [(CPDFButtonWidgetAnnotation *)annotation controlType]) {
            [(CPDFButtonWidgetAnnotation *)annotation setState:1];
        }
    } else if ([annotation isKindOfClass:[CPDFChoiceWidgetAnnotation class]]) {
        [(CPDFChoiceWidgetAnnotation *)annotation setSelectItemAtIndex:0];
    }
}

 

 

Related Operations

 

Except for creating, editing, and filling PDF form fields. There are also other operations to take into account. We are going to take “Flatten PDF Forms” as an example. For more related operations like deleting form fields, please refer to our developer guides.

 

Flatten PDF Forms

 

Form elements can be flattened in Objective-C by ComPDFKit PDF SDK. Form flattening changes active form fields into a static area that is part of the PDF document, just like other text and images in the PDF document. After filling out a PDF form, try to flatten all the PDF forms so that no one can change your information. 

 

PDF Form flattening works the same as annotation flattening, and refer to annotation flattening in our guides to see more about how to flatten PDF forms programmatically.

 

 

Final Words

 

Forms in PDF documents provide a lot of benefits for collecting information. It’s the best way for us to collect the information of people because it allows us to predefine the standardized input format for data. We can see online data submission everywhere like questionnaires, and application forms. PDF forms ensure the document will be presented exactly how you laid it out.

Ready to Get Started?

Download our all-in-one ComPDFKit for free and run it to your project within minutes!