Using the HiQPdf software it is possible to set an action to be executed when the PDF document is opened in a viewer. The possible actions are to execute a JavaScript code, go to a location in the PDF document, submit or reset the document form.
In this demo you can learn how to set an action to be executed when the PDF document is opened. There are four possible types of actions: execute a JavaScript, go to a location in PDF, submit PDF document form and reset PDF document form.
In this demo only the JavaScript and GoTo actions are exemplified. The JavaScript action is used to display an alert message when the document is opened. The GoTo Action is used to initially display a given page of the PDF document when the document is opened.
private void buttonCreatePdf_Click(object sender, EventArgs e) { // create a PDF document PdfDocument document = new PdfDocument(); // create the true type fonts that can be used in document Font ttfFont = new Font("Times New Roman", 10, System.Drawing.GraphicsUnit.Point); PdfFont newTimesFont = document.CreateFont(ttfFont); PdfFont newTimesFontEmbed = document.CreateFont(ttfFont, true); // create page 1 PdfPage page1 = document.AddPage(); // create a text object to be laid out on this page PdfText text1 = new PdfText(10, 10, "This is the Page 1 of the document with Open action", newTimesFontEmbed); // layout the text page1.Layout(text1); // create page 2 PdfPage page2 = document.AddPage(); // create a text object to be laid out on this page PdfText text2 = new PdfText(10, 10, "This is the Page 2 of the document with Open action", newTimesFontEmbed); // layout the text page2.Layout(text2); // create page 1 PdfPage page3 = document.AddPage(); // create a text object to be laid out on this page PdfText text3 = new PdfText(10, 10, "This is the Page 3 of the document with Open action", newTimesFontEmbed); // layout the text page3.Layout(text3); if (radioButtonJavaScript.Checked) { // display an alert message when the document is opened string alertMessage = textBoxAlertMessage.Text; string javaScriptCode = "app.alert({cMsg: \"" + alertMessage + "\", cTitle: \"Open Document JavaScript Action\"});"; // create the JavaScript action to display the alert PdfJavaScriptAction javaScriptAction = new PdfJavaScriptAction(javaScriptCode); // set the document JavaScript open action document.SetOpenAction(javaScriptAction); } else { // go to a given page in document and set the given zoom level when the document is opened int pageIndex = radioButtonPage1.Checked ? 0 : (radioButtonPage2.Checked ? 1 : 2); int zoomLevel = int.Parse(textBoxZoomLevel.Text); PdfDestination openDestination = new PdfDestination(document.Pages[pageIndex], new PointF(10, 10)); openDestination.Zoom = zoomLevel; PdfGoToAction goToAction = new PdfGoToAction(openDestination); // set the document GoTo open action document.SetOpenAction(goToAction); } Cursor = Cursors.WaitCursor; string pdfFile = Application.StartupPath + @"\DemoOutput\OpenAction.pdf"; try { document.WriteToFile(pdfFile); } catch (Exception ex) { MessageBox.Show(String.Format("Cannot create the PDF document. {0}", ex.Message)); return; } finally { document.Close(); Cursor = Cursors.Arrow; } // open the created PDF document try { System.Diagnostics.Process.Start(pdfFile); } catch (Exception ex) { MessageBox.Show(String.Format("The PDF document was created but cannot open '{0}'. {1}", pdfFile, ex.Message)); } }