// Copyright (C) 2003-2026, Foxit Software Inc..
// All Rights Reserved.
//
// http://www.foxitsoftware.com
//
// The following code is copyrighted and contains proprietary information and trade secrets of Foxit Software Inc..
// You cannot distribute any part of Foxit PDF SDK to any third party or general public,
// unless there is a separate license agreement with Foxit Software Inc. which explicitly grants you such rights.
//
// This file contains an example to demonstrate how to use Foxit PDF SDK to add form field and
// get form field information.

#include "FSPDFObjC.h"

NSString *input_path = @"../input_files/";
NSString *output_path = @"../output_files/";

NSString *sn = @"bPxThFlBAYifO3yiW0C51bKbjPap1zHaf5aG5sX378TjFvB7Q0802A==";
NSString *key = @"8f3g1sGNtR8NAgfSNv+BvaJWy2fnrw36NZvzh8GUxYXvs+/YczAk4e34yqlOnmL4asgxGMO7+rlOcSHUSSKagx5g4G7VEXfkYLXnDDx77FOOedZPrFFcOCu1vT4+wOKzy1zkORq9cKtiFf44byH5sfRqf+OvELjep2teT9eF9yPCsSw2vRKljXlPy40DMP9XX1O5OTe+mSG3RvwKUMb0+RpG3YobM87j+/ynTDlJMMXw4vgwuTb1ZuAtlvkTdFz7HDLV81C6TYnn73ddHeGD3qYWBVsD4uJkDwyyH/SWr7QX0HSgVg8HSZqnab1VGdAUYxKY2vGWeOTJdu6qDGb5ZEb9O3kZZg+9LYAmUKzDfCYwfIsYZ8Y9LPC4wMx70kIq7JpNnzPvigrdshV8/ew1F0+qP/P5Z1EB53vDVrp2LVSuL873Wv4g3PzjZlNH+1qgT87hC+Buto4qsSUfP5Y2z0uApu+c/TfttCXbeMLSsw9TY1HQD3mkrZExMaQqRrAuXAcZWdvBnVuG3DCZmm8q0DLxJiCfYfViAU/AkbeKMIGaTJdrvIXBcssfde4nmU6qr0M4x4+Hs2ESddtY2QRtLAUBq/7h7GJlJHkEZr8ueaUy/6D/2vvAWz6wMLgmjlPbDTLqzfnCJylHCPtcJmu1SPPT9v1VreBX3+zNWcIT8HueyHEiUniqzwWkRKaotrnNb/zhnHLPpgaBSwKjyi8rOzzVwu/nY84G9ye4jsVjmkIRItKDrW7z22iS+QQuTwfqLVuMB0OD4jMkPCC2slzPfEeZnuh4+Rewln2qLRJVgI72zJ20OBWAelGSpO1Ii2Y3gOvojdmtUwx6R3Z1qFNWTTB25o9ytcux8BLOtTCkA8T8xaT4N339OTCsu/TmfahIDixKvr8cDa6F2WkBT8SOfv64W9PgiNv5B/BMOD2VWYaAl0N7yD0NkYYg/cDWRC4dfpdeip1jrr0k3mND0oSpIkxC+FicZbaYYwRdtJ9OSVWJ5OE0voLv8HgLze8mw6s4TrN3z+a2NQCveMa2VRN7YcEF7agPrvb/qU+otMwHae/wM7+f+HFAaOW+WTfzfxIVFB8/A1DN0NiEX+em4G7nO5F5ZcvfYPpaaioe8Nn33BbVLtMjXqsOBeM8AXiVfpD6YO75j7BqnBUPnivvD8BxJaaIQZvVSuQmvGm0ynt03IYwE8Rq+jYsunvsZ86jh5BiUR+Nm7sK0Qe9BdZn/7jdqohMv9czfMGUAscX+xBMunFbDpcRbWVpUADcm209L8Ndek2XY7YlCMxPgPVNvfWRF2jxehib9Tpd7zB5ZIDfAEH9zrpLfp29DLvCh2Yx8mH7xxdKG1O9Q0GdHphYHhej6smAcPm68R0GUQaTOLfXqNpueGJC1GAOI3Y4swjyt7jMTWcibnKLzwukWE5rnEDhn5n64JnyUM0ZFkRmtOiV";

