Action (9)
Enhancing the interactivity of PDF files is a crucial aspect of document management and user engagement. Creating actions within PDFs using C# in the .NET framework allows developers to add dynamic elements such as file links, web links, and audio that can execute various functions like navigating to different pages, launching external applications, or playing background music, which improves the user experience by making PDFs more functional and engaging. This article demonstrates how to use the Spire.PDF for .NET library to create actions in PDF documents with C#.
- General Steps for Adding Actions to PDF with C#
- Create Navigation Actions in PDF with C#
- Create File Launch Actions in PDF with C#
- Create Sound Actions in PDF with C#
- Create Web Link Actions in PDF with C#
- Create JavaScript Actions in PDF with C#
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
General Steps for Adding Actions to PDF with C#
Adding actions to a PDF using C# involves integrating interactive elements that enhance user experience, such as navigation buttons, file links, web links, or sound triggers. With the Spire.PDF for .NET library, developers can create various actions in PDF documents with C# code. Below is a table of the classes for commonly used actions and their descriptions:
| Class | Decryption |
| PdfGoToAction | Represents an action that navigates to a destination within the current document. |
| PdfLaunchAction | Represents an action that opens a file. |
| PdfSoundAction | Represents an action that plays a sound. |
| PdfJavaScriptAction | Represents an action that executes JavaScript code in a PDF document. |
| PdfUriAction | Represents an action that resolves a Uniform Resource Identifier (URI). |
| PdfGoToAction | Represents an action that navigates to a destination within the current document. |
For more action classes and their descriptions, refer to Spire.PDF for .NET action API references.
Actions can be added to PDF documents in two primary ways:
1. Using Action Annotations
This method involves creating an action and linking it to an annotation on the page. The action is displayed and triggered when the annotation is clicked.
General Steps:
- Create an instance of PdfDocument class and load a PDF document using PdfDocument.LoadFromFile() method.
- Get a page using PdfDocument.Pages[] property.
- Create an instance of the class that represents the action and set the action properties.
- Create an instance of PdfActionAnnotation class in a rectangular area on the page using the action.
- Add the cue word for the action to the page (optional).
- Add the action annotation to the page using PdfPageBase.Annotations.Add() method.
- Save the result document using PdfDocument.SaveToFile() method.
2. Assigning Actions to Document Events
Actions can also be assigned to document-level events such as opening, closing, or printing the document. These actions are triggered automatically when the specified events occur.
General Steps:
- Create an instance of PdfDocument class and load a PDF document using PdfDocument.LoadFromFile() method.
- Create an instance of the class that represents the action and set the action properties.
- Assign the action to a document event using the following properties:
- PdfDocument.AfterOpenAction
- PdfDocument.AfterSaveAction
- PdfDocument.AfterPrintAction
- PdfDocument.BeforeCloseAction
- PdfDocument.BeforeSaveAction
- PdfDocument.BeforePrintAction
- Save the result document using PdfDocument.SaveToFile() method.
Create Navigation Actions in PDF with C#
Navigation actions can be created using the PdfGoToAction class, which defines navigation within the document to a specified destination. To achieve this, developers can create a PdfDestination object and pass it as a parameter to a PdfGoToAction instance.
The following is a code example of adding a navigation action to a PDF:
- C#
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Annotations;
using Spire.Pdf.General;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace AddNavigationButtonPDF
{
class Program
{
static void Main(string[] args)
{
// Create an instance of PdfDocument
PdfDocument pdf = new PdfDocument();
// Load a PDF file
pdf.LoadFromFile("Sample.pdf");
// Create a PdfDestination instance and set the destination
PdfDestination destination = new PdfDestination(pdf.Pages[1]);
destination.Location = new PointF(0, 0);
destination.Mode = PdfDestinationMode.Location;
destination.Zoom = 0.8f;
// Create a PdfGoToAction based on the destination
PdfGoToAction action = new PdfGoToAction(destination);
// Create a rectangle and draw it on the first page
RectangleF rect = new RectangleF(70, pdf.PageSettings.Size.Height - 120, 140, 20);
pdf.Pages[0].Canvas.DrawRectangle(PdfBrushes.LightGray, rect);
// Draw cue words on the rectangle
PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 14);
PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center);
pdf.Pages[0].Canvas.DrawString("To Page 2", font, PdfBrushes.Green, rect, stringFormat);
// Create a PdfActionAnnotation instance based on the rectangle and the action
PdfActionAnnotation actionAnnotation = new PdfActionAnnotation(rect, action);
// Add the action annotation to the first page
pdf.Pages[0].Annotations.Add(actionAnnotation);
// Save the document
pdf.SaveToFile("output/NavigationButton.pdf");
pdf.Close();
}
}
}

Create File Launch Actions in PDF with C#
The PdfLaunchAction class defines a file open action in a PDF that enables users to open a specific file by clicking a button embedded on a PDF page. When implementing this action, developers can set the file path (absolute or relative) and decide whether the file should open in a new window. Here is a code example for adding a file launch action in a PDF document:
- C#
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace AddFileLaunchActionPDF
{
class Program
{
static void Main(string[] args)
{
// Create an instance of PdfDocument
PdfDocument pdf = new PdfDocument();
// Load a PDF file
pdf.LoadFromFile("Sample.pdf");
// Get the first page
PdfPageBase page = pdf.Pages[0];
// Draw a rectangle on the page
RectangleF rect = new RectangleF(50, 50, 180, 20);
page.Canvas.DrawRectangle(PdfBrushes.LightGray, rect);
// Darw the cue words in the rectangle
PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 14);
PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center);
pdf.Pages[0].Canvas.DrawString("Click to launch Sample2", font, PdfBrushes.Green, rect, stringFormat);
// Create a PdfLaunchAction instance
PdfLaunchAction action = new PdfLaunchAction("C:/Sample2.pdf", PdfFilePathType.Absolute);
// Set the launch mode to open in new window
action.IsNewWindow = true;
// Create a PdfActionAnnotation instance based on the rectangle and the launch action
PdfActionAnnotation actionAnnotation = new PdfActionAnnotation(rect, action);
// Add the action annotation to the first page
page.Annotations.Add(actionAnnotation);
// Save the document
pdf.SaveToFile("output/LaunchAction.pdf");
pdf.Close();
}
}
}

Create Sound Actions in PDF with C#
Developers can embed audio as an action in PDF documents with the PdfSoundAction class, enabling the audio to play in response to specific triggers, such as opening the file or clicking a button. Below is a code example for creating a sound action in a PDF document:
- C#
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
using Spire.Pdf.General;
using System.Drawing;
namespace AddSoundActionPDF
{
class Program
{
static void Main(string[] args)
{
// Create an instance of PdfDocument
PdfDocument pdf = new PdfDocument();
// Load a PDF file
pdf.LoadFromFile("Sample.pdf");
// Get the first page
PdfPageBase page = pdf.Pages[0];
// Darw the cue image on the page
PdfImage image = PdfImage.FromFile("Sound.png");
page.Canvas.DrawImage(image, new PointF(30, 30));
// Create a PdfSoundAction instance and set its property
PdfSoundAction action = new PdfSoundAction("Wave.wav");
// Set the sound parameters
action.Sound.Bits = 16;
action.Sound.Channels = PdfSoundChannels.Stereo;
action.Sound.Encoding = PdfSoundEncoding.Signed;
action.Sound.Rate = 44100;
// Set the play options
action.Volume = 0;
action.Repeat = true;
action.Mix = true;
action.Synchronous = true;
// Create a PdfActionAnnotation instance using the sound action at the location of the cue image
RectangleF rect = new RectangleF(30, 30, image.Width, image.Height);
PdfActionAnnotation actionAnnotation = new PdfActionAnnotation(rect, action);
// Add the action annotation to the first page
page.Annotations.Add(actionAnnotation);
// Set the sound action to play after the document is opened
pdf.AfterOpenAction = action;
// Save the document
pdf.SaveToFile("output/SoundAction.pdf");
pdf.Close();
}
}
}

Create Web Link Actions in PDF with C#
Developers can use the PdfUriAction class to create a web link action in PDF documents, allowing users to open a web link when performing specific actions, such as clicking a button. Below is a code example for creating a web link action in a PDF document:
- C#
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace AddSoundActionPDF
{
class Program
{
static void Main(string[] args)
{
// Create an instance of PdfDocument
PdfDocument pdf = new PdfDocument();
// Load a PDF file
pdf.LoadFromFile("Sample.pdf");
// Get the first page
PdfPageBase page = pdf.Pages[0];
// Draw a rectangle on the page
RectangleF rect = new RectangleF(30, 30, 120, 20);
page.Canvas.DrawRectangle(PdfBrushes.LightGray, rect);
// Draw the cue words in the rectangle
PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 14);
PdfStringFormat stringFormat = new PdfStringFormat(PdfTextAlignment.Center);
page.Canvas.DrawString("Go to Google Search", font, PdfBrushes.LightSkyBlue, rect);
// Create a PdfUriAction instance and set its property
PdfUriAction action = new PdfUriAction();
action.Uri = "https://www.google.com/";
// Create a PdfActionAnnotation instance using the web link action and the rectangle
PdfActionAnnotation actionAnnotation = new PdfActionAnnotation(rect, action);
// Add the action annotation to the first page
page.Annotations.Add(actionAnnotation);
// Save the document
pdf.SaveToFile("output/WebLinkAction.pdf");
pdf.Close();
}
}
}

Create JavaScript Actions in PDF with C#
The PdfJavaScriptAction represents a JavaScript action in PDF, which allows developers to create complex interactions such as form validations, data calculations, and custom user interfaces. Below is a code example for adding a simple JavaScript action to a PDF document with C#:
- C#
using Spire.Pdf;
using Spire.Pdf.Actions;
namespace AddJavaScriptActionPDF
{
class Program
{
static void Main(string[] args)
{
// Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
// Load a PDF file
pdf.LoadFromFile("Sample.pdf");
// Define JavaScript code
var jsCode =
"app.alert({" +
" cMsg: 'Welcome to the article about the Evolution and Advantages of LCD Screens!\\n\\nThis article explores the history and benefits of LCD technology, including its impact on visual experiences across various devices.', " +
" nIcon: 3, " +
" cTitle: 'Document Introduction'" +
"});";
// Create a PdfJavaScriptAction instance using the JavaScript code
PdfJavaScriptAction action = new PdfJavaScriptAction(jsCode);
// Set the action to be executed when the PDF document is launched
pdf.AfterOpenAction = action;
// Save the document
pdf.SaveToFile("output/PDFJavaScriptAction.pdf");
pdf.Close();
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
A navigation button in a PDF document can redirect users from one page to another. For instance, when a navigation button is clicked, we can jump to the next/last page or return to the previous/first page. This article will introduce how to create such a navigation button in PDF using Spire.PDF.
Step 1: Initialize an instance of PdfDocument class and load a PDF document.
PdfDocument doc = new PdfDocument(); doc.LoadFromFile(@"C:\Users\Administrator\Desktop\Sales Report.pdf");
Step 2: Enable creation of form fields in the document.
doc.AllowCreateForm = true;
Step 3: Get the last page.
PdfPageBase page = doc.Pages[doc.Pages.Count-1];
Step 4: Create a button field and specify the button name, tooltip and border style.
PdfButtonField button = new PdfButtonField(page, "Return to First Page"); button.Bounds = new RectangleF(page.ActualSize.Width/2-50, 400, 100, 20); button.BorderColor = new PdfRGBColor(Color.AliceBlue); button.BorderStyle = PdfBorderStyle.Solid; button.ToolTip = "First Page"; button.Font = new PdfFont(PdfFontFamily.Helvetica, 7f,PdfFontStyle.Bold);
Step 5: Create a PdfNamedAction that goes to the named destination (previous, next, first or last page) and add the action to the button field.
PdfNamedAction namedAction = new PdfNamedAction(PdfActionDestination.FirstPage); button.Actions.GotFocus = namedAction;
Step 6: Add the button to the document.
doc.Form.Fields.Add(button);
doc.SaveToFile("NavigationButton.pdf", FileFormat.PDF);
Output:

Full Code:
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace NavigationButtons
{
class Program
{
static void Main(string[] args)
{
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\Sales Report.pdf");
doc.AllowCreateForm = true;
PdfPageBase page = doc.Pages[doc.Pages.Count - 1];
PdfButtonField button = new PdfButtonField(page, "Return to First Page");
button.Bounds = new RectangleF(page.ActualSize.Width / 2 - 50, 400, 100, 20);
button.BorderColor = new PdfRGBColor(Color.AliceBlue);
button.BorderStyle = PdfBorderStyle.Solid;
button.ToolTip = "First Page";
button.Font = new PdfFont(PdfFontFamily.Helvetica, 7f, PdfFontStyle.Bold);
PdfNamedAction namedAction = new PdfNamedAction(PdfActionDestination.FirstPage);
button.Actions.GotFocus = namedAction;
doc.Form.Fields.Add(button);
doc.SaveToFile("NavigationButton.pdf", FileFormat.PDF);
}
}
}
Imports Spire.Pdf
Imports Spire.Pdf.Actions
Imports Spire.Pdf.Fields
Imports Spire.Pdf.Graphics
Imports System.Drawing
Namespace NavigationButtons
Class Program
Private Shared Sub Main(args As String())
Dim doc As New PdfDocument()
doc.LoadFromFile("C:\Users\Administrator\Desktop\Sales Report.pdf")
doc.AllowCreateForm = True
Dim page As PdfPageBase = doc.Pages(doc.Pages.Count - 1)
Dim button As New PdfButtonField(page, "Return to First Page")
button.Bounds = New RectangleF(page.ActualSize.Width / 2 - 50, 400, 100, 20)
button.BorderColor = New PdfRGBColor(Color.AliceBlue)
button.BorderStyle = PdfBorderStyle.Solid
button.ToolTip = "First Page"
button.Font = New PdfFont(PdfFontFamily.Helvetica, 7F, PdfFontStyle.Bold)
Dim namedAction As New PdfNamedAction(PdfActionDestination.FirstPage)
button.Actions.GotFocus = namedAction
doc.Form.Fields.Add(button)
doc.SaveToFile("NavigationButton.pdf", FileFormat.PDF)
End Sub
End Class
End Namespace
Adding print button to PDF document is possible in Spire.PDF, when the button is clicked, the print dialog will be opened. In this tutorial, I'm going to show you how to add a print button to an existing pdf document using Spire.PDF.
To accomplish the task, first you need to create an instance of the PdfButtonField class. This class also allows you to define the appearance of the button. For instance, you can set text, color of text, background color, border color etc. of the button. Second you need to add the print action to the button by calling the AddPrintAction() method. At last, add the button to the document and save the document to file. The following code example explains the same.
Code snippets:
Step 1: Load the PDF document and enable form creation.
PdfDocument doc = new PdfDocument("Input.pdf");
doc.AllowCreateForm = true;
Step 2: Get the first page.
PdfPageBase page = doc.Pages[0];
Step 3: Create a PdfButtonField instance and set properties for the button.
PdfButtonField button = new PdfButtonField(page, "Print"); button.Bounds = new RectangleF(280, 600, 50, 20); button.BorderColor = new PdfRGBColor(Color.AliceBlue); button.BorderStyle = PdfBorderStyle.Solid; button.ForeColor = new PdfRGBColor(Color.White); button.BackColor = new PdfRGBColor(Color.Blue); button.ToolTip = "Print"; button.Font = new PdfFont(PdfFontFamily.Helvetica, 9f);
Step 4: Add print action to the button.
button.AddPrintAction();
Step 5: Add the button to the document.
doc.Form.Fields.Add(button);
Step 6: Save the document.
doc.SaveToFile("Output.pdf");
The resultant document looks as follows:

Full code:
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;
namespace Add_print_button
{
class Program
{
static void Main(string[] args)
{
//Load the PDF document
PdfDocument doc = new PdfDocument("Input.pdf");
//Enable form creation
doc.AllowCreateForm = true;
//Get the first page
PdfPageBase page = doc.Pages[0];
//Create a PdfButtonField instance
PdfButtonField button = new PdfButtonField(page, "Print");
//Set button properties
button.Bounds = new RectangleF(280, 600, 50, 20);
button.BorderColor = new PdfRGBColor(Color.AliceBlue);
button.BorderStyle = PdfBorderStyle.Solid;
button.ForeColor = new PdfRGBColor(Color.White);
button.BackColor = new PdfRGBColor(Color.Blue);
button.ToolTip = "Print";
button.Font = new PdfFont(PdfFontFamily.Helvetica, 9f);
//Add print action to the button
button.AddPrintAction();
//Add the button to document
doc.Form.Fields.Add(button);
//Save the document
doc.SaveToFile("Output.pdf");
}
}
}
Spire.PDF allows us to reset the values of PDF form fields using the PdfResetAction. The following code example demonstrates how we can use Spire.PDF to implement this function.
Code snippet:
Step 1: Create a PDF document and add a new page to it.
PdfDocument document = new PdfDocument(); PdfPageBase page = document.Pages.Add();
Step 2: Create a text box field, set properties for the text box field and add it to the document.
PdfTextBoxField textBoxField = new PdfTextBoxField(page, "Name"); textBoxField.BorderColor = new PdfRGBColor(Color.AliceBlue); textBoxField.BorderStyle = PdfBorderStyle.Solid; textBoxField.Bounds = new RectangleF(50, 50, 100, 20); textBoxField.Text = "Shawn Smith"; document.Form.Fields.Add(textBoxField);
Step 3: Create a button field, set properties for the button field and add it to the document.
PdfButtonField button = new PdfButtonField(page, "Reset"); button.Bounds = new RectangleF(80, 100, 50, 20); button.BorderColor = new PdfRGBColor(Color.AliceBlue); button.BorderStyle = PdfBorderStyle.Solid; button.ToolTip = "Reset"; button.Font = new PdfFont(PdfFontFamily.Helvetica, 9f); document.Form.Fields.Add(button);
Step 4: Create a reset action for the button field using PdfResetAction class.
PdfResetAction resetAction = new PdfResetAction(); button.Actions.GotFocus = resetAction;
Step 5: Save and close the PDF document.
document.SaveToFile("Output.pdf");
document.Close();
Screenshot before and after resetting value:

Full code:
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;
namespace Reset_form_fields_in_PDF
{
class Program
{
static void Main(string[] args)
{
//Create a PDF document
PdfDocument document = new PdfDocument();
//Add a new page
PdfPageBase page = document.Pages.Add();
//Create a text box field.
PdfTextBoxField textBoxField = new PdfTextBoxField(page, "Name");
//Set properties for the text box field.
textBoxField.BorderColor = new PdfRGBColor(Color.AliceBlue);
textBoxField.BorderStyle = PdfBorderStyle.Solid;
textBoxField.Bounds = new RectangleF(50, 50, 100, 20);
textBoxField.Text = "Shawn Smith";
//Add the text box field to the document
document.Form.Fields.Add(textBoxField);
//Create a button field.
PdfButtonField button = new PdfButtonField(page, "Reset");
//Set properties for the button field
button.Bounds = new RectangleF(80, 100, 50, 20);
button.BorderColor = new PdfRGBColor(Color.AliceBlue);
button.BorderStyle = PdfBorderStyle.Solid;
button.ToolTip = "Reset";
button.Font = new PdfFont(PdfFontFamily.Helvetica, 9f);
//Add the button field to the document
document.Form.Fields.Add(button);
//Create a reset action for the button field
PdfResetAction resetAction = new PdfResetAction();
button.Actions.GotFocus = resetAction;
//Save and close the PDF document
document.SaveToFile("Output.pdf");
document.Close();
}
}
}
A Sound action is used to embed and play a sound file in PDF document. In Spire.PDF, we can create a sound action by using the PdfSoundAction class. Attributes like sound, volume and repeat can be specified for the sound action.
Refer below code example:
Step 1: Create a new PDF document and add a page to it.
PdfDocument document = new PdfDocument(); PdfPageBase page = document.Pages.Add();
Step 2: Create a sound action and set its attributes.
PdfSoundAction soundAction = new PdfSoundAction(@"C:\Users\Administrator\Desktop\because of you.wav"); soundAction.Sound.Bits = 15; soundAction.Sound.Channels = PdfSoundChannels.Stereo; soundAction.Sound.Encoding = PdfSoundEncoding.Signed; soundAction.Volume = 0.8f; soundAction.Repeat = true;
Step 3: Set the sound action to be executed when the PDF document is opened.
document.AfterOpenAction = soundAction;
Step 4: Save and close the PDF document.
document.SaveToFile("Output.pdf");
document.Close();
Full code:
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.General;
namespace PDF_Sound_Action
{
class Program
{
static void Main(string[] args)
{
//Create a new PDF document
PdfDocument document = new PdfDocument();
//Add a page
PdfPageBase page = document.Pages.Add();
//Create a sound action
PdfSoundAction soundAction = new PdfSoundAction(@"C:\Users\Administrator\Desktop\because of you.wav");
soundAction.Sound.Bits = 15;
soundAction.Sound.Channels = PdfSoundChannels.Stereo;
soundAction.Sound.Encoding = PdfSoundEncoding.Signed;
soundAction.Volume = 0.8f;
soundAction.Repeat = true;
// Set the sound action to be executed when the PDF document is opened
document.AfterOpenAction = soundAction;
//Save and close the PDF document
document.SaveToFile("Output.pdf");
document.Close();
}
}
}
PDF actions can refer to external files by using either absolute or relative file paths. While using the relative path in action, the file can be moved into a different location. In Spire.PDF, we can use the PdfLaunchAction to link to files with relative paths.
Code snippet:
Step 1: Create a new PDF document and add a page to it.
PdfDocument document = new PdfDocument(); PdfPageBase page = document.Pages.Add();
Step 2: Create a PDF launch action that refers to an external file with relative path.
PdfLaunchAction launchAction = new PdfLaunchAction(@"..\..\test.txt", PdfFilePathType.Relative);
Step 3: Create a PDF action annotation with the launch action. Add the annotation to the page.
string text = "Click here to open file with relative path";
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 13f));
RectangleF rect = new RectangleF(50, 50, 230, 15);
page.Canvas.DrawString(text, font, PdfBrushes.ForestGreen, rect);
PdfActionAnnotation annotation = new PdfActionAnnotation(rect, launchAction);
(page as PdfNewPage).Annotations.Add(annotation);
Step 4: Save the document.
document.SaveToFile("RelativeFilePath.pdf");
Screenshot:

Full code:
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;
namespace Link_to_files_with_relative_paths
{
class Program
{
static void Main(string[] args)
{
//Create a new PDF document and add a page to it
PdfDocument document = new PdfDocument();
PdfPageBase page = document.Pages.Add();
//Create a PDF Launch Action
PdfLaunchAction launchAction = new PdfLaunchAction(@"..\..\test.txt", PdfFilePathType.Relative);
//Create a PDF Action Annotation with the PDF Launch Action
string text = "Click here to open file with relative path";
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 13f));
RectangleF rect = new RectangleF(50, 50, 230, 15);
page.Canvas.DrawString(text, font, PdfBrushes.ForestGreen, rect);
PdfActionAnnotation annotation = new PdfActionAnnotation(rect, launchAction);
//Add the PDF Action Annotation to page
(page as PdfNewPage).Annotations.Add(annotation);
//Save and close the document
document.SaveToFile("RelativeFilePath.pdf");
document.Close();
}
}
}
The GoToE (or embedded go-to) action is similar to a remote go-to action but allows jumping to a PDF file that is embedded in another PDF file. With Spire.PDF, it’s possible to create a GoToE action to direct from the current PDF file to the embedded PDF file. This article demonstrates how to accomplish this goal programmatically with C#.
At first, it's necessary to introduce you the parameters of public PdfEmbeddedGoToAction(string fileName, PdfDestination dest, bool newWindow);
Parameters:
- fileName - the name of the target file.
- dest - the destination inside the target file.
- newWindow - if true open the file in a new window, if false the current file is replaced by the new file.
Now Follow below Detail steps:
Step 1: Create a PDF file and add a page to it.
PdfDocument pdf = new PdfDocument(); PdfPageBase page = pdf.Pages.Add();
Step 2: Attach a PDF file to the newly created file.
PdfAttachment attachment = new PdfAttachment("New Zealand.pdf");
pdf.Attachments.Add(attachment);
Step 3: Draw text on the page.
string text = "Test embedded go-to action! Click this will open the attached PDF in a new window.";
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 13f));
float width = 490f;
float height = font.Height * 2.2f;
RectangleF rect = new RectangleF(0, 100, width, height);
page.Canvas.DrawString(text, font, PdfBrushes.Black, rect);
Step 4: Create an embedded go-to action (/GoToE) which allows jumping to the attached PDF file and open it in a new window at the 2nd page and 200% zoom factor.
PdfDestination dest = new PdfDestination(1, new PointF(0, 842), 2f); PdfEmbeddedGoToAction action = new PdfEmbeddedGoToAction(attachment.FileName, dest, true);
Step 5: Create an action annotation with the embedded go-to action and then add it to the page.
PdfActionAnnotation annotation = new PdfActionAnnotation(rect, action); (page as PdfNewPage).Annotations.Add(annotation);
Step 6: Save the document.
pdf.SaveToFile("result.pdf");
Effective Screenshot:

Full code:
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Annotations;
using Spire.Pdf.Attachments;
using Spire.Pdf.General;
using Spire.Pdf.Graphics;
namespace Create_A_GoToE_Action
{
class Program
{
static void Main(string[] args)
{
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.Pages.Add();
PdfAttachment attachment = new PdfAttachment("New Zealand.pdf");
pdf.Attachments.Add(attachment);
string text = "Test embedded go-to action! Click this will open the attached PDF in a new window.";
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 13f));
float width = 490f;
float height = font.Height * 2.2f;
RectangleF rect = new RectangleF(0, 100, width, height);
page.Canvas.DrawString(text, font, PdfBrushes.Black, rect);
PdfDestination dest = new PdfDestination(1, new PointF(0, 842), 2f);
PdfEmbeddedGoToAction action = new PdfEmbeddedGoToAction(attachment.FileName, dest, true);
PdfActionAnnotation annotation = new PdfActionAnnotation(rect, action);
(page as PdfNewPage).Annotations.Add(annotation);
pdf.SaveToFile("result.pdf");
}
}
}
Generally, when we open a PDF document from a PDF viewer, it displays the first page instead of others. For some reasons, we may want to skip the first few pages and start on another page. This article will introduce how to specify a particular page when viewing a PDF document in C# and VB.NET.
Code Snippet:
Step 1: Initialize an instance of PdfDocument class and a load a sample PDF file.
PdfDocument doc = new PdfDocument(); doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");
Step 2: Create a PdfGoToAction which will redirect to a destination (page 2 in this case) in the current document.
PdfDestination destination = new PdfDestination(doc.Pages[1]); PdfGoToAction action = new PdfGoToAction(destination);
Step 3: Sets the action to execute after the document is opened.
doc.AfterOpenAction = action;
Step 4: Save the file.
doc.SaveToFile("GoTo2Page.pdf",FileFormat.PDF);
Output:
The document opens at the second page.

Full Code:
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.General;
namespace OpenPDF
{
class Program
{
static void Main(string[] args)
{
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");
PdfDestination destination = new PdfDestination(doc.Pages[1]);
PdfGoToAction action = new PdfGoToAction(destination);
action.Destination.Zoom = 0.8F;
doc.AfterOpenAction = action;
doc.SaveToFile("GoTo2Page.pdf", FileFormat.PDF);
}
}
}
Imports Spire.Pdf
Imports Spire.Pdf.Actions
Imports Spire.Pdf.General
Namespace OpenPDF
Class Program
Private Shared Sub Main(args As String())
Dim doc As New PdfDocument()
doc.LoadFromFile("C:\Users\Administrator\Desktop\sample.pdf")
Dim destination As New PdfDestination(doc.Pages(1))
Dim action As New PdfGoToAction(destination)
action.Destination.Zoom = 0.8F
doc.AfterOpenAction = action
doc.SaveToFile("GoTo2Page.pdf", FileFormat.PDF)
End Sub
End Class
End Namespace
PDF supports actions. And Spire.PDF, a very powerful .NET component enables developers to add action chain to PDF file. Action chain means an action get executed automatically after another one.
In this article, a solution is introduced to add action chain to PDF file.
Step 1: Set the action that gets performed right after PDF document is opened
String script
= "app.alert({"
+ " cMsg: \"I'll lead; you must follow me.\","
+ " nIcon: 3,"
+ " cTitle: \"JavaScript Action\""
+ "});";
PdfJavaScriptAction action1 = new PdfJavaScriptAction(script);
document.AfterOpenAction = action1;
Spire.PDF provides you a class called PdfJavaScriptAction that executes JavaScript code. Create a PdfJavaScriptAction instance “action1” using JavaScript code. And set the property AfterOpenAction of “document” to action1.
Step 2: Set the action that gets performed after “action1” using the property NextAction
script
= "app.alert({"
+ " cMsg: \"The firt page!\","
+ " nIcon: 3,"
+ " cTitle: \"JavaScript Action\""
+ "});";
PdfJavaScriptAction action2 = new PdfJavaScriptAction(script);
action1.NextAction = action2;
Step 3: Set the action that gets performed after “action2”
PdfDestination dest = new PdfDestination(pagetwo); dest.Zoom = 1; PdfGoToAction action3 = new PdfGoToAction(dest); action2.NextAction = action3;
PdfDestination can mark a specified page or location in PDF. Create a PdfDestination instance “dest” using “pagetwo”. Then create a PdfGoToAction instance “action3” using “dest”.
Step 4: Set the action that gets performed after “action3”
script
= "app.alert({"
+ " cMsg: \"Oh sorry, it's the last page. I'm missing!\","
+ " nIcon: 3,"
+ " cTitle: \"JavaScript Action\""
+ "});";
PdfJavaScriptAction action4 = new PdfJavaScriptAction(script);
action3.NextAction = action4;
Step 5: Save the file
document.SaveToFile("result.pdf");
Full code:
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.General;
using Spire.Pdf.Graphics;
using System;
using System.Drawing;
namespace AddActionChain
{
class Program
{
static void Main(string[] args)
{
PdfDocument document = new PdfDocument();
PdfPageBase pageone = document.Pages.Add();
pageone.Canvas.DrawString("This is Page One.",
new PdfFont(PdfFontFamily.Helvetica, 20f),
new PdfSolidBrush(Color.Black),
10, 10);
PdfPageBase pagetwo = document.Pages.Add();
pagetwo.Canvas.DrawString("This is Page Two.",
new PdfFont(PdfFontFamily.Helvetica, 20f),
new PdfSolidBrush(Color.Black),
10, 10);
String script
= "app.alert({"
+ " cMsg: \"I'll lead; you must follow me.\","
+ " nIcon: 3,"
+ " cTitle: \"JavaScript Action\""
+ "});";
PdfJavaScriptAction action1 = new PdfJavaScriptAction(script);
document.AfterOpenAction = action1;
script
= "app.alert({"
+ " cMsg: \"The first page!\","
+ " nIcon: 3,"
+ " cTitle: \"JavaScript Action\""
+ "});";
PdfJavaScriptAction action2 = new PdfJavaScriptAction(script);
action1.NextAction = action2;
PdfDestination dest = new PdfDestination(pagetwo);
dest.Zoom = 1;
PdfGoToAction action3 = new PdfGoToAction(dest);
action2.NextAction = action3;
script
= "app.alert({"
+ " cMsg: \"Oh sorry, it's the last page. I'm missing!\","
+ " nIcon: 3,"
+ " cTitle: \"JavaScript Action\""
+ "});";
PdfJavaScriptAction action4 = new PdfJavaScriptAction(script);
action3.NextAction = action4;
document.SaveToFile("result.pdf");
System.Diagnostics.Process.Start("result.pdf");
}
}
}
Screenshot:

