Working with Actions

19 Dec 202424 minutes to read

Essential® PDF supports different actions that can be triggered by different events and user interactions.

Adding an action to the PDF

The below code example illustrates how to add the action to the PDF document using PdfLaunchAction class.

//Create a new PDF document.
PdfDocument document = new PdfDocument();

//Create and add new launch Action to the document.
PdfLaunchAction action = new PdfLaunchAction("logo.png");
document.Actions.AfterOpen = action;

//Save the document into stream
MemoryStream stream = new MemoryStream();
document.Save(stream);
stream.Position = 0;
//Close the document
document.Close(true);
//Defining the ContentType for pdf file
string contentType = "application/pdf";
//Define the file name
string fileName = "Output.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name
return File(stream, contentType, fileName);
//Create a new PDF document
PdfDocument document = new PdfDocument();

//Create and add new launch action to the document
PdfLaunchAction action = new PdfLaunchAction("logo.png");
document.Actions.AfterOpen = action;

//Save the document
document.Save("LaunchAction.pdf");
//Close the document
document.Close(true);
'Create a new PDF document
Dim document As New PdfDocument()

'Create and add new launch action to the document
Dim action As New PdfLaunchAction("logo.png")
document.Actions.AfterOpen = action

'Save the document
document.Save("LaunchAction.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

Supported action types

Essential® PDF supports the following types of actions.

Sound action

The PdfSoundAction plays a specified music file in the PDF document. Volume and repeat can be specified for the sound action.

//Create a new document
PdfDocument document = new PdfDocument();
//Add a page
PdfPage page = document.Pages.Add();

//Create a sound action
FileStream fileStream = new FileStream("Startup.wav", FileMode.Open, FileAccess.Read);
PdfSoundAction soundAction = new PdfSoundAction(fileStream);
soundAction.Sound.Bits = 16;
soundAction.Sound.Channels = PdfSoundChannels.Stereo;
soundAction.Sound.Encoding = PdfSoundEncoding.Signed;
soundAction.Volume = 0.9f;

//Set the sound action
document.Actions.AfterOpen = soundAction;

//Save the document into stream
MemoryStream stream = new MemoryStream();
document.Save(stream);
stream.Position = 0;
//Close the document
document.Close(true);
//Defining the ContentType for pdf file
string contentType = "application/pdf";
//Define the file name
string fileName = "Output.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name
return File(stream, contentType, fileName);
//Create a new document
PdfDocument document = new PdfDocument();
//Add a page
PdfPage page = document.Pages.Add();

//Create a sound action
PdfSoundAction soundAction = new PdfSoundAction("../../Data/Startup.wav");
soundAction.Sound.Bits = 16;
soundAction.Sound.Channels = PdfSoundChannels.Stereo;
soundAction.Sound.Encoding = PdfSoundEncoding.Signed;
soundAction.Volume = 0.9f;

//Set the sound action
document.Actions.AfterOpen = soundAction;

//Save and close the PDF document
document.Save("Output.pdf");
document.Close(true);
'Create a new document
Dim document As New PdfDocument()
'Add a page
Dim page As PdfPage = document.Pages.Add()

'Create a sound action
Dim soundAction As New PdfSoundAction("../../Data/Startup.wav")
soundAction.Sound.Bits = 16
soundAction.Sound.Channels = PdfSoundChannels.Stereo
soundAction.Sound.Encoding = PdfSoundEncoding.Signed
soundAction.Volume = 0.9F

'Set the sound action
document.Actions.AfterOpen = soundAction

'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

JavaScript action

The PdfJavaScriptAction allows execution of JavaScript code embedded in the PDF document.

//Create a new document
PdfDocument document = new PdfDocument();
//Add a page
PdfPage page = document.Pages.Add();

//Create JavaScript action
PdfJavaScriptAction scriptAction = new PdfJavaScriptAction("app.alert(\"Hello World!!!\")");

//Add the JavaScript action
document.Actions.AfterOpen = scriptAction;

//Save the document into stream
MemoryStream stream = new MemoryStream();
document.Save(stream);
stream.Position = 0;
//Close the document
document.Close(true);
//Defining the ContentType for pdf file
string contentType = "application/pdf";
//Define the file name
string fileName = "Output.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name
return File(stream, contentType, fileName);
//Create a new document
PdfDocument document = new PdfDocument();
//Add a page
PdfPage page = document.Pages.Add();

//Create JavaScript action
PdfJavaScriptAction scriptAction = new PdfJavaScriptAction("app.alert(\"Hello World!!!\")");

//Add the JavaScript action
document.Actions.AfterOpen = scriptAction;

//Save and close the PDF document
document.Save("Output.pdf");
document.Close(true);
'Create a new document
Dim document As New PdfDocument()
'Add a page
Dim page As PdfPage = document.Pages.Add()

'Create JavaScript action
Dim scriptAction As New PdfJavaScriptAction("app.alert(""Hello World!!!"")")

'Add the JavaScript action
document.Actions.AfterOpen = scriptAction

'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

NOTE

You can refer more PDF JavaScript code in PdfJavaScriptAction from the below developer guide.
https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsdevguide/index.html

URI action

The PdfUriAction allows you to create a hyperlink that can open web page in a web browser.

//Create a new document with PDF/A standard
PdfDocument document = new PdfDocument();

//Create a URI action
PdfUriAction uriAction = new PdfUriAction("http://www.google.com");

//Add the action to the document
document.Actions.AfterOpen = uriAction;

//Save the document into stream
MemoryStream stream = new MemoryStream();
document.Save(stream);
stream.Position = 0;
//Close the document
document.Close(true);
//Defining the ContentType for PDF file
string contentType = "application/pdf";
//Define the file name
string fileName = "Output.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name
return File(stream, contentType, fileName);
//Create a new document with PDF/A standard
PdfDocument document = new PdfDocument();

//Create a uri action
PdfUriAction uriAction = new PdfUriAction("http://www.google.com");

//Add the action to the document
document.Actions.AfterOpen = uriAction;

//Save and close the PDF document
document.Save("Output.pdf");
document.Close(true);
'Create a new document with PDF/A standard
Dim document As New PdfDocument()

'Create a URI action
Dim uriAction As New PdfUriAction("http://www.google.com")

'Add the action to the document
document.Actions.AfterOpen = uriAction

'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

GoTo action

The PdfGoToAction displays the specified page in the current document. The location can be specified for the destination page.

//Create a new document
PdfDocument document = new PdfDocument();
//Add first page
PdfPage page = document.Pages.Add();
//Add second page
PdfPage secondPage = document.Pages.Add();

//Set the goto action
PdfGoToAction gotoAction = new PdfGoToAction(secondPage);
//Set destination location
gotoAction.Destination = new PdfDestination(secondPage, new PointF(0, 100));

//Add the action to the document
document.Actions.AfterOpen = gotoAction;

//Save the document into stream
MemoryStream stream = new MemoryStream();
document.Save(stream);
stream.Position = 0;
//Close the document
document.Close(true);
//Defining the ContentType for PDF file
string contentType = "application/pdf";
//Define the file name
string fileName = "Output.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name
return File(stream, contentType, fileName);
//Create a new document
PdfDocument document = new PdfDocument();
//Add first page
PdfPage page = document.Pages.Add();
//Add second page
PdfPage secondPage = document.Pages.Add();

//Set the goto action
PdfGoToAction gotoAction = new PdfGoToAction(secondPage);
//Set destination location
gotoAction.Destination = new PdfDestination(secondPage, new PointF(0, 100));

//Add the action to the document
document.Actions.AfterOpen = gotoAction;

//Save and close the PDF document
document.Save("Output.pdf");
document.Close(true);
'Create a new document
Dim document As New PdfDocument()
'Add first page
Dim page As PdfPage = document.Pages.Add()
'Add second page
Dim secondPage As PdfPage = document.Pages.Add()

'Set the goto action
Dim gotoAction As New PdfGoToAction(secondPage)
'Set destination location
gotoAction.Destination = New PdfDestination(secondPage, New PointF(0, 100))

'Add the action to the document
document.Actions.AfterOpen = gotoAction

'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

Launch action

The PdfLaunchAction allows execution of an external file. The following code example explains how to add a launch action in PDF document.

//Create a new PDF document.
PdfDocument document = new PdfDocument();

//Create and add new launch Action to the document.
PdfLaunchAction action = new PdfLaunchAction("logo.png");
document.Actions.AfterOpen = action;

//Save the document into stream
MemoryStream stream = new MemoryStream();
document.Save(stream);
stream.Position = 0;
//Close the document
document.Close(true);
//Defining the ContentType for pdf file
string contentType = "application/pdf";
//Define the file name
string fileName = "Output.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name
return File(stream, contentType, fileName);
//Create a new PDF document
PdfDocument document = new PdfDocument();

//Create and add new launch Action to the document
PdfLaunchAction action = new PdfLaunchAction("logo.png");

//Add the action to the document. 
document.Actions.AfterOpen = action;

//Save and close the document
document.Save("LaunchAction.pdf");
document.Close(true);
'Create a new PDF document
Dim document As New PdfDocument()

'Create and add new launch Action to the document
Dim action As New PdfLaunchAction("logo.png")

'Add the action to the document. 
document.Actions.AfterOpen = action

'Save and close the document
document.Save("LaunchAction.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

Named action

The PdfNamedAction allows execution of below predefined PDF actions using PdfActionDestination Enum.

  • Go to next page
  • Go to previous page
  • Go to first page and
  • Go to last page
//Create a new document
PdfDocument document = new PdfDocument();
//Add the page
document.Pages.Add();
document.Pages.Add();

//Create a named action
PdfNamedAction namedAction = new PdfNamedAction(PdfActionDestination.LastPage);

//Add the named action
document.Actions.AfterOpen = namedAction;

//Save the document into stream.
MemoryStream stream = new MemoryStream();
document.Save(stream);
stream.Position = 0;
//Close the document.
document.Close(true);
//Defining the ContentType for PDF file.
string contentType = "application/pdf";
//Define the file name.
string fileName = "Output.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name.
return File(stream, contentType, fileName);
//Create a new document
PdfDocument document = new PdfDocument();
//Add the page
document.Pages.Add();
document.Pages.Add();

//Create a named action
PdfNamedAction namedAction = new PdfNamedAction(PdfActionDestination.LastPage);

//Add the named action
document.Actions.AfterOpen = namedAction;

//Save and close the PDF document
document.Save("Output.pdf");
document.Close(true);
'Create a new document
Dim document As New PdfDocument()
'Add the page
document.Pages.Add()
document.Pages.Add()

'Create a named action
Dim namedAction As New PdfNamedAction(PdfActionDestination.LastPage)

'Add the named action
document.Actions.AfterOpen = namedAction

'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

Submit action

The PdfSubmitAction allows submission of data that is entered in the PDF form using PdfButtonField form field.

//Create a PDF document
PdfDocument document = new PdfDocument();
//Add a new page
PdfPage page = document.Pages.Add();

//Create a Button field
PdfButtonField submitButton = new PdfButtonField(page, "Submit data");
submitButton.Bounds = new RectangleF(100, 60, 50, 20);
submitButton.ToolTip = "Submit";
//Add button field to the form
document.Form.Fields.Add(submitButton);

//Create a submit action. It submit the data of the form fields to the mentioned URL
PdfSubmitAction submitAction = new PdfSubmitAction("http://www.syncfusionforms.com/Submit.aspx");
submitAction.DataFormat = SubmitDataFormat.Html;
submitButton.Actions.GotFocus = submitAction;

//Save the document into stream
MemoryStream stream = new MemoryStream();
document.Save(stream);
stream.Position = 0;
//Close the document
document.Close(true);
//Defining the ContentType for PDF file
string contentType = "application/pdf";
//Define the file name
string fileName = "Output.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name
return File(stream, contentType, fileName);
//Create a PDF document
PdfDocument document = new PdfDocument();
//Add a new page
PdfPage page = document.Pages.Add();

//Create a Button field
PdfButtonField submitButton = new PdfButtonField(page, "Submit data");
submitButton.Bounds = new RectangleF(100, 60, 50, 20);
submitButton.ToolTip = "Submit";
//Add button field to the form
document.Form.Fields.Add(submitButton);

//Create a submit action. It submit the data of the form fields to the mentioned URL
PdfSubmitAction submitAction = new PdfSubmitAction("http://www.syncfusionforms.com/Submit.aspx");
submitAction.DataFormat = SubmitDataFormat.Html;
submitButton.Actions.GotFocus = submitAction;

//Save and close the PDF document
document.Save("Output.pdf");
document.Close(true);
'Create a PDF document
Dim document As New PdfDocument()
'Add a new page
Dim page As PdfPage = document.Pages.Add()

'Create a Button field.
Dim submitButton As New PdfButtonField(page, "Submit data")
submitButton.Bounds = New RectangleF(100, 60, 50, 20)
submitButton.ToolTip = "Submit"
'Add button field to the form
document.Form.Fields.Add(submitButton)

'Create a submit action. It submit the data of the form fields to the mentioned URL
Dim submitAction As New PdfSubmitAction("http:// www.example.com/Submit.aspx")
submitAction.DataFormat = SubmitDataFormat.Html
submitButton.Actions.GotFocus = submitAction

'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

Reset action

The PdfResetAction allows execution of reset of all the form fields in the PDF document.

//Create a PDF document
PdfDocument document = new PdfDocument();
//Add a new page
PdfPage page = document.Pages.Add();

//Create a Text box field
PdfTextBoxField textBoxField = new PdfTextBoxField(page, "FirstName");
//Set properties to the textbox
textBoxField.BorderColor = new PdfColor(Color.Gray);
textBoxField.BorderStyle = PdfBorderStyle.Beveled;
textBoxField.Bounds = new RectangleF(80, 0, 100, 20);
textBoxField.Text = "First Name";

//Add the form field to the document
document.Form.Fields.Add(textBoxField);

//Create a Button field
PdfButtonField clearButton = new PdfButtonField(page, "Clear");
clearButton.Bounds = new RectangleF(100, 60, 50, 20);
clearButton.ToolTip = "Clear";
//Add button field to the form
document.Form.Fields.Add(clearButton);

//Save the document into stream
MemoryStream stream = new MemoryStream();
document.Save(stream);
stream.Position = 0;
//Close the document
document.Close(true);
//Defining the ContentType for PDF file
string contentType = "application/pdf";
//Define the file name
string fileName = "Output.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name
return File(stream, contentType, fileName);
//Create a PDF document
PdfDocument document = new PdfDocument();
//Add a new page
PdfPage page = document.Pages.Add();

//Create a Text box field
PdfTextBoxField textBoxField = new PdfTextBoxField(page, "FirstName");
//Set properties to the textbox
textBoxField.BorderColor = new PdfColor(Color.Gray);
textBoxField.BorderStyle = PdfBorderStyle.Beveled;
textBoxField.Bounds = new RectangleF(80, 0, 100, 20);
textBoxField.Text = "First Name";

//Add the form field to the document
document.Form.Fields.Add(textBoxField);

//Create a Button field.
PdfButtonField clearButton = new PdfButtonField(page, "Clear");
clearButton.Bounds = new RectangleF(100, 60, 50, 20);
clearButton.ToolTip = "Clear";
//Add button field to the form
document.Form.Fields.Add(clearButton);

//Create an instance of reset action
PdfResetAction resetAction = new PdfResetAction();
clearButton.Actions.GotFocus = resetAction;

//Save and close the PDF document
document.Save("Output.pdf");
document.Close(true);
'Create a PDF document
Dim document As New PdfDocument()
'Add a new page
Dim page As PdfPage = document.Pages.Add()

'Create a Text box field.
Dim textBoxField As New PdfTextBoxField(page, "FirstName")
'Set properties to the textbox.
textBoxField.BorderColor = New PdfColor(Color.Gray)
textBoxField.BorderStyle = PdfBorderStyle.Beveled
textBoxField.Bounds = New RectangleF(80, 0, 100, 20)
textBoxField.Text = "First Name"

'Add the form field to the document
document.Form.Fields.Add(textBoxField)

'Create a Button field.
Dim clearButton As New PdfButtonField(page, "Clear")
clearButton.Bounds = New RectangleF(100, 60, 50, 20)
clearButton.ToolTip = "Clear"
'Add button field to the form
document.Form.Fields.Add(clearButton)

'Create an instance of reset action
Dim resetAction As New PdfResetAction()
clearButton.Actions.GotFocus = resetAction

'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

Remote GoTo action:

The PdfRemoteGoToAction in a PDF document enables users to navigate to a specific destination within a remote PDF file. This feature seamlessly directs users to specific pages or locations in another PDF document.

//Create a new PDF document
PdfDocument document = new PdfDocument();
//Create a new page
PdfPage page = document.Pages.Add();
//Create font and font style
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold);
//Create a new PdfButtonField
PdfButtonField submitButton = new PdfButtonField(page, "submitButton");
submitButton.Bounds = new RectangleF(25, 160, 100, 20);
submitButton.Font = font;
submitButton.Text = "Open file";
submitButton.BackColor = new PdfColor(181, 191, 203);
//Create a new remote destination
PdfRemoteDestination remoteDestination = new PdfRemoteDestination();
remoteDestination.RemotePageNumber = 3;
remoteDestination.Mode = PdfDestinationMode.FitToPage;
//Create a new PdfRemoteGoToAction
PdfRemoteGoToAction goToAction = new PdfRemoteGoToAction("input.pdf", remoteDestination);
//Set the IsNewWindow
goToAction.IsNewWindow = true;
//Add the action to the button.
submitButton.Actions.GotFocus = goToAction;
//Add the submit button to a new document
document.Form.Fields.Add(submitButton);
//Save the document into stream
MemoryStream stream = new MemoryStream();
document.Save(stream);
stream.Position = 0;
//Close the document
document.Close(true);
// Defining the ContentType for the PDF file
string contentType = "application/pdf";
//Define the file name
string fileName = "Output.pdf";
//Create a FileContentResult object by using the file contents, content type, and file name
return File(stream,  contentType,  fileName);
//Create a new PDF document
PdfDocument document = new PdfDocument();
//Create a new page
PdfPage page = document.Pages.Add();
//Create font and font style
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold);
//Create a new PdfButtonField
PdfButtonField submitButton = new PdfButtonField(page, "submitButton");
submitButton.Bounds = new RectangleF(25, 160, 100, 20);
submitButton.Font = font;
submitButton.Text = "Open file";
submitButton.BackColor = new PdfColor(181, 191, 203);
//Create a new remote destination
PdfRemoteDestination remoteDestination = new PdfRemoteDestination();
remoteDestination.RemotePageNumber = 3;
remoteDestination.Mode = PdfDestinationMode.FitToPage;
//Create a new PdfRemoteGoToAction
PdfRemoteGoToAction goToAction = new PdfRemoteGoToAction("input.pdf", remoteDestination);
//Set the IsNewWindow
goToAction.IsNewWindow = true;
//Add the goToAction
submitButton.Actions.GotFocus = goToAction;
//Add the submit button to a new document
document.Form.Fields.Add(submitButton);
//Save the document into stream
MemoryStream stream = new MemoryStream();
document.Save(stream);
stream.Position = 0;
//Close the document
document.Close(true);
'Create a new document
Dim document As PdfDocument = New PdfDocument
'Create a new page
Dim page As PdfPage = document.Pages.Add
'Create a new font and font style
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 12!, PdfFontStyle.Bold)
'Create a new Buttonfield
Dim submitButton As PdfButtonField = New PdfButtonField(page, "submitButton")
submitButton.Bounds = New RectangleF(25, 160, 100, 20)
submitButton.Font = font
submitButton.Text = "Open file"
submitButton.BackColor = New PdfColor(181, 191, 203)
'Create a new PdfRemoteDestination
Dim remoteDestination As PdfRemoteDestination = New PdfRemoteDestination
remoteDestination.RemotePageNumber = 3
remoteDestination.Mode = PdfDestinationMode.FitToPage
'Create a new PdfRemoteGoToAction
Dim goToAction As PdfRemoteGoToAction = New PdfRemoteGoToAction("input.pdf", remoteDestination)
'Set the IsNewWindow
goToAction.IsNewWindow = true
'Add the goToAction
submitButton.Actions.GotFocus = goToAction
'Add the submit button to a new document
document.Form.Fields.Add(submitButton)
Dim stream As MemoryStream = New MemoryStream
document.Save(stream)
stream.Position = 0
'Close the document
document.Close(true)

