private static void CreateDocumentActions(PDFFixedDocument document) { // Create an action that will open the document at the last page with 200% zoom. PDFPageDirectDestination pageDestination = new PDFPageDirectDestination(); pageDestination.Page = document.Pages[document.Pages.Count - 1]; pageDestination.Zoom = 200; pageDestination.Top = 0; pageDestination.Left = 0; PDFGoToAction openAction = new PDFGoToAction(); openAction.Destination = pageDestination; document.OpenAction = openAction; // Create an action that is executed when the document is closed. PDFJavaScriptAction jsCloseAction = new PDFJavaScriptAction(); jsCloseAction.Script = "app.alert({cMsg: \"The document will close now.\", cTitle: \"O2S.Components.PDF4NET Actions Sample\"});"; document.BeforeCloseAction = jsCloseAction; // Create an action that is executed before the document is printed. PDFJavaScriptAction jsBeforePrintAction = new PDFJavaScriptAction(); jsBeforePrintAction.Script = "app.alert({cMsg: \"The document will be printed.\", cTitle: \"O2S.Components.PDF4NET Actions Sample\"});"; document.BeforePrintAction = jsBeforePrintAction; // Create an action that is executed after the document is printed. PDFJavaScriptAction jsAfterPrintAction = new PDFJavaScriptAction(); jsAfterPrintAction.Script = "app.alert({cMsg: \"The document has been printed.\", cTitle: \"O2S.Components.PDF4NET Actions Sample\"});"; document.AfterPrintAction = jsAfterPrintAction; }
private static void CreateJavaScriptActions(PDFFixedDocument document, PDFFont font) { PDFPen blackPen = new PDFPen(new PDFRgbColor(0, 0, 0), 1); PDFBrush blackBrush = new PDFBrush(); font.Size = 12; PDFStringAppearanceOptions sao = new PDFStringAppearanceOptions(); sao.Brush = blackBrush; sao.Font = font; PDFStringLayoutOptions slo = new PDFStringLayoutOptions(); slo.HorizontalAlign = PDFStringHorizontalAlign.Center; slo.VerticalAlign = PDFStringVerticalAlign.Middle; for (int i = 0; i < document.Pages.Count; i++) { document.Pages[i].Canvas.DrawString("JavaScript actions:", font, blackBrush, 400, 480); document.Pages[i].Canvas.DrawRectangle(blackPen, 400, 500, 200, 20); slo.X = 500; slo.Y = 510; document.Pages[i].Canvas.DrawString("Click me", sao, slo); // Create a link annotation on top of the widget. PDFLinkAnnotation link = new PDFLinkAnnotation(); document.Pages[i].Annotations.Add(link); link.DisplayRectangle = new PDFDisplayRectangle(400, 500, 200, 20); // Create a Javascript action and attach it to the link. PDFJavaScriptAction jsAction = new PDFJavaScriptAction(); jsAction.Script = "app.alert({cMsg: \"JavaScript action: you are now on page " + (i + 1) + "\", cTitle: \"O2S.Components.PDF4NET Actions Sample\"});"; link.Action = jsAction; } }
static void Main(string[] args) { // Create the pdf document //PDF4NET v5: PDFDocument doc = new PDFDocument(); PDFFixedDocument doc = new PDFFixedDocument(); // Create the graphic objects //PDF4NET v5: PDFFont fontText = new PDFFont(PDFFontFace.Helvetica, 12); PDFStandardFont fontText = new PDFStandardFont(PDFStandardFontFace.Helvetica, 12); PDFBrush blackBrush = new PDFBrush(new PDFRgbColor()); // Create 3 pages //PDF4NET v5: PDFPage page = doc.AddPage(); PDFPage page = doc.Pages.Add(); //PDF4NET v5: page.Canvas.DrawText("Page 1", fontText, null, blackBrush, 20, 30); page.Canvas.DrawString("Page 1", fontText, blackBrush, 20, 30); //PDF4NET v5: page = doc.AddPage(); page = doc.Pages.Add(); //PDF4NET v5: page.Canvas.DrawText("Page 2", fontText, null, blackBrush, 20, 30); page.Canvas.DrawString("Page 2", fontText, blackBrush, 20, 30); //PDF4NET v5: page = doc.AddPage(); page = doc.Pages.Add(); //PDF4NET v5: page.Canvas.DrawText("Page 3", fontText, null, blackBrush, 20, 30); page.Canvas.DrawString("Page 3", fontText, blackBrush, 20, 30); /// URI Action // create a web action //PDF4NET v5: PDFURIAction uriAction = new PDFURIAction(); PDFUriAction uriAction = new PDFUriAction(); uriAction.URI = "http://www.o2sol.com/"; // create a bookmark to point to the url above //PDF4NET v5: PDFBookmark url = new PDFBookmark(); PDFOutlineItem url = new PDFOutlineItem(); url.Title = "www.o2sol.com"; url.Color = new PDFRgbColor(); url.Action = uriAction; // add the bookmark to the document //PDF4NET v5: doc.Bookmarks.Add(url); doc.Outline.Add(url); /// URI Action /// Javascript Action // create a javascript action to print the document PDFJavaScriptAction jsAction = new PDFJavaScriptAction(); jsAction.Script = "this.print(true);\n"; // create a bookmark to point to execute the action //PDF4NET v5: PDFBookmark js = new PDFBookmark(); PDFOutlineItem js = new PDFOutlineItem(); js.Title = "Print me from JavaScript"; js.Color = new PDFRgbColor(); js.Action = jsAction; // add the bookmark to the document //PDF4NET v5: doc.Bookmarks.Add(url); doc.Outline.Add(js); /// Javascript Action /// OpenDocument action // Create an action to be executed when the document is opened // The action will open the document at the // second page of the document using a 800% zoom //PDF4NET v5: PDFPageDestination pageDestination = new PDFPageDestination(); PDFPageDirectDestination pageDestination = new PDFPageDirectDestination(); pageDestination.Page = doc.Pages[1]; //PDF4NET v5: pageDestination.MagnificationMode = PDFMagnificationMode.XYZ; pageDestination.ZoomMode = PDFDestinationZoomMode.XYZ; pageDestination.Left = 0; pageDestination.Top = 0; pageDestination.Zoom = 800; PDFGoToAction goToAction = new PDFGoToAction(); goToAction.Destination = pageDestination; // set the action on the document doc.OpenAction = goToAction; /// OpenDocument action // Save the document to disk doc.Save("Sample_Actions.pdf"); }
/// <summary> /// Main method for running the sample. /// </summary> public static SampleOutputInfo[] Run() { PDFFixedDocument document = new PDFFixedDocument(); PDFStandardFont helvetica = new PDFStandardFont(PDFStandardFontFace.Helvetica, 12); PDFBrush brush = new PDFBrush(); PDFPage page = document.Pages.Add(); // First name page.Canvas.DrawString("First name:", helvetica, brush, 50, 50); PDFTextBoxField firstNameTextBox = new PDFTextBoxField("firstname"); page.Fields.Add(firstNameTextBox); firstNameTextBox.Widgets[0].Font = helvetica; firstNameTextBox.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(150, 45, 200, 20); firstNameTextBox.Widgets[0].BorderColor = PDFRgbColor.Black; firstNameTextBox.Widgets[0].BorderWidth = 1; // Last name page.Canvas.DrawString("Last name:", helvetica, brush, 50, 80); PDFTextBoxField lastNameTextBox = new PDFTextBoxField("lastname"); page.Fields.Add(lastNameTextBox); lastNameTextBox.Widgets[0].Font = helvetica; lastNameTextBox.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(150, 75, 200, 20); lastNameTextBox.Widgets[0].BorderColor = PDFRgbColor.Black; lastNameTextBox.Widgets[0].BorderWidth = 1; // Sex page.Canvas.DrawString("Sex:", helvetica, brush, 50, 110); PDFRadioButtonField sexRadioButton = new PDFRadioButtonField("sex"); PDFRadioButtonWidget maleRadioItem = new PDFRadioButtonWidget(); sexRadioButton.Widgets.Add(maleRadioItem); PDFRadioButtonWidget femaleRadioItem = new PDFRadioButtonWidget(); sexRadioButton.Widgets.Add(femaleRadioItem); page.Fields.Add(sexRadioButton); page.Canvas.DrawString("Male", helvetica, brush, 180, 110); maleRadioItem.ExportValue = "M"; maleRadioItem.CheckStyle = PDFCheckStyle.Circle; maleRadioItem.DisplayRectangle = new PDFDisplayRectangle(150, 105, 20, 20); maleRadioItem.BorderColor = PDFRgbColor.Black; maleRadioItem.BorderWidth = 1; page.Canvas.DrawString("Female", helvetica, brush, 280, 110); femaleRadioItem.ExportValue = "F"; femaleRadioItem.CheckStyle = PDFCheckStyle.Circle; femaleRadioItem.DisplayRectangle = new PDFDisplayRectangle(250, 105, 20, 20); femaleRadioItem.BorderColor = PDFRgbColor.Black; femaleRadioItem.BorderWidth = 1; // First car page.Canvas.DrawString("First car:", helvetica, brush, 50, 140); PDFDropDownListField firstCarList = new PDFDropDownListField("firstcar"); firstCarList.Items.Add(new PDFListItem("Mercedes", "Mercedes")); firstCarList.Items.Add(new PDFListItem("BMW", "BMW")); firstCarList.Items.Add(new PDFListItem("Audi", "Audi")); firstCarList.Items.Add(new PDFListItem("Volkswagen", "Volkswagen")); firstCarList.Items.Add(new PDFListItem("Porsche", "Porsche")); firstCarList.Items.Add(new PDFListItem("Honda", "Honda")); firstCarList.Items.Add(new PDFListItem("Toyota", "Toyota")); firstCarList.Items.Add(new PDFListItem("Lexus", "Lexus")); firstCarList.Items.Add(new PDFListItem("Infiniti", "Infiniti")); firstCarList.Items.Add(new PDFListItem("Acura", "Acura")); page.Fields.Add(firstCarList); firstCarList.Widgets[0].Font = helvetica; firstCarList.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(150, 135, 200, 20); firstCarList.Widgets[0].BorderColor = PDFRgbColor.Black; firstCarList.Widgets[0].BorderWidth = 1; // Second car page.Canvas.DrawString("Second car:", helvetica, brush, 50, 170); PDFListBoxField secondCarList = new PDFListBoxField("secondcar"); secondCarList.Items.Add(new PDFListItem("Mercedes", "Mercedes")); secondCarList.Items.Add(new PDFListItem("BMW", "BMW")); secondCarList.Items.Add(new PDFListItem("Audi", "Audi")); secondCarList.Items.Add(new PDFListItem("Volkswagen", "Volkswagen")); secondCarList.Items.Add(new PDFListItem("Porsche", "Porsche")); secondCarList.Items.Add(new PDFListItem("Honda", "Honda")); secondCarList.Items.Add(new PDFListItem("Toyota", "Toyota")); secondCarList.Items.Add(new PDFListItem("Lexus", "Lexus")); secondCarList.Items.Add(new PDFListItem("Infiniti", "Infiniti")); secondCarList.Items.Add(new PDFListItem("Acura", "Acura")); page.Fields.Add(secondCarList); secondCarList.Widgets[0].Font = helvetica; secondCarList.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(150, 165, 200, 60); secondCarList.Widgets[0].BorderColor = PDFRgbColor.Black; secondCarList.Widgets[0].BorderWidth = 1; // I agree page.Canvas.DrawString("I agree:", helvetica, brush, 50, 240); PDFCheckBoxField agreeCheckBox = new PDFCheckBoxField("agree"); page.Fields.Add(agreeCheckBox); agreeCheckBox.Widgets[0].Font = helvetica; (agreeCheckBox.Widgets[0] as PDFCheckWidget).ExportValue = "YES"; (agreeCheckBox.Widgets[0] as PDFCheckWidget).CheckStyle = PDFCheckStyle.Check; agreeCheckBox.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(150, 235, 20, 20); agreeCheckBox.Widgets[0].BorderColor = PDFRgbColor.Black; agreeCheckBox.Widgets[0].BorderWidth = 1; // Sign here page.Canvas.DrawString("Sign here:", helvetica, brush, 50, 270); PDFSignatureField signHereField = new PDFSignatureField("signhere"); page.Fields.Add(signHereField); signHereField.Widgets[0].Font = helvetica; signHereField.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(150, 265, 200, 60); // Submit form PDFPushButtonField submitBtn = new PDFPushButtonField("submit"); page.Fields.Add(submitBtn); submitBtn.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(450, 45, 150, 30); (submitBtn.Widgets[0] as PDFPushButtonWidget).Caption = "Submit form"; submitBtn.Widgets[0].BackgroundColor = PDFRgbColor.LightGray; PDFSubmitFormAction submitFormAction = new PDFSubmitFormAction(); submitFormAction.DataFormat = PDFSubmitDataFormat.FDF; submitFormAction.Fields.Add("firstname"); submitFormAction.Fields.Add("lastname"); submitFormAction.Fields.Add("sex"); submitFormAction.Fields.Add("firstcar"); submitFormAction.Fields.Add("secondcar"); submitFormAction.Fields.Add("agree"); submitFormAction.Fields.Add("signhere"); submitFormAction.SubmitFields = true; submitFormAction.Url = "http://www.o2sol.com/"; submitBtn.Widgets[0].MouseUp = submitFormAction; // Reset form PDFPushButtonField resetBtn = new PDFPushButtonField("reset"); page.Fields.Add(resetBtn); resetBtn.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(450, 85, 150, 30); (resetBtn.Widgets[0] as PDFPushButtonWidget).Caption = "Reset form"; resetBtn.Widgets[0].BackgroundColor = PDFRgbColor.LightGray; PDFResetFormAction resetFormAction = new PDFResetFormAction(); resetBtn.Widgets[0].MouseUp = resetFormAction; // Print form PDFPushButtonField printBtn = new PDFPushButtonField("print"); page.Fields.Add(printBtn); printBtn.Widgets[0].DisplayRectangle = new PDFDisplayRectangle(450, 125, 150, 30); (printBtn.Widgets[0] as PDFPushButtonWidget).Caption = "Print form"; printBtn.Widgets[0].BackgroundColor = PDFRgbColor.LightGray; PDFJavaScriptAction printAction = new PDFJavaScriptAction(); printAction.Script = "this.print(true);\n"; printBtn.Widgets[0].MouseUp = printAction; SampleOutputInfo[] output = new SampleOutputInfo[] { new SampleOutputInfo(document, "formgenerator.pdf") }; return(output); }