Skip to content
Guides

Create Interactive Forms in PDF

This sample shows how to print form list information, set up interactive forms (including text, checkbox, radioButton, button, list, Combox, and sign forms, delete forms), and fill out form information.

objective-c
// Init Methods
- (instancetype)initWithDocument:(CPDFDocument *)document {
  CPDFDocument *myDocument = document;
  [self createTestForms:myDocument];
}

// Create all type form
- (void)createTestForms:(CPDFDocument *)oldDocument {
  NSString *commandLineStr = @"";
  
    // Get Sandbox path for saving the PDFFile
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"InteractiveForms"];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
        [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"CreateInteractiveFormsTest"];
    
    // Save the document in the test PDF file
    NSURL *interactiveFormsURL = [NSURL fileURLWithPath:writeFilePath];
    [oldDocument writeToURL:interactiveFormsURL];
    
    // Create a new document for test PDF file
    CPDFDocument *document = [[CPDFDocument alloc] initWithURL:interactiveFormsURL];
    
    // Create form using API
    CPDFPage *page = [document pageAtIndex:0];
    CGSize pageSize = [document pageSizeAtIndex:0];
    CGFloat hight = pageSize.height;
    
    // create new Form Fields and Widget Annotations.
    {
        // Insert a single-line TextField.
        CPDFTextWidgetAnnotation *textWidget1 = [[CPDFTextWidgetAnnotation alloc] initWithDocument:document];
        textWidget1.bounds = CGRectMake(28, hight-30, 80, 25);
        textWidget1.fieldName = @"TextField1";
        textWidget1.isMultiline = NO;
        textWidget1.stringValue = @"Basic Text Field";
        textWidget1.fontColor = [UIColor blackColor];
        textWidget1.font = [UIFont systemFontOfSize:15];
        [page addAnnotation:textWidget1];
    }
    
    {
        // Insert a multiline TextField.
        CPDFTextWidgetAnnotation *textWidget2 = [[CPDFTextWidgetAnnotation alloc] initWithDocument:document];
        textWidget2.bounds = CGRectMake(28, hight-100, 80, 60);
        textWidget2.fieldName = @"TextField2";
        textWidget2.stringValue = @"Basic Text Field\nBasic Text Field\nBasic Text Field";
        textWidget2.isMultiline = YES;
        textWidget2.fontColor = [UIColor blackColor];
        textWidget2.font = [UIFont systemFontOfSize:15];
        [page addAnnotation:textWidget2];
    }
    
    {
        // Insert a ListBox widget.
        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, hight-100, 200, 100);
        choiceWidget.items = items;
        choiceWidget.selectItemAtIndex = 2;
        [page addAnnotation:choiceWidget];
    }
    
    {
        // Insert a ComboBox Widget.
        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, hight-200, 200, 100);
        choiceWidget.items = items;
        choiceWidget.selectItemAtIndex = 2;
        [page addAnnotation:choiceWidget];
    }
    
    {
        //Insert a Form Signature Widget (unsigned)
        CPDFSignatureWidgetAnnotation *signatureWidget = [[CPDFSignatureWidgetAnnotation alloc] initWithDocument:document];
        signatureWidget.bounds = CGRectMake(28, hight-206, 80, 101);
        signatureWidget.fieldName = @"Signature1";
        [page addAnnotation:signatureWidget];
    }
    
    {
        // Insert a PushButton to jump to a page.
        CPDFButtonWidgetAnnotation *pushButton = [[CPDFButtonWidgetAnnotation alloc] initWithDocument:document controlType:CPDFWidgetPushButtonControl];
        pushButton.bounds = CGRectMake(267, hight-300, 130, 80);
        pushButton.fieldName = @"PushButton1";
        pushButton.caption = @"PushButton";
        pushButton.fontColor = [UIColor blackColor];
        pushButton.font = [UIFont systemFontOfSize:15];
        
        CPDFDestination * destination = [[CPDFDestination alloc] initWithDocument:document pageIndex:1];
        CPDFGoToAction * goToAction = [[CPDFGoToAction alloc] initWithDestination:destination];
        [pushButton setAction:goToAction];
        
        [page addAnnotation:pushButton];
    }
    
    {
        // Insert a PushButton to jump to a website..
        CPDFButtonWidgetAnnotation *pushButton = [[CPDFButtonWidgetAnnotation alloc] initWithDocument:document controlType:CPDFWidgetPushButtonControl];
        pushButton.bounds = CGRectMake(367, hight-303, 150, 80);
        pushButton.fieldName = @"PushButton2";
        pushButton.caption = @"PushButton";
        pushButton.fontColor = [UIColor blackColor];
        pushButton.font = [UIFont systemFontOfSize:15];
        
        CPDFURLAction *urlAction = [[CPDFURLAction alloc] initWithURL:@"https://www.compdf.com/"];
        [pushButton setAction:urlAction];
        
        [page addAnnotation:pushButton];
    }
    
    {
        //Insert CheckBox Widget
        CPDFButtonWidgetAnnotation *checkBox = [[CPDFButtonWidgetAnnotation alloc] initWithDocument:document controlType:CPDFWidgetCheckBoxControl];
        checkBox.bounds = CGRectMake(67, hight-351, 100, 90);
        checkBox.fieldName = @"CheckBox1";
        checkBox.borderColor = [UIColor blackColor];
        checkBox.backgroundColor = [UIColor greenColor];
        checkBox.borderWidth = 2.0;
        checkBox.state = 0;
        checkBox.font = [UIFont systemFontOfSize:15];
        [page addAnnotation:checkBox];
    }
    
    {
        //Insert CheckBox Widget
        CPDFButtonWidgetAnnotation *checkBox = [[CPDFButtonWidgetAnnotation alloc] initWithDocument:document controlType:CPDFWidgetCheckBoxControl];
        checkBox.bounds = CGRectMake(167, hight-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];
    }
    
    {
        //Insert Radio Button Widget
        CPDFButtonWidgetAnnotation *radioButton = [[CPDFButtonWidgetAnnotation alloc] initWithDocument:document controlType:CPDFWidgetRadioButtonControl];
        radioButton.bounds = CGRectMake(167, hight-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];
    }
    
    {
        //Insert Radio Button Widget
        CPDFButtonWidgetAnnotation *radioButton = [[CPDFButtonWidgetAnnotation alloc] initWithDocument:document controlType:CPDFWidgetRadioButtonControl];
        radioButton.bounds = CGRectMake(267, hight-451, 100, 90);
        radioButton.fieldName = @"RadioButton2";
        radioButton.borderColor = [UIColor blackColor];
        radioButton.backgroundColor = [UIColor greenColor];
        radioButton.borderWidth = 2.0;
        radioButton.state = 1;
        radioButton.font = [UIFont systemFontOfSize:15];
        [page addAnnotation:radioButton];
    }
    
    {
        //Insert Radio Button Widget
        CPDFButtonWidgetAnnotation *radioButton = [[CPDFButtonWidgetAnnotation alloc] initWithDocument:document controlType:CPDFWidgetRadioButtonControl];
        radioButton.bounds = CGRectMake(367, hight-451, 100, 90);
        radioButton.fieldName = @"RadioButton3";
        radioButton.borderColor = [UIColor blackColor];
        radioButton.backgroundColor = [UIColor greenColor];
        radioButton.borderWidth = 2.0;
        radioButton.state = 0;
        radioButton.font = [UIFont systemFontOfSize:15];
        [page addAnnotation:radioButton];
    }
    
    // Save the create forms action in document
    [document writeToURL:interactiveFormsURL];
    
    // Delete the first form
    [self deleteForm:document];
  
  // Print annotation list information
    [self printFormsMessage:document];
  
    commandLineStr = [commandLineStr stringByAppendingString:@"Done.\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in CreateInteractiveFormsTest.pdf\n"];
}