You can download a complete working sample from [GitHub]

Adding an action to the form field

Essential® PDF provides support to add various actions to the form fields. The following code example illustrates how to add actions to the form field in PDF document using PdfFieldActions class.

//Create a new PDF document
PdfDocument document = new PdfDocument();
//Creates a new page
PdfPage page = document.Pages.Add();

//Create a new PdfButtonField
PdfButtonField submitButton = new PdfButtonField(page, "submitButton");
submitButton.Bounds = new RectangleF(25, 160, 100, 20);
submitButton.Text = "Apply";
submitButton.BackColor = new PdfColor(181, 191, 203);

//Create a new PdfJavaScriptAction
PdfJavaScriptAction scriptAction = new PdfJavaScriptAction("app.alert(\"You are looking at Form field action of PDF \")");
//Set the scriptAction to submitButton
submitButton.Actions.MouseDown = scriptAction;

//Add the submit button to the new document
document.Form.Fields.Add(submitButton);

//Save the document into stream
MemoryStream stream = new MemoryStream();
document.Save(stream);
stream.Position = 0;
//Close the document
document.Close(true);
//Defining the ContentType for PDF file
string contentType = "application/pdf";
//Define the file name
string fileName = "Output.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name
return File(stream, contentType, fileName);
//Create a new PDF document
PdfDocument document = new PdfDocument();
//Creates a new page
PdfPage page = document.Pages.Add();

