Foxit PDF SDK for Linux Foxit PDF SDK for Mac Foxit PDF SDK for Windows

How to Create your own JavaScript for PDF Forms

JavaScript is crucial to form workflows for companies who want to boast the latest and greatest in document technologies. The article below demonstrates how to add JavaScript to PDF forms with Foxit PDF SDK.

Platform: Windows, Mac, Linux

Programming Language: C++, Java, C#, Objective-C, C

License Key requirement: Standard

SDK Version: Foxit PDF SDK 8.3


Below we will detail six simple demos.

Step 1

AFSimple_Calculate: We will add two text field whose values will be used to add together. The third text field will contain the value of the sum of the first two form fields.

C++:

//AFSimple_Calculate JavaScript for text fields.
void AddInteractiveFormsForSimpleCalculate(PDFPage & page, interform::Form &form) {
//Add the first text field.
Control control = form.AddControl(page, L"Text Field Addend", Field::e_TypeTextField, RectF(50, 600, 90, 640));
control.GetField().SetValue(L"3");
// Update text field's appearance.
control.GetWidget().ResetAppearanceStream();
//Add the second text field.
Control control1 = form.AddControl(page, L"Text Field Augend", Field::e_TypeTextField, RectF(100, 600, 140, 640));
control1.GetField().SetValue(L"23");
// Update text field's appearance.
control1.GetWidget().ResetAppearanceStream();
Control control2 = form.AddControl(page, L"Text Field Sum", Field::e_TypeTextField, RectF(150, 600, 190, 640));//Add the third text field.
//Create a new JavaScript action.
actions::JavaScriptAction javascipt_action = (actions::JavaScriptAction)Action::Create(form.GetDocument(), Action::e_TypeJavaScript);
//Set Javascript
javascipt_action.SetScript(L"AFSimple_Calculate(\"SUM\", new Array (\"Text Field Addend\", \"Text Field Augend\"));");
Field field2 = control2.GetField();
AdditionalAction additional_act(field2);
//Set the JS action to the Additional action of the form field.
additional_act.SetAction(AdditionalAction::e_TriggerFieldRecalculateValue, javascipt_action);
//Trigger this action. Although the set value is 333333, the actual value is 26 by adding 3 to 23.
field2.SetValue(L"333333");
// Update text field's appearance.
control2.GetWidget().ResetAppearanceStream();
}

C#.Net and .Net Core:

static void AddInteractiveFormsForSimpleCalculate(PDFPage page, Form form) {
//Add the first text field.
using (Control control = form.AddControl(page, "Text Field Addend", Field.Type.e_TypeTextField, new RectF(50, 600, 90, 640)))
//Add the second text field.
using (Control control1 = form.AddControl(page, "Text Field Augend", Field.Type.e_TypeTextField, new RectF(100, 600, 140, 640)))
//Add the third text field.
using (Control control2 = form.AddControl(page, "Text Field Sum", Field.Type.e_TypeTextField, new RectF(150, 600, 190, 640)))
using (Field field = control.GetField())
using (Field field2 = control2.GetField())
using (Widget widget = control.GetWidget())
using (Widget widget1 = control1.GetWidget())
using (Widget widget2 = control2.GetWidget())
using (PDFDoc doc = form.GetDocument()) {
field.SetValue("3");
// Update text field's appearance.
widget.ResetAppearanceStream();
field1.SetValue("23");
// Update text field's appearance.
widget1.ResetAppearanceStream();
//Create a new JavaScript action.
using (foxit.pdf.actions.Action action = foxit.pdf.actions.Action.Create(doc, foxit.pdf.actions.Action.Type.e_TypeJavaScript))
using (JavaScriptAction javascipt_action = new JavaScriptAction(action)) {
//Set Javascript
javascipt_action.SetScript("AFSimple_Calculate(\"SUM\", new Array (\"Text Field Addend\", \"Text Field Augend\"));");
using (AdditionalAction additional_act = new AdditionalAction(field2)) {
//Set the JS action to the Additional action of the form field.
additional_act.SetAction(foxit.pdf.actions.AdditionalAction.TriggerEvent.e_TriggerFieldRecalculateValue, javascipt_action);
}
}
//Trigger this action. Although the set value is 333333, the actual value is 26 by adding 3 to 23.
field2.SetValue("333333");
//Update text field's appearance.
widget2.ResetAppearanceStream();
}
}

Standard Java:

static void AddInteractiveFormsForSimpleCalculate(PDFPage page, Form form) {
try {
//Add the first text field.
Control control = form.addControl(page, "Text Field Addend", com.foxit.sdk.pdf.interform.Field.e_TypeTextField, new RectF(50, 600, 90, 640));
//Add the second text field.
Control control1 = form.addControl(page, "Text Field Augend", com.foxit.sdk.pdf.interform.Field.e_TypeTextField, new RectF(100, 600, 140, 640));
//Add the third text field.
Control control2 = form.addControl(page, "Text Field Sum", com.foxit.sdk.pdf.interform.Field.e_TypeTextField, new RectF(150, 600, 190, 640));
Field field = control.getField();
Field field1 = control1.getField();
Field field2 = control2.getField();
Widget widget = control.getWidget();
Widget widget1 = control1.getWidget();
Widget widget2 = control2.getWidget();
PDFDoc doc = form.getDocument();
field.setValue("3");
// Update text field's appearance.
widget.resetAppearanceStream();
field1.setValue("23");
// Update text field's appearance.
widget1.resetAppearanceStream();
//Create a new JavaScript action.
com.foxit.sdk.pdf.actions.Action action = com.foxit.sdk.pdf.actions.Action.create(doc, com.foxit.sdk.pdf.actions.Action.e_TypeJavaScript);
JavaScriptAction javascipt_action = new JavaScriptAction(action);
//Set Javascript
javascipt_action.setScript("AFSimple_Calculate(\"SUM\", new Array (\"Text Field Addend\", \"Text Field Augend\"));");
AdditionalAction additional_act = new AdditionalAction(field2);
//Set the JS action to the Additional action of the form field.
additional_act.setAction(com.foxit.sdk.pdf.actions.AdditionalAction.e_TriggerFieldRecalculateValue, javascipt_action);
//Trigger this action. Although the set value is 333333, the actual value is 26 by adding 3 to 23.
field2.setValue("333333");
// Update text field's appearance.
widget2.resetAppearanceStream();
} catch (PDFException e) {
e.printStackTrace();
}
}

ObjectiveC:

//AFSimple_Calculate JavaScript for text fields.
void AddInteractiveFormsForSimpleCalculate(FSPDFPage* page, FSForm* form) {
@autoreleasepool {
//Add the first text field.
FSControl *control = [form addControl:page field_name:@"Text Field Addend" 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];
//Add the second text field.
FSControl *control1 = [form addControl:page field_name:@"Text Field Augend" field_type:FSFieldTypeTextField rect:[[FSRectF alloc] initWithLeft1:100.0 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 Field Sum" field_type:FSFieldTypeTextField rect:[[FSRectF alloc] initWithLeft1:150.0 bottom1:600 right1:190 top1:640]];//Add the third text field.
//Create a new JavaScript action.
FSJavaScriptAction *javascipt_action = [[FSJavaScriptAction alloc] initWithAction:[FSAction create:[form getDocument] action_type:FSActionTypeJavaScript]];
//Set Javascript
javascipt_action.script = @"AFSimple_Calculate(\"SUM\", new Array (\"Text Field Addend\", \"Text Field Augend\"));";
FSField *field2 = [control2 getField];
FSAdditionalAction *additional_act = [[FSAdditionalAction alloc] initWithField:field2];
[additional_act setAction:FSAdditionalActionTriggerFieldRecalculateValue action:javascipt_action];
//Trigger this action. Although the set value is 333333, the actual value is 26 by adding 3 to 23.
[control2 getField].value = @"333333";
// Update text field's appearance.
[[control2 getWidget] resetAppearanceStream];
}
}

C:

//AFSimple_Calculate JavaScript for text fields.
void AddInteractiveFormsForSimpleCalculate(FS_PDFPAGE_HANDLE & page, FS_FORM_HANDLE &form) {
//Add the first text field.
FS_CONTROL_HANDLE control;
FSRectF rect;
rect.left = 50;
rect.bottom = 600;
rect.right = 90;
rect.top = 640;
FSDK_Form_AddControl(form, page, L"Text Field Addend", e_FSTypeTextField, rect, control);
FS_FIELD_HANDLE field;
FSDK_Control_GetField(control, field);
FSDK_Field_SetValue(field, L"3");
// Update text field's appearance.
FS_WIDGETANNOT_HANDLE widget;
FSDK_Control_GetWidget(control, widget);
FS_BOOL ret = false;
FSDK_Annot_ResetAppearanceStream(widget, ret);

//Add the second text field.
FS_CONTROL_HANDLE control1;
rect.left = 100;
rect.bottom = 600;
rect.right = 140;
rect.top = 640;
FSDK_Form_AddControl(form, page, L"Text Field Augend", e_FSTypeTextField, rect, control1);
FS_FIELD_HANDLE field1;
FSDK_Control_GetField(control1, field1);
FSDK_Field_SetValue(field1, L"23");

// Update text field's appearance.
FS_WIDGETANNOT_HANDLE widget1;
FSDK_Control_GetWidget(control1, widget1);
FSDK_Annot_ResetAppearanceStream(widget1, ret);

//Add the third text field.
FS_CONTROL_HANDLE control2;
rect.left = 150;
rect.bottom = 600;
rect.right = 190;
rect.top = 640;
FSDK_Form_AddControl(form, page, L"Text Field Sum", e_FSTypeTextField, rect, control2);
FS_FIELD_HANDLE field2;
FSDK_Control_GetField(control2, field2);

FS_PDFDOC_HANDLE doc;
FSDK_Form_GetDocument(form, doc);
FS_ACTION_HANDLE action;
FSDK_Action_Create(doc, e_FSTypeJavaScript, action);
FS_JAVASCRIPTACTION_HANDLE javascript_action;
FSDK_JavaScriptAction_Create(action, javascript_action);
FSDK_JavaScriptAction_SetScript(javascript_action, L"AFSimple_Calculate(\"SUM\", new Array (\"Text Field Addend\", \"Text Field Augend\"));");

FS_ADDITIONALACTION_HANDLE addition_action;
FSDK_AdditionalAction_Create1(field2, addition_action);
FSDK_AdditionalAction_SetAction(addition_action, e_FSTriggerFieldRecalculateValue, javascript_action);
//Trigger this action. Although the set value is 333333, the actual value is 26 by adding 3 to 23
FSDK_Field_SetValue(field2, L"333333");

// Update text field's appearance.
FS_WIDGETANNOT_HANDLE widget2;
FSDK_Control_GetWidget(control1, widget2);
FSDK_Annot_ResetAppearanceStream(widget2, ret);

FSDK_AdditionalAction_Release(addition_action);
FSDK_Action_Release(javascript_action);
FSDK_Widget_Release(widget2);
FSDK_Widget_Release(widget1);
FSDK_Widget_Release(widget);
FSDK_Control_Release(control2);
FSDK_Control_Release(control1);
FSDK_Control_Release(control);
FSDK_Field_Release(field2);
FSDK_Field_Release(field1);
FSDK_Field_Release(field);
FSDK_PDFDoc_Release(doc);
}

Step 2

LaunchURL: Next we will add a push button to trigger the “launchURLJavaScript to launch “https://webviewer-demo.foxit.com

C++:

//launchURL JavaScript for push button.
void AddInteractiveFormsForLaunchURL(PDFPage & page, interform::Form &form) {
// Add push button.
Control control = form.AddControl(page, L"Push Button launchURL", Field::e_TypePushButton, RectF(50, 500, 150, 520));
// Set push button appearance.
Widget widget = control.GetWidget();
widget.SetHighlightingMode(foxit::pdf::annots::Annot::e_HighlightingPush);
widget.SetMKBorderColor(0xFF0000);
widget.SetMKBackgroundColor(0xF0F0F0);
widget.SetMKNormalCaption(L"Lanuch SDK Web Viewer Demo");
widget.ResetAppearanceStream();
//Create a new JavaScript action.
actions::JavaScriptAction javascipt_action = (actions::JavaScriptAction)Action::Create(form.GetDocument(), Action::e_TypeJavaScript);
//Set Javascript
javascipt_action.SetScript(L"app.launchURL(\"https://webviewer-demo.foxit.com\", true);");
widget.SetAction(javascipt_action);
}

C#.Net and .Net Core:

//launchURL JavaScript for push button.
static void AddInteractiveFormsForLaunchURL(PDFPage page, Form form) {
// Add push button.
using(Control control = form.AddControl(page, "Push Button launchURL", Field.Type.e_TypePushButton, new RectF(50, 500, 150, 520)))
using (Widget widget = control.GetWidget())
using (PDFDoc doc = form.GetDocument()) {
// Set push button appearance.
widget.SetHighlightingMode(Annot.HighlightingMode.e_HighlightingPush);
widget.SetMKBorderColor(0xFF0000);
widget.SetMKBackgroundColor(0xF0F0F0);
widget.SetMKNormalCaption("Lanuch SDK Web Viewer Demo");
widget.ResetAppearanceStream();
//Create a new JavaScript action.
using (foxit.pdf.actions.Action action = foxit.pdf.actions.Action.Create(doc, foxit.pdf.actions.Action.Type.e_TypeJavaScript))
using (JavaScriptAction javascipt_action = new JavaScriptAction(action)) {
//Set Javascript
javascipt_action.SetScript("app.launchURL(\"https://webviewer-demo.foxit.com\", true);");
widget.SetAction(javascipt_action);
}
}
}

Standard Java:

//launchURL JavaScript for push button.
static void AddInteractiveFormsForLaunchURL(PDFPage page, Form form) {
try {
// Add push button.
Control control = form.addControl(page, "Push Button launchURL", Field.e_TypePushButton, new RectF(50, 500, 150, 520));
Widget widget = control.getWidget();
PDFDoc doc = form.getDocument();
// Set push button appearance.
widget.setHighlightingMode(Annot.e_HighlightingPush);
widget.setMKBorderColor(0xFF0000);
widget.setMKBackgroundColor(0xF0F0F0);
widget.setMKNormalCaption("Lanuch SDK Web Viewer Demo");
widget.resetAppearanceStream();
//Create a new JavaScript action.
com.foxit.sdk.pdf.actions.Action action = com.foxit.sdk.pdf.actions.Action.create(doc, com.foxit.sdk.pdf.actions.Action.e_TypeJavaScript);
JavaScriptAction javascipt_action = new JavaScriptAction(action);
//Set Javascript
javascipt_action.setScript("app.launchURL(\"https://webviewer-demo.foxit.com\", true);");
widget.setAction(javascipt_action);
} catch (PDFException e) {
e.printStackTrace();
}
}

Objective-C:

//launchURL JavaScript for push button.
void AddInteractiveFormsForLaunchURL(FSPDFPage* page, FSForm* form) {
@autoreleasepool {
// Add push button.
FSControl *control = [form addControl:page field_name:@"Push Button launchURL" field_type:FSFieldTypePushButton rect:[[FSRectF alloc] initWithLeft1:50.0 bottom1:500 right1:150 top1:520]];
// Set push button appearance.
FSWidget *widget = [control getWidget];
widget.highlightingMode = FSAnnotHighlightingPush;
widget.mKBorderColor = 0xFF0000;
widget.mKBackgroundColor = 0xF0F0F0;
widget.mKNormalCaption = @"Lanuch SDK Web Viewer Demo";
[widget resetAppearanceStream];

//Create a new JavaScript action.
FSJavaScriptAction *javascipt_action = [[FSJavaScriptAction alloc] initWithAction:[FSAction create:[form getDocument] action_type:FSActionTypeJavaScript]];
//Set Javascript
javascipt_action.script = @"app.launchURL(\"https://webviewer-demo.foxit.com\", true);";
[widget setAction:javascipt_action];
}
}

C:

//launchURL JavaScript for push button.
void AddInteractiveFormsForLaunchURL(FS_PDFPAGE_HANDLE & page, FS_FORM_HANDLE &form) {
// Add push button.
FS_CONTROL_HANDLE control;
FSRectF rect;
rect.left = 50;
rect.bottom = 500;
rect.right = 150;
rect.top = 520;
FSDK_Form_AddControl(form, page, L"Push Button launchURL", e_FSTypePushButton, rect, control);
// Set push button appearance.
FS_WIDGETANNOT_HANDLE widget;
FSDK_Control_GetWidget(control, widget);

FSDK_Widget_SetHighlightingMode(widget, e_FSHighlightingPush);
FSDK_Widget_SetMKBorderColor(widget, 0xFF0000);
FSDK_Widget_SetMKBackgroundColor(widget, 0xF0F0F0);
FSDK_Widget_SetMKNormalCaption(widget, L"Lanuch SDK Web Viewer Demo");
FS_BOOL ret = false;
FSDK_Annot_ResetAppearanceStream(widget, ret);

FS_PDFDOC_HANDLE doc;
FSDK_Form_GetDocument(form, doc);
//Create a new JavaScript action.
FS_ACTION_HANDLE action;
FSDK_Action_Create(doc, e_FSTypeJavaScript, action);
FS_JAVASCRIPTACTION_HANDLE javascript_action;
FSDK_JavaScriptAction_Create(action, javascript_action);
//Set Javascript
FSDK_JavaScriptAction_SetScript(javascript_action, L"app.launchURL(\"https://webviewer-demo.foxit.com\", true);");
FSDK_Widget_SetAction(widget, javascript_action);

FSDK_Action_Release(javascript_action);
FSDK_Widget_Release(widget);
FSDK_Control_Release(control);
FSDK_PDFDoc_Release(doc);
}

Step 3

AddField: Now we will add a push button to trigger the “addFieldJavaScript to add a password field.

C++:

//"Add a password text field" JavaScript for push button.
void AddInteractiveFormsForAddPasswordTextField(PDFPage & page, interform::Form &form) {
// Add push button.
Control control = form.AddControl(page, L"Push Button Add a password text field", Field::e_TypePushButton, RectF(50, 470, 150, 490));
// Set push button appearance.
Widget widget = control.GetWidget();
widget.SetHighlightingMode(foxit::pdf::annots::Annot::e_HighlightingPush);
widget.SetMKBorderColor(0xFF0000);
widget.SetMKBackgroundColor(0xF0F0F0);
widget.SetMKNormalCaption(L"Add Password Text Field");
widget.ResetAppearanceStream();
//Create a new JavaScript action.
actions::JavaScriptAction javascipt_action = (actions::JavaScriptAction)Action::Create(form.GetDocument(), Action::e_TypeJavaScript);
//Set Javascript
WString ws_javascript = L"var aRect = [170, 470, 270, 490] \r\n\
var f = this.addField(\"Password\", \"text\", 0, aRect) \r\n\
f.borderStyle = border.s; \r\n\
f.textSize = 10; \r\n\
f.password = true; \r\n\
f.textColor = color.blue; \r\n\
f.fillColor = color.ltGray; \r\n\
f.value = \"12345\";";
javascipt_action.SetScript(ws_javascript);
widget.SetAction(javascipt_action);
}

C#.Net and .Net Core:

//"Add a password text field" JavaScript for push button.
static void AddInteractiveFormsForAddPasswordTextField(PDFPage page, Form form) {
// Add push button.
using (Control control = form.AddControl(page, "Push Button Add a password text field", Field.Type.e_TypePushButton, new RectF(50, 470, 150, 490)))
using (Widget widget = control.GetWidget())
using (PDFDoc doc = form.GetDocument()) {
// Set push button appearance.
widget.SetHighlightingMode(Annot.HighlightingMode.e_HighlightingPush);
widget.SetMKBorderColor(0xFF0000);
widget.SetMKBackgroundColor(0xF0F0F0);
widget.SetMKNormalCaption("Add Password Text Field");
widget.ResetAppearanceStream();
//Create a new JavaScript action.
using (foxit.pdf.actions.Action action = foxit.pdf.actions.Action.Create(doc, foxit.pdf.actions.Action.Type.e_TypeJavaScript))
using (JavaScriptAction javascipt_action = new JavaScriptAction(action)) {
//Set Javascript
String ws_javascript = "var aRect = [170, 470, 270, 490] \r\n" +
"var f = this.addField(\"Password\", \"text\", 0, aRect) \r\n" +
"f.borderStyle = border.s; \r\n" +
"f.textSize = 10; \r\n" +
"f.password = true; \r\n" +
"f.textColor = color.blue; \r\n" +
"f.fillColor = color.ltGray; \r\n" +
"f.value = \"12345\";";
javascipt_action.SetScript(ws_javascript);
widget.SetAction(javascipt_action);
}
}
}

Standard Java:

//"Add a password text field" JavaScript for push button.
static void AddInteractiveFormsForAddPasswordTextField(PDFPage page, Form form) {
try {
// Add push button.
Control control = form.addControl(page, "Push Button Add a password text field", Field.e_TypePushButton, new RectF(50, 470, 150, 490));
Widget widget = control.getWidget();
PDFDoc doc = form.getDocument();
// Set push button appearance.
widget.setHighlightingMode(Annot.e_HighlightingPush);
widget.setMKBorderColor(0xFF0000);
widget.setMKBackgroundColor(0xF0F0F0);
widget.setMKNormalCaption("Add Password Text Field");
widget.resetAppearanceStream();
//Create a new JavaScript action.
com.foxit.sdk.pdf.actions.Action action = com.foxit.sdk.pdf.actions.Action.create(doc, com.foxit.sdk.pdf.actions.Action.e_TypeJavaScript);
JavaScriptAction javascipt_action = new JavaScriptAction(action);
//Set Javascript
String ws_javascript = "var aRect = [170, 470, 270, 490] \r\n" +
"var f = this.addField(\"Password\", \"text\", 0, aRect) \r\n" +
"f.borderStyle = border.s; \r\n" +
"f.textSize = 10; \r\n" +
"f.password = true; \r\n" +
"f.textColor = color.blue; \r\n" +
"f.fillColor = color.ltGray; \r\n" +
"f.value = \"12345\";";
javascipt_action.setScript(ws_javascript);
widget.setAction(javascipt_action);
} catch (PDFException e) {
e.printStackTrace();
}
}

Objective-C:

//"Add a password text field" JavaScript for push button.
void AddInteractiveFormsForAddPasswordTextField(FSPDFPage* page, FSForm* form) {
@autoreleasepool {
// Add push button.
FSControl *control = [form addControl:page field_name:@"Push Button Add a password text field" field_type:FSFieldTypePushButton rect:[[FSRectF alloc] initWithLeft1:50.0 bottom1:470 right1:150 top1:490]];
// Set push button appearance.
FSWidget *widget = [control getWidget];
widget.highlightingMode = FSAnnotHighlightingPush;
widget.mKBorderColor = 0xFF0000;
widget.mKBackgroundColor = 0xF0F0F0;
widget.mKNormalCaption = @"Add Password Text Field";
[widget resetAppearanceStream];

//Create a new JavaScript action.
FSJavaScriptAction *javascipt_action = [[FSJavaScriptAction alloc] initWithAction:[FSAction create:[form getDocument] action_type:FSActionTypeJavaScript]];
//Set Javascript
javascipt_action.script = @"var aRect = [170, 470, 270, 490]\r\nvar f = this.addField(\"Password\", \"text\", 0,aRect)\r\nf.borderStyle = border.s;\r\nf.textSize = 10;\r\nf.password = true;\r\nf.textColor = color.blue;\r\nf.fillColor = color.ltGray;\r\nf.value = \"12345\";";
[widget setAction:javascipt_action];
}
}

C:

//"Add a password text field" JavaScript for push button.
void AddInteractiveFormsForAddPasswordTextField(FS_PDFPAGE_HANDLE & page, FS_FORM_HANDLE &form) {
// Add push button.
FS_CONTROL_HANDLE control;
FSRectF rect;
rect.left = 50;
rect.bottom = 470;
rect.right = 150;
rect.top = 490;
FSDK_Form_AddControl(form, page, L"Push Button Add a password text field", e_FSTypePushButton, rect, control);
// Set push button appearance.
FS_WIDGETANNOT_HANDLE widget;
FSDK_Control_GetWidget(control, widget);

FSDK_Widget_SetHighlightingMode(widget, e_FSHighlightingPush);
FSDK_Widget_SetMKBorderColor(widget, 0xFF0000);
FSDK_Widget_SetMKBackgroundColor(widget, 0xF0F0F0);
FSDK_Widget_SetMKNormalCaption(widget, L"Add Password Text Field");
FS_BOOL ret = false;
FSDK_Annot_ResetAppearanceStream(widget, ret);

FS_PDFDOC_HANDLE doc;
FSDK_Form_GetDocument(form, doc);
//Create a new JavaScript action.
FS_ACTION_HANDLE action;
FSDK_Action_Create(doc, e_FSTypeJavaScript, action);
FS_JAVASCRIPTACTION_HANDLE javascript_action;
FSDK_JavaScriptAction_Create(action, javascript_action);
//Set Javascript
const wchar_t* ws_javascript = L"var aRect = [170, 470, 270, 490] \r\n\
var f = this.addField(\"Password\", \"text\", 0, aRect) \r\n\
f.borderStyle = border.s; \r\n\
f.textSize = 10; \r\n\
f.password = true; \r\n\
f.textColor = color.blue; \r\n\
f.fillColor = color.ltGray; \r\n\
f.value = \"12345\";";

FSDK_JavaScriptAction_SetScript(javascript_action, ws_javascript);
FSDK_Widget_SetAction(widget, javascript_action);

FSDK_Action_Release(javascript_action);
FSDK_Widget_Release(widget);
FSDK_Control_Release(control);
FSDK_PDFDoc_Release(doc);
}

Step 4

addAnnot: We will add a push button to trigger the “addAnnotJavaScript to add a note annotation.

C++:

//"Add a note annotation" JavaScript for push button.
void AddInteractiveFormsForAddNoteAnnotation(PDFPage & page, interform::Form &form) {
// Add push button.
Control control = form.AddControl(page, L"Push Button Add An Annotation", Field::e_TypePushButton, RectF(50, 440, 150, 460));
// Set push button appearance.
Widget widget = control.GetWidget();
widget.SetHighlightingMode(foxit::pdf::annots::Annot::e_HighlightingPush);
widget.SetMKBorderColor(0xFF0000);
widget.SetMKBackgroundColor(0xF0F0F0);
widget.SetMKNormalCaption(L"Add a note annotation");
widget.ResetAppearanceStream();
//Create a new JavaScript action.
actions::JavaScriptAction javascipt_action = (actions::JavaScriptAction)Action::Create(form.GetDocument(), Action::e_TypeJavaScript);
//Set Javascript
WString ws_javascript = L"var annot = this.addAnnot\r\n \
({ \r\n \
page: 0, \r\n \
type : \"Text\", \r\n \
author : \"A. C. Robat\", \r\n \
point : [300,440], \r\n \
strokeColor : color.yellow, \r\n \
contents : \"Need a little help with this paragraph.\", \r\n \
noteIcon : \"Help\" \r\n \
});";
javascipt_action.SetScript(ws_javascript);
widget.SetAction(javascipt_action);
}

C#.Net and .Net Core:

//"Add a note annotation" JavaScript for push button.
static void AddInteractiveFormsForAddNoteAnnotation(PDFPage page, Form form) {
// Add push button.
using (Control control = form.AddControl(page, "Push Button Add a annotation", Field.Type.e_TypePushButton, new RectF(50, 440, 150, 460)))
using (Widget widget = control.GetWidget())
using (PDFDoc doc = form.GetDocument()) {
// Set push button appearance.
widget.SetHighlightingMode(Annot.HighlightingMode.e_HighlightingPush);
widget.SetMKBorderColor(0xFF0000);
widget.SetMKBackgroundColor(0xF0F0F0);
widget.SetMKNormalCaption("Add a note annotation");
widget.ResetAppearanceStream();
//Create a new JavaScript action.
using (foxit.pdf.actions.Action action = foxit.pdf.actions.Action.Create(doc, foxit.pdf.actions.Action.Type.e_TypeJavaScript))
using (JavaScriptAction javascipt_action = new JavaScriptAction(action)) {
//Set Javascript
String ws_javascript = "var annot = this.addAnnot\r\n" +
"({ \r\n" +
"page: 0, \r\n" +
"type: \"Text\", \r\n" +
"author: \"A. C. Robat\", \r\n" +
"point: [300, 440], \r\n" +
"strokeColor: color.yellow, \r\n" +
"contents: \"Need a little help with this paragraph.\", \r\n" +
"noteIcon: \"Help\" \r\n" +
"}); ";
javascipt_action.SetScript(ws_javascript);
widget.SetAction(javascipt_action);
}
}
}

Standard Java:

//"Add a note annotation" JavaScript for push button.
static void AddInteractiveFormsForAddNoteAnnotation(PDFPage page, Form form) {
try {
// Add push button.
Control control = form.addControl(page, "Push Button Add a annotation", Field.e_TypePushButton, new RectF(50, 440, 150, 460));
Widget widget = control.getWidget();
PDFDoc doc = form.getDocument();
// Set push button appearance.
widget.setHighlightingMode(Annot.e_HighlightingPush);
widget.setMKBorderColor(0xFF0000);
widget.setMKBackgroundColor(0xF0F0F0);
widget.setMKNormalCaption("Add a note annotation");
widget.resetAppearanceStream();
//Create a new JavaScript action.
com.foxit.sdk.pdf.actions.Action action = com.foxit.sdk.pdf.actions.Action.create(doc, com.foxit.sdk.pdf.actions.Action.e_TypeJavaScript);
JavaScriptAction javascipt_action = new JavaScriptAction(action);
//Set Javascript
String ws_javascript = "var annot = this.addAnnot\r\n" +
"({ \r\n" +
"page: 0, \r\n" +
"type: \"Text\", \r\n" +
"author: \"A. C. Robat\", \r\n" +
"point: [300, 440], \r\n" +
"strokeColor: color.yellow, \r\n" +
"contents: \"Need a little help with this paragraph.\", \r\n" +
"noteIcon: \"Help\" \r\n" +
"}); ";
javascipt_action.setScript(ws_javascript);
widget.setAction(javascipt_action);
} catch (PDFException e) {
e.printStackTrace();
}
}

Objective-C:

//"Add a note annotation" JavaScript for push button.
void AddInteractiveFormsForAddNoteAnnotation(FSPDFPage* page, FSForm* form) {
@autoreleasepool {
// Add push button.
FSControl *control = [form addControl:page field_name:@"Push Button Add An Annotation" field_type:FSFieldTypePushButton rect:[[FSRectF alloc] initWithLeft1:50.0 bottom1:440 right1:150 top1:460]];
// Set push button appearance.
FSWidget *widget = [control getWidget];
widget.highlightingMode = FSAnnotHighlightingPush;
widget.mKBorderColor = 0xFF0000;
widget.mKBackgroundColor = 0xF0F0F0;
widget.mKNormalCaption = @"Add a note annotation";
[widget resetAppearanceStream];

//Create a new JavaScript action.
FSJavaScriptAction *javascipt_action = [[FSJavaScriptAction alloc] initWithAction:[FSAction create:[form getDocument] action_type:FSActionTypeJavaScript]];
//Set Javascript
javascipt_action.script = @"var annot = this.addAnnot\r\n ({ \r\npage: 0, \r\ntype : \"Text\", \r\nauthor : \"A. C. Robat\", \r\npoint : [300,440], \r\nstrokeColor : color.yellow, \r\ncontents : \"Need a little help with this paragraph.\", \r\nnoteIcon : \"Help\" \r\n});";
[widget setAction:javascipt_action];
}
}

C:

//"Add a note annotation" JavaScript for push button.
void AddInteractiveFormsForAddNoteAnnotation(FS_PDFPAGE_HANDLE & page, FS_FORM_HANDLE &form) {
// Add push button.
FS_CONTROL_HANDLE control;
FSRectF rect;
rect.left = 50;
rect.bottom = 440;
rect.right = 150;
rect.top = 460;
FSDK_Form_AddControl(form, page, L"Push Button Add An Annotation", e_FSTypePushButton, rect, control);
// Set push button appearance.
FS_WIDGETANNOT_HANDLE widget;
FSDK_Control_GetWidget(control, widget);

FSDK_Widget_SetHighlightingMode(widget, e_FSHighlightingPush);
FSDK_Widget_SetMKBorderColor(widget, 0xFF0000);
FSDK_Widget_SetMKBackgroundColor(widget, 0xF0F0F0);
FSDK_Widget_SetMKNormalCaption(widget, L"Add a note annotation");
FS_BOOL ret = false;
FSDK_Annot_ResetAppearanceStream(widget, ret);

FS_PDFDOC_HANDLE doc;
FSDK_Form_GetDocument(form, doc);
//Create a new JavaScript action.
FS_ACTION_HANDLE action;
FSDK_Action_Create(doc, e_FSTypeJavaScript, action);
FS_JAVASCRIPTACTION_HANDLE javascript_action;
FSDK_JavaScriptAction_Create(action, javascript_action);
//Set Javascript
const wchar_t* ws_javascript = L"var annot = this.addAnnot\r\n \
({ \r\n \
page: 0, \r\n \
type : \"Text\", \r\n \
author : \"A. C. Robat\", \r\n \
point : [300,440], \r\n \
strokeColor : color.yellow, \r\n \
contents : \"Need a little help with this paragraph.\", \r\n \
noteIcon : \"Help\" \r\n \
});";

FSDK_JavaScriptAction_SetScript(javascript_action, ws_javascript);
FSDK_Widget_SetAction(widget, javascript_action);

FSDK_Action_Release(javascript_action);
FSDK_Widget_Release(widget);
FSDK_Control_Release(control);
FSDK_PDFDoc_Release(doc);
}

Step 5

PopUpMenuEx: We will then add a push button to trigger the “popUpMenuExJavaScript to popup a menu.

C++:

//"popUpMenuEx" JavaScript for push button.
void AddInteractiveFormsForPopupMenu(PDFPage & page, interform::Form &form) {
// Add push button.
Control control = form.AddControl(page, L"Push Button Trigger a popup menu", Field::e_TypePushButton, RectF(50, 410, 150, 430));
// Set push button appearance.
Widget widget = control.GetWidget();
widget.SetHighlightingMode(foxit::pdf::annots::Annot::e_HighlightingPush);
widget.SetMKBorderColor(0xFF0000);
widget.SetMKBackgroundColor(0xF0F0F0);
widget.SetMKNormalCaption(L"Trigger a Popup Menu");
widget.ResetAppearanceStream();
//Create a new JavaScript action.
actions::JavaScriptAction javascipt_action = (actions::JavaScriptAction)Action::Create(form.GetDocument(), Action::e_TypeJavaScript);
//Set Javascript
WString ws_javascript = L"var cChoice = app.popUpMenuEx(\r\n \
{ cName: \"Item 1\", bMarked : true, bEnabled : false }, \r\n \
{ cName: \"-\" }, \r\n \
{ cName: \"Item 2\", oSubMenu : \r\n \
[{cName: \"Item 2, Submenu 1\"}, \r\n \
{ \r\n \
cName: \"Item 2, Submenu 2\", \r\n \
oSubMenu : {cName:\"Item 2, Submenu 2, Subsubmenu 1\", cReturn : \"0\"} \r\n \
} \r\n \
] \r\n \
}, \r\n \
{ cName: \"Item 3\" }, \r\n \
{ cName: \"Item 4\", bMarked : true, cReturn : \"1\" } \r\n \
) \r\n \
app.alert(\"You chose the \\\"\" + cChoice + \"\\\" menu item\");";
javascipt_action.SetScript(ws_javascript);
widget.SetAction(javascipt_action);
}

C#.Net and .Net Core:

//"popUpMenuEx" JavaScript for push button.
static void AddInteractiveFormsForPopupMenu(PDFPage page, Form form) {
// Add push button.
using (Control control = form.AddControl(page, "Push Button Trigger a popup menu", Field.Type.e_TypePushButton, new RectF(50, 410, 150, 430))) {
using (Widget widget = control.GetWidget())
using (PDFDoc doc = form.GetDocument()) {
// Set push button appearance.
widget.SetHighlightingMode(Annot.HighlightingMode.e_HighlightingPush);
widget.SetMKBorderColor(0xFF0000);
widget.SetMKBackgroundColor(0xF0F0F0);
widget.SetMKNormalCaption("Trigger a Popup Menu");
widget.ResetAppearanceStream();
//Create a new JavaScript action.
using (foxit.pdf.actions.Action action = foxit.pdf.actions.Action.Create(doc, foxit.pdf.actions.Action.Type.e_TypeJavaScript))
using (JavaScriptAction javascipt_action = new JavaScriptAction(action)) {
//Set Javascript
String ws_javascript = "var cChoice = app.popUpMenuEx(\r\n" +
"{\r\n" +
"cName: \"Item 1\", bMarked : true, bEnabled : false }, \r\n" +
"{\r\n" +
"cName: \"-\" }, \r\n" +
"{\r\n" +
"cName: \"Item 2\", oSubMenu : \r\n" +
"[{ cName: \"Item 2, Submenu 1\"}, \r\n" +
"{\r\n" +
"cName: \"Item 2, Submenu 2\", \r\n" +
"oSubMenu:\r\n" +
"{ cName:\"Item 2, Submenu 2, Subsubmenu 1\", cReturn : \"0\"} \r\n" +
"} \r\n" +
"] \r\n" +
"}, \r\n" +
"{ cName: \"Item 3\" }, \r\n" +
"{ cName: \"Item 4\", bMarked : true, cReturn : \"1\" } \r\n" +
") \r\n" +
"app.alert(\"You chose the \\\"\" + cChoice + \"\\\" menu item\");";
javascipt_action.SetScript(ws_javascript);
widget.SetAction(javascipt_action);
}
}
}
}

Standard Java:

//"popUpMenuEx" JavaScript for push button.
static void AddInteractiveFormsForPopupMenu(PDFPage page, Form form) {
try {
// Add push button.
Control control = form.addControl(page, "Push Button Trigger a popup menu", Field.e_TypePushButton, new RectF(50, 410, 150, 430));
Widget widget = control.getWidget();
PDFDoc doc = form.getDocument();
// Set push button appearance.
widget.setHighlightingMode(Annot.e_HighlightingPush);
widget.setMKBorderColor(0xFF0000);
widget.setMKBackgroundColor(0xF0F0F0);
widget.setMKNormalCaption("Trigger a Popup Menu");
widget.resetAppearanceStream();
//Create a new JavaScript action.
com.foxit.sdk.pdf.actions.Action action = com.foxit.sdk.pdf.actions.Action.create(doc, com.foxit.sdk.pdf.actions.Action.e_TypeJavaScript);
JavaScriptAction javascipt_action = new JavaScriptAction(action);
//Set Javascript
String ws_javascript = "var cChoice = app.popUpMenuEx(\r\n" +
"{\r\n" +
"cName: \"Item 1\", bMarked : true, bEnabled : false }, \r\n" +
"{\r\n" +
"cName: \"-\" }, \r\n" +
"{\r\n" +
"cName: \"Item 2\", oSubMenu : \r\n" +
"[{ cName: \"Item 2, Submenu 1\"}, \r\n" +
"{\r\n" +
"cName: \"Item 2, Submenu 2\", \r\n" +
"oSubMenu:\r\n" +
"{ cName:\"Item 2, Submenu 2, Subsubmenu 1\", cReturn : \"0\"} \r\n" +
"} \r\n" +
"] \r\n" +
"}, \r\n" +
"{ cName: \"Item 3\" }, \r\n" +
"{ cName: \"Item 4\", bMarked : true, cReturn : \"1\" } \r\n" +
") \r\n" +
"app.alert(\"You chose the \\\"\" + cChoice + \"\\\" menu item\");";
javascipt_action.setScript(ws_javascript);
widget.setAction(javascipt_action);
} catch (PDFException e) {
e.printStackTrace();
}
}

Objective-C:

//"popUpMenuEx" JavaScript for push button.
void AddInteractiveFormsForPopupMenu(FSPDFPage* page, FSForm* form) {
@autoreleasepool {
// Add push button.
FSControl *control = [form addControl:page field_name:@"Push Button Trigger a popup menu" field_type:FSFieldTypePushButton rect:[[FSRectF alloc] initWithLeft1:50.0 bottom1:410 right1:150 top1:430]];
// Set push button appearance.
FSWidget *widget = [control getWidget];
widget.highlightingMode = FSAnnotHighlightingPush;
widget.mKBorderColor = 0xFF0000;
widget.mKBackgroundColor = 0xF0F0F0;
widget.mKNormalCaption = @"Trigger a Popup Menu";
[widget resetAppearanceStream];

//Create a new JavaScript action.
FSJavaScriptAction *javascipt_action = [[FSJavaScriptAction alloc] initWithAction:[FSAction create:[form getDocument] action_type:FSActionTypeJavaScript]];
//Set Javascript
javascipt_action.script = @"var cChoice = app.popUpMenuEx(\r\n{ cName: \"Item 1\", bMarked : true, bEnabled : false }, \r\n{ cName: \"-\" }, \r\n{ cName: \"Item 2\", oSubMenu : \r\n[{cName: \"Item 2, Submenu 1\"}, \r\n{ \r\ncName: \"Item 2, Submenu 2\", \r\n oSubMenu : {cName:\"Item 2, Submenu 2, Subsubmenu 1\", cReturn : \"0\"} \r\n} \r\n] \r\n}, \r\n{ cName: \"Item 3\" }, \r\n{ cName: \"Item 4\", bMarked : true, cReturn : \"1\" } \r\n) \r\napp.alert(\"You chose the \\\"\" + cChoice + \"\\\" menu item\");";
[widget setAction:javascipt_action];
}
}

C:

//"popUpMenuEx" JavaScript for push button.
void AddInteractiveFormsForPopupMenu(FS_PDFPAGE_HANDLE & page, FS_FORM_HANDLE &form) {
// Add push button.
FS_CONTROL_HANDLE control;
FSRectF rect;
rect.left = 50;
rect.bottom = 410;
rect.right = 150;
rect.top = 430;
FSDK_Form_AddControl(form, page, L"Push Button Trigger a popup menu", e_FSTypePushButton, rect, control);
// Set push button appearance.
FS_WIDGETANNOT_HANDLE widget;
FSDK_Control_GetWidget(control, widget);

FSDK_Widget_SetHighlightingMode(widget, e_FSHighlightingPush);
FSDK_Widget_SetMKBorderColor(widget, 0xFF0000);
FSDK_Widget_SetMKBackgroundColor(widget, 0xF0F0F0);
FSDK_Widget_SetMKNormalCaption(widget, L"Trigger a Popup Menu");
FS_BOOL ret = false;
FSDK_Annot_ResetAppearanceStream(widget, ret);

FS_PDFDOC_HANDLE doc;
FSDK_Form_GetDocument(form, doc);
//Create a new JavaScript action.
FS_ACTION_HANDLE action;
FSDK_Action_Create(doc, e_FSTypeJavaScript, action);
FS_JAVASCRIPTACTION_HANDLE javascript_action;
FSDK_JavaScriptAction_Create(action, javascript_action);
//Set Javascript
const wchar_t* ws_javascript = L"var cChoice = app.popUpMenuEx(\r\n \
{ cName: \"Item 1\", bMarked : true, bEnabled : false }, \r\n \
{ cName: \"-\" }, \r\n \
{ cName: \"Item 2\", oSubMenu : \r\n \
[{cName: \"Item 2, Submenu 1\"}, \r\n \
{ \r\n \
cName: \"Item 2, Submenu 2\", \r\n \
oSubMenu : {cName:\"Item 2, Submenu 2, Subsubmenu 1\", cReturn : \"0\"} \r\n \
} \r\n \
] \r\n \
}, \r\n \
{ cName: \"Item 3\" }, \r\n \
{ cName: \"Item 4\", bMarked : true, cReturn : \"1\" } \r\n \
) \r\n \
app.alert(\"You chose the \\\"\" + cChoice + \"\\\" menu item\");";

FSDK_JavaScriptAction_SetScript(javascript_action, ws_javascript);
FSDK_Widget_SetAction(widget, javascript_action);

FSDK_Action_Release(javascript_action);
FSDK_Widget_Release(widget);
FSDK_Control_Release(control);
FSDK_PDFDoc_Release(doc);
}

Step 6

Add a submit action to a push button: We will now add a push button to trigger the “submit action” to submit data to “http://www.foxit.com“.

C++:

//Submit action for push button.
void AddInteractiveFormsForSubmitAction(PDFPage & page, interform::Form &form) {
// Add push button field.
Control btn_submit = form.AddControl(page, L"Push Button Submit", Field::e_TypePushButton, RectF(50, 380, 150, 400));

// Set default appearance
foxit::pdf::DefaultAppearance default_ap;
default_ap.flags = DefaultAppearance::e_FlagFont | DefaultAppearance::e_FlagFontSize | DefaultAppearance::e_FlagTextColor;
default_ap.font = Font(Font::e_StdIDHelveticaB);
default_ap.text_size = 12.0f;
default_ap.text_color = 0x000000;
form.SetDefaultAppearance(default_ap);

// Set push button appearance.
Widget widget = btn_submit.GetWidget();
widget.SetHighlightingMode(foxit::pdf::annots::Annot::e_HighlightingPush);
widget.SetMKBorderColor(0xFF0000);
widget.SetMKBackgroundColor(0xF0F0F0);
widget.SetMKNormalCaption(L"Submit");
widget.ResetAppearanceStream();

// Set submit form action.
actions::SubmitFormAction submit_action = (actions::SubmitFormAction)Action::Create(form.GetDocument(), Action::e_TypeSubmitForm);
int count = form.GetFieldCount(NULL);
WStringArray name_array;
for (int i = 0; i < count; i++)
name_array.Add(form.GetField(i,NULL).GetName());

submit_action.SetFieldNames(name_array);
submit_action.SetURL("http://www.foxit.com");
widget.SetAction(submit_action);
}

C#.Net and .Net Core:

//Sstatic void AddInteractiveFormsForSubmitAction(PDFPage page, Form form) {
// Add push button field.
using (Control btn_submit = form.AddControl(page, "Push Button Submit", Field.Type.e_TypePushButton, new RectF(50, 380, 150, 400)))
// Set default appearance
using (DefaultAppearance default_ap = new DefaultAppearance())
{
default_ap.flags = (int)(DefaultAppearance.DefAPFlags.e_FlagFont | DefaultAppearance.DefAPFlags.e_FlagFontSize | DefaultAppearance.DefAPFlags.e_FlagTextColor);
using (default_ap.font = new Font(Font.StandardID.e_StdIDHelveticaB))
{
default_ap.text_size = 12.0f;
default_ap.text_color = 0x000000;
form.SetDefaultAppearance(default_ap);
}

// Set push button appearance.
using (Widget widget = btn_submit.GetWidget())
{
widget.SetHighlightingMode(Annot.HighlightingMode.e_HighlightingPush);
widget.SetMKBorderColor(0xFF0000);
widget.SetMKBackgroundColor(0xF0F0F0);
widget.SetMKNormalCaption("Submit");
widget.ResetAppearanceStream();

// Set submit form action.
using (PDFDoc doc = form.GetDocument())
{
using (foxit.pdf.actions.Action action = foxit.pdf.actions.Action.Create(doc, foxit.pdf.actions.Action.Type.e_TypeSubmitForm))
using (SubmitFormAction submit_action = new SubmitFormAction(action))
{
int count = form.GetFieldCount(null);
WStringArray name_array = new WStringArray();
for (int i = 0; i < count; i++)
{
Field field = form.GetField(i, null);
using (field)
{
name_array.Add(field.GetName());
}
}

submit_action.SetFieldNames(name_array);
submit_action.SetURL("http://www.foxit.com");
widget.SetAction(submit_action);
widget.ResetAppearanceStream();
}
}
}
}
}

Standard Java:

//Submit action for push button.
static void AddInteractiveFormsForSubmitAction(PDFPage page, Form form) {
try {
// Add push button field.
Control btn_submit = form.addControl(page, "Push Button Submit", Field.e_TypePushButton, new RectF(50, 380, 150, 400));
// Set default appearance
DefaultAppearance default_ap = new DefaultAppearance();
default_ap.setFlags((int)(DefaultAppearance.e_FlagFont | DefaultAppearance.e_FlagFontSize | DefaultAppearance.e_FlagTextColor));
default_ap.setFont(new Font(Font.e_StdIDHelveticaB));
default_ap.setText_size(12.0f);
default_ap.setText_color(0x000000);
form.setDefaultAppearance(default_ap);

// Set push button appearance.
Widget widget = btn_submit.getWidget();
widget.setHighlightingMode(Annot.e_HighlightingPush);
widget.setMKBorderColor(0xFF0000);
widget.setMKBackgroundColor(0xF0F0F0);
widget.setMKNormalCaption("Submit");
widget.resetAppearanceStream();

// Set submit form action.
PDFDoc doc = form.getDocument();
com.foxit.sdk.pdf.actions.Action action = com.foxit.sdk.pdf.actions.Action.create(doc, com.foxit.sdk.pdf.actions.Action.e_TypeSubmitForm);
SubmitFormAction submit_action = new SubmitFormAction(action);
int count = form.getFieldCount(null);
WStringArray name_array = new WStringArray();
for (int i = 0; i < count; i++) {
Field field = form.getField(i, null);
name_array.add(field.getName());
}

submit_action.setFieldNames(name_array);
submit_action.setURL("http://www.foxit.com");
widget.setAction(submit_action);
widget.resetAppearanceStream();
} catch (PDFException e) {
e.printStackTrace();
}
}

Objective-C:

//Submit action for push button.
void AddInteractiveFormsForSubmitAction(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:380 right1:150 top1:400]];