// Print form message
- (void)printFormsMessage:(CPDFDocument *)document {
  NSString *commandLineStr = @"";
  
    CPDFPage *page = [document pageAtIndex:0];

    NSArray *annotations = [page annotations];
    for (CPDFWidgetAnnotation *annotation in annotations) {
        commandLineStr = [commandLineStr stringByAppendingFormat:@"Field name : %@\n", annotation.fieldName];
        if ([annotation isKindOfClass:[CPDFTextWidgetAnnotation class]]) {
            commandLineStr = [commandLineStr stringByAppendingFormat:@"Field partial name :  %@\n", ((CPDFTextWidgetAnnotation*)annotation).stringValue];
        } else if ([annotation isKindOfClass:[CPDFButtonWidgetAnnotation class]]) {
            if (CPDFWidgetRadioButtonControl == [(CPDFButtonWidgetAnnotation *)annotation controlType]) {
                commandLineStr = [commandLineStr stringByAppendingFormat:@"Field isChecked :   %ld\n", ((CPDFButtonWidgetAnnotation *)annotation).state];
            } else if (CPDFWidgetPushButtonControl == [(CPDFButtonWidgetAnnotation *)annotation controlType]) {
                commandLineStr = [commandLineStr stringByAppendingFormat:@"Field PushButton Title :   %@\n", ((CPDFButtonWidgetAnnotation *)annotation).caption];
                CPDFAction *action = ((CPDFButtonWidgetAnnotation *)annotation).action;
                if ([action isKindOfClass:[CPDFURLAction class]]) {
                    commandLineStr = [commandLineStr stringByAppendingFormat:@"Field PushButton Action : %@\n", ((CPDFURLAction *)action).url];
                } else if ([action isKindOfClass:[CPDFGoToAction class]]) {
                    commandLineStr = [commandLineStr stringByAppendingFormat:@"Field PushButton Action : Jump to page %ld of the document\n",((CPDFGoToAction *)action).destination.pageIndex];
                }
            } else if (CPDFWidgetCheckBoxControl == [(CPDFButtonWidgetAnnotation *)annotation controlType]) {
                commandLineStr = [commandLineStr stringByAppendingFormat:@"Field isChecked :   %ld\n", ((CPDFButtonWidgetAnnotation *)annotation).state];
            }
        } else if ([annotation isKindOfClass:[CPDFChoiceWidgetAnnotation class]]) {
            commandLineStr = [commandLineStr stringByAppendingFormat:@"Field Select Item :   %ld\n", ((CPDFChoiceWidgetAnnotation *)annotation).selectItemAtIndex];
        }
        
        commandLineStr = [commandLineStr stringByAppendingFormat:@"\tPosition: %d, %d, %d, %d\n", (int)annotation.bounds.origin.x,(int)annotation.bounds.origin.y, (int)annotation.bounds.size.width, (int)annotation.bounds.size.height];
        commandLineStr = [commandLineStr stringByAppendingFormat:@"Widget type : %@\n", annotation.type];
        commandLineStr = [commandLineStr stringByAppendingString:@"-------------------------------------\n"];
    }
}