//Create a new PdfButtonField
PdfButtonField submitButton = new PdfButtonField(page, "submitButton");
submitButton.Bounds = new RectangleF(25, 160, 100, 20);
submitButton.Text = "Apply";
submitButton.BackColor = new PdfColor(181, 191, 203);

//Create a new PdfJavaScriptAction
PdfJavaScriptAction scriptAction = new PdfJavaScriptAction("app.alert(\"You are looking at Form field action of PDF \")");
//Set the scriptAction to submitButton
submitButton.Actions.MouseDown = scriptAction;

//Add the submit button to the new document
document.Form.Fields.Add(submitButton);

//Save document to disk
document.Save("fieldAction.pdf");
//Close the document
document.Close(true);
'Create a new PDF document
Dim document As New PdfDocument()

'Creates a new page
Dim page As PdfPage = document.Pages.Add()

'Create a new PdfButtonField
Dim submitButton As New PdfButtonField(page, "submitButton")
submitButton.Bounds = New RectangleF(25, 160, 100, 20)
submitButton.Text = "Apply"
submitButton.BackColor = New PdfColor(181, 191, 203)

'Create a new PdfJavaScriptAction
Dim scriptAction As New PdfJavaScriptAction("app.alert(\"You are looking at Form field action of PDF \")")
'Set the scriptAction to submitButton
submitButton.Actions.MouseDown = scriptAction