// 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.foxit.com";
widget.action = submit_action;
}
}

C:

//Submit action for push button.
void AddInteractiveFormsForSubmitAction(FS_PDFPAGE_HANDLE & page, FS_FORM_HANDLE &form) {
// Add push button.
FS_CONTROL_HANDLE btn_submit;
FSRectF rect;
rect.left = 50;
rect.bottom = 380;
rect.right = 150;
rect.top = 400;
FSDK_Form_AddControl(form, page, L"Push Button Submit", e_FSTypePushButton, rect, btn_submit);

// Set default appearance
FSDefaultAppearance default_ap;
default_ap.flags = e_FSFlagFont | e_FSFlagFontSize | e_FSFlagTextColor;
FS_FONT_HANDLE font;
FSDK_Font_Create0(e_FSStdIDHelveticaB, font);
default_ap.font = font;
default_ap.text_size = 12.0f;
default_ap.text_color = 0x000000;
FSDK_Form_SetDefaultAppearance(form, default_ap);

// Set push button appearance.
FS_WIDGETANNOT_HANDLE widget;
FSDK_Control_GetWidget(btn_submit, widget);
FSDK_Widget_SetHighlightingMode(widget, e_FSHighlightingPush);
FSDK_Widget_SetMKBorderColor(widget, 0xFF0000);
FSDK_Widget_SetMKBackgroundColor(widget, 0xF0F0F0);
FSDK_Widget_SetMKNormalCaption(widget, L"Submit");
FS_BOOL ret = false;
FSDK_Annot_ResetAppearanceStream(widget, ret);

FS_PDFDOC_HANDLE doc;
FSDK_Form_GetDocument(form, doc);
// Set submit form action.
FS_ACTION_HANDLE action;
FSDK_Action_Create(doc, e_FSTypeSubmitForm, action);
FS_SUBMITFORMACTION_HANDLE submit_action;
FSDK_SubmitFormAction_Create(action, submit_action);

int count = 0;
FSDK_Form_GetFieldCount(form, L"", count);
FS_WSTR* name_array = new FS_WSTR[count];
for (int i = 0; i < count; i++) {
FS_FIELD_HANDLE field;
FSDK_Form_GetField(form, i, NULL, field);
FSDK_Field_GetName(field, name_array[i]);
}

FSDK_SubmitFormAction_SetFieldNames(submit_action, name_array, count);
FSDK_SubmitFormAction_SetURL(submit_action, "http://www.foxit.com");
FSDK_Widget_SetAction(widget, submit_action);
delete[]name_array;

FSDK_Font_Release(font);
FSDK_Action_Release(submit_action);
FSDK_Widget_Release(widget);
FSDK_PDFDoc_Release(doc);
}

Updated on April 29, 2022

Was this article helpful?
Thanks for your feedback. If you have a comment on how to improve the article, you can write it here: