How to Create your own JavaScript for Annotations
This article demonstrates how to use JavaScript for annotations so you can get the most out of Foxit PDF SDK. Read on below for the 4 simple steps to get started.
Platform: Windows, Mac, Linux
Programming Language: C++, Java, C#, Objective-C, C
License Key requirement: Standard
SDK Version: Foxit PDF SDK 8.3
Step 1
LaunchURL: We will start by adding a link annotation to trigger the “launchURL” JavaScript to launch “https://webviewer-demo.foxit.com”
C++:
//launchURL JavaScript for link annotation. void AddLinkAnnotForLaunchURL(PDFPage & page) { page.AddText(L"https://webviewer-demo.foxit.com", RectF(50, 600, 200, 620), RichTextStyle()); page.GenerateContent(); // Add link annotation Link link = (Link)page.AddAnnot(foxit::pdf::annots::Annot::e_Link, RectF(50, 600, 220, 620)); //This flag is used for printing annotations. link.SetFlags(4); link.SetHighlightingMode(Annot::e_HighlightingInvert); // Appearance should be reset. link.ResetAppearanceStream(); // Create a new JavaScript action. actions::JavaScriptAction javascipt_action = (actions::JavaScriptAction)Action::Create(page.GetDocument(), Action::e_TypeJavaScript); // Set Javascript javascipt_action.SetScript(L"app.launchURL(\"https://webviewer-demo.foxit.com\", true);"); // Set action for link annotation link.SetAction(javascipt_action); }
C#.Net and .Net Core:
//launchURL JavaScript for link annotation. static void AddLinkAnnotForLaunchURL(PDFPage page) { page.AddText("Add a password text field", new RectF(50, 600, 200, 620), new RichTextStyle()); page.GenerateContent(); // Add link annotation. using (Annot annot = page.AddAnnot(Annot.Type.e_Link, new RectF(50, 600, 220, 620))) using (Link link = new Link(annot)) using (PDFDoc doc = page.GetDocument()) { //This flag is used for printing annotations. link.SetFlags(4); link.SetHighlightingMode(Link.HighlightingMode.e_HighlightingInvert); // Appearance should be reset. link.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);"); link.SetAction(javascipt_action); } } }
Standard Java:
//launchURL JavaScript for link annotation. static void AddLinkAnnotForLaunchURL(PDFPage page) { try { page.addText("https://webviewer-demo.foxit.com", new RectF(50, 600, 200, 620), new RichTextStyle()); page.generateContent(); // Add link annotation Annot annot = page.addAnnot(Annot.e_Link, new RectF(50, 600, 220, 620)); Link link = new Link(annot); //This flag is used for printing annotations. link.setFlags(4); link.setHighlightingMode(Annot.e_HighlightingInvert); // Appearance should be reset. link.resetAppearanceStream(); //Create a new JavaScript action. com.foxit.sdk.pdf.actions.Action action = com.foxit.sdk.pdf.actions.Action.create(page.getDocument(), 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);"); link.setAction(javascipt_action); } catch (PDFException e) { e.printStackTrace(); } }
Objective-C:
//launchURL JavaScript for link annotation. void AddLinkAnnotForLaunchURL(FSPDFPage* page) { @autoreleasepool { FSRichTextStyle* richtext_style = [[FSRichTextStyle alloc] init]; FSFont* rcstyle_font_1 = [[FSFont alloc] initWithName:@"Times New Roman" styles:0 charset:FSFontCharsetANSI weight:0]; [richtext_style setFont:rcstyle_font_1]; [richtext_style setText_color:0xFF0000]; [richtext_style setText_size:10]; [page addText:@"https://webviewer-demo.foxit.com" rect:[[FSRectF alloc] initWithLeft1:50.0 bottom1:600 right1:200 top1:620] style:richtext_style]; [page generateContent]; // Add link annotation. FSRectF* annot_rect = [[FSRectF alloc] initWithLeft1:50 bottom1:600 right1:220 top1:620]; FSLink* link = [[FSLink alloc] initWithAnnot:[page addAnnot:FSAnnotLink rect:annot_rect]]; //This flag is used for printing annotations. [link setFlags:4]; [link setHighlightingMode:FSAnnotHighlightingInvert]; [link resetAppearanceStream]; //Create a new JavaScript action. FSJavaScriptAction *javascipt_action = [[FSJavaScriptAction alloc] initWithAction:[FSAction create:[page getDocument] action_type:FSActionTypeJavaScript]]; //Set Javascript javascipt_action.script = @"app.launchURL(\"https://webviewer-demo.foxit.com\", true);"; [link setAction:javascipt_action]; } }
C:
//launchURL JavaScript for link annotation. void AddLinkAnnotForLaunchURL(FS_PDFPAGE_HANDLE & page) { FSRichTextStyle richtextstyle; FS_FONT_HANDLE font0; FSDK_Font_Create(L"Times New Roman", 0, e_FSCharsetANSI, 0, font0); richtextstyle.font = font0; richtextstyle.text_color = 0xFF0000; richtextstyle.text_alignment = e_FSAlignmentLeft; richtextstyle.mark_style = e_FSCornerMarkNone; richtextstyle.text_size = 10; richtextstyle.is_bold = 0; richtextstyle.is_italic = 0; richtextstyle.is_strikethrough = 0; richtextstyle.is_underline = 0; FSRectF rect; rect.left = 50; rect.bottom = 600; rect.right = 200; rect.top = 620; FS_BOOL ret = false; FSDK_PDFPage_AddText(page, L"https://webviewer-demo.foxit.com", rect, richtextstyle, ret); SDK_GraphicsObjects_GenerateContent(page, ret); // Add link annotation FSRectF rectf; rectf.left = 50; rectf.bottom = 600; rectf.right = 220; rectf.top = 620; FS_ANNOT_HANDLE annot; FSDK_PDFPage_AddAnnot(page, e_FSLink, rectf, annot); FS_ANNOT_HANDLE link; FSDK_Link_Create0(annot, link); //This flag is used for printing annotations. FSDK_Annot_SetFlags(link, 4); FSDK_Link_SetHighlightingMode(link, e_FSHighlightingInvert); FSDK_Annot_ResetAppearanceStream(link, ret); FS_PDFDOC_HANDLE doc; FSDK_PDFPage_GetDocument(page, 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_Link_SetAction(link, javascript_action); FSDK_Action_Release(javascript_action); FSDK_Link_Release(link); FSDK_PDFDoc_Release(doc); }
Step 2
AddField: We will now add a push button to trigger the “addField” JavaScript to add a password field.
C++:
//"Add a password text field" JavaScript for link annotation. void AddLinkAnnotForAddPasswordTextField(PDFPage & page) { page.AddText(L"Add a password text field", RectF(50, 570, 200, 590), RichTextStyle()); page.GenerateContent(); // Add link annotation Link link = (Link)page.AddAnnot(foxit::pdf::annots::Annot::e_Link, RectF(50, 570, 220, 590)); //This flag is used for printing annotations. link.SetFlags(4); link.SetHighlightingMode(Annot::e_HighlightingInvert); // Appearance should be reset. link.ResetAppearanceStream(); // Create a new JavaScript action. actions::JavaScriptAction javascipt_action = (actions::JavaScriptAction)Action::Create(page.GetDocument(), Action::e_TypeJavaScript); // Set Javascript WString ws_javascript = L"var aRect = [240, 570, 340, 590] \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); // Set action for link annotation link.SetAction(javascipt_action); }
C#.Net and .Net Core:
//"Add a password text field" JavaScript for link annotation. static void AddLinkAnnotForAddPasswordTextField(PDFPage page) { page.AddText("Add a password text field", new RectF(50, 570, 200, 590), new RichTextStyle()); page.GenerateContent(); // Add link annotation. using (Annot annot = page.AddAnnot(Annot.Type.e_Link, new RectF(50, 570, 220, 590))) using (Link link = new Link(annot)) using (PDFDoc doc = page.GetDocument()) { //This flag is used for printing annotations. link.SetFlags(4); link.SetHighlightingMode(Link.HighlightingMode.e_HighlightingInvert); // Appearance should be reset. link.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 = [240, 570, 340, 590] \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); link.SetAction(javascipt_action); } } }
Standard Java:
//"Add a password text field" JavaScript for link annotation. static void AddLinkAnnotForAddPasswordTextField(PDFPage page) { try { page.addText("Add a password text field", new RectF(50, 570, 200, 590), new RichTextStyle()); page.generateContent(); // Add link annotation Annot annot = page.addAnnot(Annot.e_Link, new RectF(50, 570, 220, 590)); Link link = new Link(annot); //This flag is used for printing annotations. link.setFlags(4); link.setHighlightingMode(Annot.e_HighlightingInvert); // Appearance should be reset. link.resetAppearanceStream(); //Create a new JavaScript action. com.foxit.sdk.pdf.actions.Action action = com.foxit.sdk.pdf.actions.Action.create(page.getDocument(), com.foxit.sdk.pdf.actions.Action.e_TypeJavaScript); JavaScriptAction javascipt_action = new JavaScriptAction(action); //Set Javascript String ws_javascript = "var aRect = [240, 570, 340, 590] \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); link.setAction(javascipt_action); } catch (PDFException e) { e.printStackTrace(); } }
Objective-C:
//"Add a password text field" JavaScript for link annotation. void AddLinkAnnotForAddPasswordTextField(FSPDFPage* page) { @autoreleasepool { FSRichTextStyle* richtext_style = [[FSRichTextStyle alloc] init]; FSFont* rcstyle_font_1 = [[FSFont alloc] initWithName:@"Times New Roman" styles:0 charset:FSFontCharsetANSI weight:0]; [richtext_style setFont:rcstyle_font_1]; [richtext_style setText_color:0xFF0000]; [richtext_style setText_size:10]; [page addText:@"Add a password text field" rect:[[FSRectF alloc] initWithLeft1:50.0 bottom1:570 right1:200 top1:590] style:richtext_style]; [page generateContent]; // Add link annotation. FSRectF* annot_rect = [[FSRectF alloc] initWithLeft1:50 bottom1:570 right1:220 top1:590]; FSLink* link = [[FSLink alloc] initWithAnnot:[page addAnnot:FSAnnotLink rect:annot_rect]]; //This flag is used for printing annotations. [link setFlags:4]; [link setHighlightingMode:FSAnnotHighlightingInvert]; [link resetAppearanceStream]; //Create a new JavaScript action. FSJavaScriptAction *javascipt_action = [[FSJavaScriptAction alloc] initWithAction:[FSAction create:[page getDocument] action_type:FSActionTypeJavaScript]]; //Set Javascript javascipt_action.script = @"var aRect = [240, 570, 340, 590]\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\";"; [link setAction:javascipt_action]; } }
C:
//"Add a password text field" JavaScript for link annotation. void AddLinkAnnotForAddPasswordTextField(FS_PDFPAGE_HANDLE & page) { FSRichTextStyle richtextstyle; FS_FONT_HANDLE font0; FSDK_Font_Create(L"Times New Roman", 0, e_FSCharsetANSI, 0, font0); richtextstyle.font = font0; richtextstyle.text_color = 0xFF0000; richtextstyle.text_alignment = e_FSAlignmentLeft; richtextstyle.mark_style = e_FSCornerMarkNone; richtextstyle.text_size = 10; richtextstyle.is_bold = 0; richtextstyle.is_italic = 0; richtextstyle.is_strikethrough = 0; richtextstyle.is_underline = 0; FSRectF rect; rect.left = 50; rect.bottom = 570; rect.right = 200; rect.top = 590; FS_BOOL ret = false; FSDK_PDFPage_AddText(page, L"Add a password text field", rect, richtextstyle, ret); FSDK_GraphicsObjects_GenerateContent(page, ret); // Add link annotation FSRectF rectf; rectf.left = 50; rectf.bottom = 570; rectf.right = 220; rectf.top = 590; FS_ANNOT_HANDLE annot; FSDK_PDFPage_AddAnnot(page, e_FSLink, rectf, annot); FS_ANNOT_HANDLE link; FSDK_Link_Create0(annot, link); //This flag is used for printing annotations. FSDK_Annot_SetFlags(link, 4); FSDK_Link_SetHighlightingMode(link, e_FSHighlightingInvert); FSDK_Annot_ResetAppearanceStream(link, ret); FS_PDFDOC_HANDLE doc; FSDK_PDFPage_GetDocument(page, 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 = [240, 570, 340, 590] \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_Link_SetAction(link, javascript_action); FSDK_Action_Release(javascript_action); FSDK_Link_Release(link); FSDK_PDFDoc_Release(doc); }
Step 3
AddAnnot: We will then add a push button to trigger the “addAnnot” JavaScript to add a note annotation.
C++:
//"Add a note annotation" JavaScript for link annotation. void AddLinkAnnotForAddNoteAnnotation(PDFPage & page) { page.AddText(L"Add a note annotation", RectF(50, 540, 200, 560), RichTextStyle()); page.GenerateContent(); // Add link annotation Link link = (Link)page.AddAnnot(foxit::pdf::annots::Annot::e_Link, RectF(50, 540, 220, 560)); //This flag is used for printing annotations. link.SetFlags(4); link.SetHighlightingMode(Annot::e_HighlightingInvert); // Appearance should be reset. link.ResetAppearanceStream(); // Create a new JavaScript action. actions::JavaScriptAction javascipt_action = (actions::JavaScriptAction)Action::Create(page.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,540], \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); // Set action for link annotation link.SetAction(javascipt_action); }
C#.Net and .Net Core:
//"Add a note annotation" JavaScript for link annotation. static void AddLinkAnnotForAddNoteAnnotation(PDFPage page) { page.AddText("Add a note annotation", new RectF(50, 540, 200, 560), new RichTextStyle()); page.GenerateContent(); // Add link annotation. using (Annot annot = page.AddAnnot(Annot.Type.e_Link, new RectF(50, 540, 220, 560))) using (Link link = new Link(annot)) using (PDFDoc doc = page.GetDocument()) { //This flag is used for printing annotations. link.SetFlags(4); link.SetHighlightingMode(Link.HighlightingMode.e_HighlightingInvert); // Appearance should be reset. link.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, 540], \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); link.SetAction(javascipt_action); } } }
Standard Java:
//"Add a note annotation" JavaScript for link annotation. static void AddLinkAnnotForAddNoteAnnotation(PDFPage page) { try { page.addText("Add a note annotation", new RectF(50, 540, 200, 560), new RichTextStyle()); page.generateContent(); // Add link annotation Annot annot = page.addAnnot(Annot.e_Link, new RectF(50, 540, 220, 560)); Link link = new Link(annot); //This flag is used for printing annotations. link.setFlags(4); link.setHighlightingMode(Annot.e_HighlightingInvert); // Appearance should be reset. link.resetAppearanceStream(); //Create a new JavaScript action. com.foxit.sdk.pdf.actions.Action action = com.foxit.sdk.pdf.actions.Action.create(page.getDocument(), 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, 540], \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); link.setAction(javascipt_action); } catch (PDFException e) { e.printStackTrace(); } }
Objective-C:
//"Add a note annotation" JavaScript for link annotation. void AddLinkAnnotForAddNoteAnnotation(FSPDFPage* page) { @autoreleasepool { FSRichTextStyle* richtext_style = [[FSRichTextStyle alloc] init]; FSFont* rcstyle_font_1 = [[FSFont alloc] initWithName:@"Times New Roman" styles:0 charset:FSFontCharsetANSI weight:0]; [richtext_style setFont:rcstyle_font_1]; [richtext_style setText_color:0xFF0000]; [richtext_style setText_size:10]; [page addText:@"Add a note annotation" rect:[[FSRectF alloc] initWithLeft1:50.0 bottom1:540 right1:200 top1:560] style:richtext_style]; [page generateContent]; // Add link annotation. FSRectF* annot_rect = [[FSRectF alloc] initWithLeft1:50 bottom1:540 right1:220 top1:560]; FSLink* link = [[FSLink alloc] initWithAnnot:[page addAnnot:FSAnnotLink rect:annot_rect]]; //This flag is used for printing annotations. [link setFlags:4]; [link setHighlightingMode:FSAnnotHighlightingInvert]; [link resetAppearanceStream]; //Create a new JavaScript action. FSJavaScriptAction *javascipt_action = [[FSJavaScriptAction alloc] initWithAction:[FSAction create:[page 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,540], \r\nstrokeColor : color.yellow, \r\ncontents : \"Need a little help with this paragraph.\", \r\nnoteIcon : \"Help\" \r\n});"; [link setAction:javascipt_action]; } }
C:
//"Add a note annotation" JavaScript for link annotation. void AddLinkAnnotForAddNoteAnnotation(FS_PDFPAGE_HANDLE & page) { FSRichTextStyle richtextstyle; FS_FONT_HANDLE font0; FSDK_Font_Create(L"Times New Roman", 0, e_FSCharsetANSI, 0, font0); richtextstyle.font = font0; richtextstyle.text_color = 0xFF0000; richtextstyle.text_alignment = e_FSAlignmentLeft; richtextstyle.mark_style = e_FSCornerMarkNone; richtextstyle.text_size = 10; richtextstyle.is_bold = 0; richtextstyle.is_italic = 0; richtextstyle.is_strikethrough = 0; richtextstyle.is_underline = 0; FSRectF rect; rect.left = 50; rect.bottom = 540; rect.right = 200; rect.top = 560; FS_BOOL ret = false; FSDK_PDFPage_AddText(page, L"Add a note annotation", rect, richtextstyle, ret); FSDK_GraphicsObjects_GenerateContent(page, ret); // Add link annotation FSRectF rectf; rectf.left = 50; rectf.bottom = 540; rectf.right = 220; rectf.top = 560; FS_ANNOT_HANDLE annot; FSDK_PDFPage_AddAnnot(page, e_FSLink, rectf, annot); FS_ANNOT_HANDLE link; FSDK_Link_Create0(annot, link); //This flag is used for printing annotations. FSDK_Annot_SetFlags(link, 4); FSDK_Link_SetHighlightingMode(link, e_FSHighlightingInvert); FSDK_Annot_ResetAppearanceStream(link, ret); FS_PDFDOC_HANDLE doc; FSDK_PDFPage_GetDocument(page, 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,540], \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_Link_SetAction(link, javascript_action); FSDK_Action_Release(javascript_action); FSDK_Link_Release(link); FSDK_PDFDoc_Release(doc); }
Step 4
PopUpMenuEx: We will add a push button to trigger the “popUpMenuEx” JavaScript to popup a menu.
C++:
//"popUpMenuEx" JavaScript for link annotation. void AddLinkAnnotForPopupMenu(PDFPage & page) { page.AddText(L"Trigger a popup menu", RectF(50, 510, 200, 530), RichTextStyle()); page.GenerateContent(); // Add link annotation Link link = (Link)page.AddAnnot(foxit::pdf::annots::Annot::e_Link, RectF(50, 510, 220, 530)); //This flag is used for printing annotations. link.SetFlags(4); link.SetHighlightingMode(Annot::e_HighlightingInvert); // Appearance should be reset. link.ResetAppearanceStream(); // Create a new JavaScript action. actions::JavaScriptAction javascipt_action = (actions::JavaScriptAction)Action::Create(page.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); // Set action for link annotation link.SetAction(javascipt_action); }
C#.Net and .Net Core:
//"popUpMenuEx" JavaScript for link annotation. static void AddLinkAnnotForPopupMenu(PDFPage page) { page.AddText("Trigger a popup menu", new RectF(50, 510, 200, 530), new RichTextStyle()); page.GenerateContent(); // Add link annotation. using (Annot annot = page.AddAnnot(Annot.Type.e_Link, new RectF(50, 510, 220, 530))) using (Link link = new Link(annot)) using (PDFDoc doc = page.GetDocument()) { //This flag is used for printing annotations. link.SetFlags(4); link.SetHighlightingMode(Link.HighlightingMode.e_HighlightingInvert); // Appearance should be reset. link.ResetAppearanceStream(); //Create a new JavaScript action. using (foxit.pdf.actions.Action action = foxit.pdf.actions.Action.Create(doc, oxit.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); link.SetAction(javascipt_action); } } }
Standard Java:
//"popUpMenuEx" JavaScript for link annotation. static void AddLinkAnnotForPopupMenu(PDFPage page) { try { page.addText("Trigger a popup menu", new RectF(50, 510, 200, 530), new RichTextStyle()); page.generateContent(); // Add link annotation Annot annot = page.addAnnot(Annot.e_Link, new RectF(50, 510, 220, 530)); Link link = new Link(annot); //This flag is used for printing annotations. link.setFlags(4); link.setHighlightingMode(Annot.e_HighlightingInvert); // Appearance should be reset. link.resetAppearanceStream(); //Create a new JavaScript action. com.foxit.sdk.pdf.actions.Action action = com.foxit.sdk.pdf.actions.Action.create(page.getDocument(), 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); link.setAction(javascipt_action); } catch (PDFException e) { e.printStackTrace(); } }
Objective-C:
//"popUpMenuEx" JavaScript for link annotation. void AddLinkAnnotForPopupMenu(FSPDFPage* page) { @autoreleasepool { FSRichTextStyle* richtext_style = [[FSRichTextStyle alloc] init]; FSFont* rcstyle_font_1 = [[FSFont alloc] initWithName:@"Times New Roman" styles:0 charset:FSFontCharsetANSI weight:0]; [richtext_style setFont:rcstyle_font_1]; [richtext_style setText_color:0xFF0000]; [richtext_style setText_size:10]; [page addText:@"Trigger a popup menu" rect:[[FSRectF alloc] initWithLeft1:50.0 bottom1:510 right1:200 top1:530] style:richtext_style]; [page generateContent]; // Add link annotation. FSRectF* annot_rect = [[FSRectF alloc] initWithLeft1:50 bottom1:510 right1:220 top1:530]; FSLink* link = [[FSLink alloc] initWithAnnot:[page addAnnot:FSAnnotLink rect:annot_rect]]; //This flag is used for printing annotations. [link setFlags:4]; [link setHighlightingMode:FSAnnotHighlightingInvert]; [link resetAppearanceStream]; //Create a new JavaScript action. FSJavaScriptAction *javascipt_action = [[FSJavaScriptAction alloc] initWithAction:[FSAction create:[page 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\");"; [link setAction:javascipt_action]; } }
C:
//"popUpMenuEx" JavaScript for link annotation. void AddLinkAnnotForPopupMenu(FS_PDFPAGE_HANDLE & page) { FSRichTextStyle richtextstyle; FS_FONT_HANDLE font0; FSDK_Font_Create(L"Times New Roman", 0, e_FSCharsetANSI, 0, font0); richtextstyle.font = font0; richtextstyle.text_color = 0xFF0000; richtextstyle.text_alignment = e_FSAlignmentLeft; richtextstyle.mark_style = e_FSCornerMarkNone; richtextstyle.text_size = 10; richtextstyle.is_bold = 0; richtextstyle.is_italic = 0; richtextstyle.is_strikethrough = 0; richtextstyle.is_underline = 0; FSRectF rect; rect.left = 50; rect.bottom = 510; rect.right = 200; rect.top = 530; FS_BOOL ret = false; FSDK_PDFPage_AddText(page, L"Trigger a popup menu", rect, richtextstyle, ret); FSDK_GraphicsObjects_GenerateContent(page, ret); // Add link annotation FSRectF rectf; rectf.left = 50; rectf.bottom = 510; rectf.right = 220; rectf.top = 530; FS_ANNOT_HANDLE annot; FSDK_PDFPage_AddAnnot(page, e_FSLink, rectf, annot); FS_ANNOT_HANDLE link; FSDK_Link_Create0(annot, link); //This flag is used for printing annotations. FSDK_Annot_SetFlags(link, 4); FSDK_Link_SetHighlightingMode(link, e_FSHighlightingInvert); FSDK_Annot_ResetAppearanceStream(link, ret); FS_PDFDOC_HANDLE doc; FSDK_PDFPage_GetDocument(page, 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_Link_SetAction(link, javascript_action); FSDK_Action_Release(javascript_action); FSDK_Link_Release(link); FSDK_PDFDoc_Release(doc); }
Updated on April 29, 2022