'Add the submit button to the new document
document.Form.Fields.Add(submitButton)

'Save document to disk
document.Save("fieldAction.pdf")
'Close the document
document.Close(True)

You can download a complete working sample from GitHub.

Adding an action to the bookmarks

Essential® PDF provides support to add the various actions to the Bookmarks. The code snippet below shows how to add an URI action to bookmark.

//Create a new document
PdfDocument document = new PdfDocument();
//Add a page
PdfPage page = document.Pages.Add();

//Create document bookmarks
PdfBookmark bookmark = document.Bookmarks.Add("Page 1");
//Set the text style and color
bookmark.TextStyle = PdfTextStyle.Bold;
bookmark.Color = Color.Red;

//Create a Uri action
PdfUriAction uriAction = new PdfUriAction("http://www.google.com");
//Set the Uri action to the bookmark 
bookmark.Action = uriAction;

//Save the document into stream
MemoryStream stream = new MemoryStream();
document.Save(stream);
stream.Position = 0;
//Close the document.
document.Close(true);
//Defining the ContentType for PDF file
string contentType = "application/pdf";
//Define the file name
string fileName = "Output.pdf";
//Creates a FileContentResult object by using the file contents, content type, and file name
return File(stream, contentType, fileName);
//Create a new document
PdfDocument document = new PdfDocument();
//Add a page
PdfPage page = document.Pages.Add();

//Create document bookmarks
PdfBookmark bookmark = document.Bookmarks.Add("Page 1");
//Set the text style and color
bookmark.TextStyle = PdfTextStyle.Bold;
bookmark.Color = Color.Red;

//Create a Uri action
PdfUriAction uriAction = new PdfUriAction("http://www.google.com");
//Set the Uri action to bookmark
bookmark.Action = uriAction;

//Save and close the PDF document.
document.Save("Output.pdf");
document.Close(true);
'Create a new document
Dim document As New PdfDocument()
'Add a page
Dim page As PdfPage = document.Pages.Add()

'Create document bookmarks
Dim bookmark As PdfBookmark = document.Bookmarks.Add("Page 1")
'Set the text style and color
bookmark.TextStyle = PdfTextStyle.Bold
bookmark.Color = Color.Red

'Create a Uri action
Dim uriAction As New PdfUriAction("http://www.google.com")
'Set the Uri action to the bookmark
bookmark.Action = uriAction

'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

Add actions to the existing PDF document

To add actions to an existing PDF document, use the following code example.

//Load the PDF document.
FileStream docStream = new FileStream("input.pdf", FileMode.Open, FileAccess.Read); PdfLoadedDocument document = new PdfLoadedDocument(docStream); 

//Create JavaScript action
PdfJavaScriptAction scriptAction = new PdfJavaScriptAction("app.alert(\"Hello World!!!\")"); 

//Add the JavaScript action
document.Actions.AfterOpen = scriptAction;

//Save the document into stream
MemoryStream stream = new MemoryStream(); 
document.Save(stream); stream.Position = 0;
//Close the document.
document.Close(true);
//Define the ContentType for a pdf file
string contentType = "application/pdf"; 
//Define the file name.
string fileName = "output.pdf"; 
//Create a FileContentResult object by using the file contents, content type, and file name. 
return File(stream, contentType, fileName);
//Load a document from the disk.
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("input.pdf");

//Create JavaScript action.
PdfJavaScriptAction scriptAction = new PdfJavaScriptAction("app.alert(\"Hello World!!!\")");

//Add the JavaScript action.
loadedDocument.Actions.AfterOpen = scriptAction;
           
// Save and close the document.
loadedDocument.Save("Output.pdf");
loadedDocument.Close();
'Create a new PDF document. 
Dim document As New PdfLoadedDocument("input.pdf")

'Create JavaScript action. 
Dim scriptAction As New PdfJavaScriptAction("app.alert(""Hello World!!!"")") 

'Add the JavaScript action.
 document.Actions.AfterOpen = scriptAction

'Save the document to disk.
 document.Save("PopupAnnotation.pdf") 
document.Close(True)

You can download a complete working sample from GitHub.

NOTE

The action assigned to the bookmark works only when destination of bookmark is not set.

Document-Level JavaScript Actions

Document-level JavaScript actions allow you to embed interactivity into your PDF documents. Using the Essential® PDF Library, you can easily add these actions to execute scripts when the document is opened.

Adding Document-Level JavaScript Actions

The following example demonstrates how to add document-level JavaScript actions to a PDF document using the PdfDocumentJavaScriptCollection class.

// Create a new PDF document instance.
PdfDocument document = new PdfDocument();
// Retrieve the JavaScript collection from the document.
PdfDocumentJavaScriptCollection javaScriptCollection = document.DocumentJavaScripts;

// Define a new JavaScript action that displays an alert with the message "Hello World!!!".
PdfJavaScriptAction javaScriptAction = new PdfJavaScriptAction("app.alert(\"Hello World!!!\")");

// Set the name of the JavaScript action.
javaScriptAction.Name = "Test";
// Add the JavaScript action to the document's JavaScript collection.
javaScriptCollection.Add(javaScriptAction);

//Save the document into stream
using(MemoryStream stream = new MemoryStream())
{
    document.Save(stream);
}

//Close the document.
document.Close(true);
// Create a new PDF document instance.
PdfDocument document = new PdfDocument();
// Retrieve the JavaScript collection from the document.
PdfDocumentJavaScriptCollection javaScriptCollection = document.DocumentJavaScripts;

// Define a new JavaScript action that displays an alert with the message "Hello World!!!".
PdfJavaScriptAction javaScriptAction = new PdfJavaScriptAction("app.alert(\"Hello World!!!\")");

// Set the name of the JavaScript action.
javaScriptAction.Name = "Test";
// Add the JavaScript action to the document's JavaScript collection.
javaScriptCollection.Add(javaScriptAction);

// Save the PDF document to a file named "Output.pdf".
document.Save("Output.pdf");

//Close the document
document.Close(true);
' Create a new PDF document instance.
Dim document As New PdfDocument()
' Retrieve the JavaScript collection from the document.
Dim javaScriptCollection As PdfDocumentJavaScriptCollection = document.DocumentJavaScripts

' Define a new JavaScript action that displays an alert with the message "Hello World!!!".
Dim javaScriptAction As New PdfJavaScriptAction("app.alert(""Hello World!!!"")")

' Set the name of the JavaScript action.
javaScriptAction.Name = "Test"
' Add the JavaScript action to the document's JavaScript collection.
javaScriptCollection.Add(javaScriptAction)

' Save the PDF document to a file named "Output.pdf".
document.Save("Output.pdf")

' Close the document
document.Close(True)

You can download a complete working sample from GitHub.

Retrieve Document-Level JavaScript Actions

The following example demonstrates how to retrieve document-level JavaScript actions using the PdfDocumentJavaScriptCollection class.

//Load an existing PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument document = new PdfLoadedDocument(docStream);

// Retrieve the JavaScript collection from the loaded document.
PdfDocumentJavaScriptCollection javaScriptCollection = document.DocumentJavaScripts;

// Iterate through the JavaScript actions in the collection.
foreach (PdfJavaScriptAction action in javaScriptCollection)
{
    // Display the name of the JavaScript action.
    Console.WriteLine($"Action Name: {action.Name}");

    // Display the JavaScript code associated with the action.
    Console.WriteLine($"JavaScript Code: {action.JavaScript}");
}

// Close the document and release all associated resources.
document.Close(true);
// Load an existing PDF document.
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");

// Retrieve the JavaScript collection from the loaded document.
PdfDocumentJavaScriptCollection javaScriptCollection = document.DocumentJavaScripts;

// Iterate through the JavaScript actions in the collection.
foreach (PdfJavaScriptAction action in javaScriptCollection)
{
    // Display the name of the JavaScript action.
    Console.WriteLine($"Action Name: {action.Name}");

    // Display the JavaScript code associated with the action.
    Console.WriteLine($"JavaScript Code: {action.JavaScript}");
}

// Close the document and release all associated resources.
document.Close(true);
' Load an existing PDF document.
Dim document As New PdfLoadedDocument("Input.pdf")

' Retrieve the JavaScript collection from the loaded document.
Dim javaScriptCollection As PdfDocumentJavaScriptCollection = document.DocumentJavaScripts

' Iterate through the JavaScript actions in the collection.
For Each action As PdfJavaScriptAction In javaScriptCollection
    ' Display the name of the JavaScript action.
    Console.WriteLine($"Action Name: {action.Name}")

    ' Display the JavaScript code associated with the action.
    Console.WriteLine($"JavaScript Code: {action.JavaScript}")
Next

' Close the document and release all associated resources.
document.Close(True)

You can download a complete working sample from GitHub.

Remove Document-Level JavaScript Actions

The following example demonstrates how to remove document-level JavaScript actions using the PdfDocumentJavaScriptCollection class.

//Load an existing PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument document = new PdfLoadedDocument(docStream);

// Retrieve the JavaScript collection from the document.
PdfDocumentJavaScriptCollection javaScriptCollection = document.DocumentJavaScripts;

// Access the first JavaScript action in the collection.
PdfJavaScriptAction javaScriptAction = javaScriptCollection[0];

// Remove the selected JavaScript action from the collection.
javaScriptCollection.Remove(javaScriptAction);

// Close the document and release all associated resources.
document.Close(true);
// Load an existing PDF document.
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");

// Retrieve the JavaScript collection from the document.
PdfDocumentJavaScriptCollection javaScriptCollection = document.DocumentJavaScripts;

// Access the first JavaScript action in the collection.
PdfJavaScriptAction javaScriptAction = javaScriptCollection[0];

// Remove the selected JavaScript action from the collection.
javaScriptCollection.Remove(javaScriptAction);

// Close the document and release all associated resources.
document.Close(true);
' Load an existing PDF document.
Dim document As New PdfLoadedDocument("Input.pdf")

' Retrieve the JavaScript collection from the document.
Dim javaScriptCollection As PdfDocumentJavaScriptCollection = document.DocumentJavaScripts

' Access the first JavaScript action from the collection.
Dim javaScriptAction As PdfJavaScriptAction = javaScriptCollection(0)

' Remove the selected JavaScript action from the collection.
javaScriptCollection.Remove(javaScriptAction)

' Close the document and release all associated resources.
document.Close(True)

You can download a complete working sample from GitHub.