void AddInteractiveForms(FSPDFPage *page, FSForm *form) {
    @autoreleasepool {
        {
            // Add push button field.
            FSControl *btn_submit = [form addControl:page field_name:@"Push Button Submit" field_type:FSFieldTypePushButton rect:[[FSRectF alloc] initWithLeft1:50 bottom1:750 right1:150 top1:780]];

            // Set default appearance
            FSDefaultAppearance *default_ap = [[FSDefaultAppearance alloc] initWithFlags:FSDefaultAppearanceFlagFont | FSDefaultAppearanceFlagTextColor | FSDefaultAppearanceFlagFontSize font:[[FSFont alloc] initWithFont_id:FSFontStdIDHelveticaB] text_size:12.0f text_color:0x000000];
            [form setDefaultAppearance:default_ap];

            // Set push button appearance.
            FSWidget *widget = [btn_submit getWidget];
            widget.highlightingMode = FSAnnotHighlightingPush;
            widget.mKBorderColor = 0xFF0000;
            widget.mKBackgroundColor = 0xF0F0F0;
            widget.mKNormalCaption = @"Submit";
            [widget resetAppearanceStream];

            // Set submit form action.
            FSSubmitFormAction *submit_action = [[FSSubmitFormAction alloc] initWithAction:[FSAction create:[form getDocument] action_type:FSActionTypeSubmitForm]];
            int count = [form getFieldCount:@""];
            NSArray *name_array = [[NSArray alloc] init];
            for (int i = 0; i < count; i++) {
                name_array = [name_array arrayByAddingObject:[[form getField:i filter:@""] getName]];
            }
            submit_action.fieldNames = name_array;
            submit_action.uRL = @"http://www.foxitsoftware.com";
            widget.action = submit_action;
            NSLog(@"Add button field.");
        }
        {
            // Add radio button group
            FSControl *control = [form addControl:page field_name:@"Radio Button0" field_type:FSFieldTypeRadioButton rect:[[FSRectF alloc] initWithLeft1:50.0 bottom1:700 right1:90 top1:740]];
            FSControl *control1 = [form addControl:page field_name:@"Radio Button0" field_type:FSFieldTypeRadioButton rect:[[FSRectF alloc] initWithLeft1:100.0 bottom1:700 right1:140 top1:740]];

            control.exportValue = @"Yes";
            [control setChecked:YES];
            // Update radio button's appearance.
            [[control getWidget] resetAppearanceStream];

            control1.exportValue = @"NO";
            [control1 setChecked:NO];
            // Update radio button's appearance.
            [[control1 getWidget] resetAppearanceStream];

            NSLog(@"Add radio button.");
        }
        {
            // Add check box
            FSControl *control = [form addControl:page field_name:@"Check Box0" field_type:FSFieldTypeCheckBox rect:[[FSRectF alloc] initWithLeft1:50.0 bottom1:650 right1:90 top1:690]];
            [control setChecked:YES];
            FSWidget *widget = [control getWidget];
            widget.mKBorderColor = 0x000000;
            widget.mKBackgroundColor = 0xFFFFFF;
            [widget resetAppearanceStream];
            NSLog(@"Add check box.");
        }
        {
            // Add text field
            FSControl *control = [form addControl:page field_name:@"Text Field0" field_type:FSFieldTypeTextField rect:[[FSRectF alloc] initWithLeft1:50.0 bottom1:600 right1:90 top1:640]];
            [control getField].value = @"3";
            // Update text field's appearance.
            [[control getWidget] resetAppearanceStream];
            FSControl *control1 = [form addControl:page field_name:@"Text Field1" field_type:FSFieldTypeTextField rect:[[FSRectF alloc] initWithLeft1:100 bottom1:600 right1:140 top1:640]];
            [control1 getField].value = @"23";
            // Update text field's appearance.
            [[control1 getWidget] resetAppearanceStream];

            FSControl *control2 = [form addControl:page field_name:@"Text Field2" field_type:FSFieldTypeTextField rect:[[FSRectF alloc] initWithLeft1:150 bottom1:600 right1:190 top1:640]];
            FSJavaScriptAction *javascipt_action = [[FSJavaScriptAction alloc] initWithAction:[FSAction create:[form getDocument] action_type:FSActionTypeJavaScript]];
            javascipt_action.script = @"AFSimple_Calculate(\"SUM\", new Array (\"Text Field0\", \"Text Field1\"));";
            FSField *field2 = [control2 getField];
            FSAdditionalAction *additional_act = [[FSAdditionalAction alloc] initWithField:field2];
            [additional_act setAction:FSAdditionalActionTriggerFieldRecalculateValue action:javascipt_action];
            [control2 getField].value = @"333333";
            // Update text field's appearance.
            [[control2 getWidget] resetAppearanceStream];

            NSLog(@"Add text field.");
        }
        {
            // Add text field with flag comb
            FSControl *control = [form addControl:page field_name:@"Text Field3" field_type:FSFieldTypeTextField rect:[[FSRectF alloc] initWithLeft1:50.0 bottom1:570 right1:100 top1:590]];
            FSField *field = [control getField];
            field.flags = FSFieldFlagTextComb;
            field.value = @"94538";
            field.maxLength = 5;
            // Update text field's appearance.
            [[control getWidget] resetAppearanceStream];
        }
        {
            // Add text field, with flag multiline.
            FSControl *control = [form addControl:page field_name:@"Text Field4" field_type:FSFieldTypeTextField rect:[[FSRectF alloc] initWithLeft1:110.0 bottom1:550 right1:190 top1:590]];
            FSField *field = [control getField];
            field.flags = FSFieldFlagTextMultiline;
            field.value = @"Text fields are boxes or spaces in which the user can enter text from the keyboard.";
            // Update text field's appearance.
            [[control getWidget] resetAppearanceStream];
        }
        {
            // Add text field, with flag password.
            FSControl *control = [form addControl:page field_name:@"Text Field5" field_type:FSFieldTypeTextField rect:[[FSRectF alloc] initWithLeft1:200.0 bottom1:570 right1:240 top1:590]];
            FSField *field = [control getField];
            field.flags = FSFieldFlagTextPassword;
            field.value = @"Password";
            // Update text field's appearance.
            [[control getWidget] resetAppearanceStream];
        }
        {
            // Add list box
            FSControl *control = [form addControl:page field_name:@"List Box0" field_type:FSFieldTypeListBox rect:[[FSRectF alloc] initWithLeft1:50.0 bottom1:450 right1:350 top1:500]];
            FSField *field = [control getField];
            FSChoiceOptionArray *options = [[FSChoiceOptionArray alloc] init];
            [options add:[[FSChoiceOption alloc] initWithOption_value:@"Foxit SDK" option_label:@"Foxit SDK" selected:YES default_selected:YES]];
            [options add:[[FSChoiceOption alloc] initWithOption_value:@"Foxit Reader" option_label:@"Foxit Reader" selected:YES default_selected:YES]];
            [options add:[[FSChoiceOption alloc] initWithOption_value:@"Foxit Phantom" option_label:@"Foxit Phantom" selected:YES default_selected:YES]];
            field.options = options;

            FSWidget *widget = [control getWidget];
            widget.mKBorderColor = 0x000000;
            widget.mKBackgroundColor = 0xFFFFFF;
            [widget resetAppearanceStream];
        }
        {
            // Add combo box
            FSControl *control = [form addControl:page field_name:@"Combo Box0" field_type:FSFieldTypeComboBox rect:[[FSRectF alloc] initWithLeft1:50.0 bottom1:350 right1:350 top1:400]];
            FSField *field = [control getField];
            FSChoiceOptionArray *options = [[FSChoiceOptionArray alloc] init];
            [options add:[[FSChoiceOption alloc] initWithOption_value:@"Foxit SDK" option_label:@"Foxit SDK" selected:YES default_selected:YES]];
            [options add:[[FSChoiceOption alloc] initWithOption_value:@"Foxit Reader" option_label:@"Foxit Reader" selected:YES default_selected:YES]];
            [options add:[[FSChoiceOption alloc] initWithOption_value:@"Foxit Phantom" option_label:@"Foxit Phantom" selected:YES default_selected:YES]];
            field.options = options;

            FSWidget *widget = [control getWidget];
            widget.mKBorderColor = 0x000000;
            widget.mKBackgroundColor = 0xFFFFFF;
            [widget resetAppearanceStream];
        }
    }
}

