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:


