Pdf Open Actions |
Select.Pdf Library for .NET can create pdf documents that will perform a certain action when the document is opened in a pdf viewer.
To set a pdf document open action, the OpenAction property of the PdfDocument object must be used. This is an instance of PdfDocumentOpenAction class that exposes the Action property.
The pdf document can perform 2 types of actions when it is opened:
PdfActionGoTo - The pdf document opens at a specific PdfDestination specified in the Destination property.
PdfActionJavaScript - The pdf document will run a specific javascript.
To instruct the pdf viewer to open the pdf document at a certain page, location on that page and zoom factor, the Action property needs to be set to an instance of PdfActionGoTo class.
The following sample code shows how to generate a pdf document from a html to pdf conversion and open the pdf document on page 2 directly in a pdf viewer.
// instantiate a html to pdf converter object HtmlToPdf converter = new HtmlToPdf(); // create a new pdf document converting an url PdfDocument doc = converter.ConvertUrl(TxtUrl.Text); // set the second page as open action destination if (doc.Pages.Count > 1) { PdfPage secondPage = doc.Pages[1]; PdfDestination secondPageDestination = new PdfDestination(secondPage); doc.OpenAction.Action = new PdfActionGoTo(secondPageDestination); } // save pdf document doc.Save(Response, false, "Sample.pdf"); // close pdf document doc.Close();
To instruct the pdf viewer to execute a Javascript when the pdf document is opened in a pdf viewer, the Action property needs to be set to an instance of PdfActionJavaScript class.
The following sample code shows how to generate a pdf document from a html to pdf conversion and display the print dialog when the pdf file is opened in a viewer.
// instantiate a html to pdf converter object HtmlToPdf converter = new HtmlToPdf(); // create a new pdf document converting an url PdfDocument doc = converter.ConvertUrl(url); // set the javascript open action doc.OpenAction.Action = new PdfActionJavaScript("print()"); // save pdf document doc.Save(Response, false, "Sample.pdf"); // close pdf document doc.Close();
Here are some common JavaScript methods or properties supported by Adobe Acrobat Javascript. These should server as a parameter for PdfActionJavaScript.
The following javascript can be used to display an alert popup when the pdf document is displayed in a pdf viewer:
app.alert("Alert Message")
The following javascript can be used to open the print dialog when the pdf document is displayed in a pdf viewer:
print()
The following javascript can be used to print silently (viewer should only ask for permission) when the pdf document is displayed in a pdf viewer:
print({bUI: false, bSilent: true, bShrinkToFit: true})
The following javascript can be used to set the zoom to a certain level when the pdf document is displayed in a pdf viewer:
zoom = 150