int main(int argc, const char *argv[]) {
    @autoreleasepool {
        NSString *output_directory = [output_path stringByAppendingString:@"form/"];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        BOOL isDir = NO;
        if ([fileManager fileExistsAtPath:output_directory isDirectory:&isDir]) {
            if (!isDir) {
                NSError *error = nil;
                if (![fileManager removeItemAtPath:output_directory error:&error] || error) {
                    return -1;
                }
                if (![fileManager createDirectoryAtPath:output_directory withIntermediateDirectories:YES attributes:nil error:&error] || error) {
                    return -1;
                }
            }
        } else {
            NSError *error = nil;
            if (![fileManager createDirectoryAtPath:output_directory withIntermediateDirectories:YES attributes:nil error:&error] || error) {
                return -1;
            }
        }

        // Initialize library
        FSErrorCode errorCode = [FSLibrary initialize:sn key:key];
        if (errorCode != FSErrSuccess) {
            NSLog(@"Library Initialize Error: %ld", errorCode);
            return -1;
        }

        @try {
            FSPDFDoc *doc = [[FSPDFDoc alloc] init];
            FSForm *form = [[FSForm alloc] initWithDocument:doc];
            // Create a blank new page and add some form fields.
            FSPDFPage *page = [doc insertPageWithType:0 size:FSPDFPageSizeLetter];

            AddInteractiveForms(page, form);

            NSString *newPdf = [output_directory stringByAppendingString:@"form.pdf"];
            [doc saveAs:newPdf save_flags:FSPDFDocSaveFlagNoOriginal];
        }
        @catch (NSException *e) {
            NSLog(@"Unknown Exception, %@", e);
        }
    }
    [FSLibrary destroy];
    return 0;
}