// Delete the first form
- (void)deleteForm:(CPDFDocument *)oldDocument {
  NSString *commandLineStr = @"";
  
    // Get Sandbox path for saving the PDFFile
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *writeDirectoryPath = [NSString stringWithFormat:@"%@/%@", path, @"InteractiveForms"];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:writeDirectoryPath])
        [[NSFileManager defaultManager] createDirectoryAtURL:[NSURL fileURLWithPath:writeDirectoryPath] withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *writeFilePath = [NSString stringWithFormat:@"%@/%@.pdf",writeDirectoryPath,@"DeleteInteractiveFormsTest"];
    
    // Save the document in the test PDF file
    NSURL *deleteInteractiveFormsURL = [NSURL fileURLWithPath:writeFilePath];
    [oldDocument writeToURL:deleteInteractiveFormsURL];
    
    // Create a new document for test PDF file
    CPDFDocument *document = [[CPDFDocument alloc] initWithURL:deleteInteractiveFormsURL];
    
    // Remove the first form from document
    CPDFPage *page = [document pageAtIndex:0];

    CPDFAnnotation *annotation = [[page annotations] objectAtIndex:0];
    [page removeAnnotation:annotation];
    
    // Save the remove form action in document
    [document writeToURL:deleteInteractiveFormsURL];
    commandLineStr = [commandLineStr stringByAppendingString:@"Done.\n"];
    commandLineStr = [commandLineStr stringByAppendingString:@"Done. Results saved in DeleteInteractiveFormsTest.pdf\n"];
}