![]() | Get access, create and manipulate actions |
This topic contains the following sections:
Patagames PDF SDK provides APIs to create a series of actions and get the action handlers, such as remote or embedded goto action, named action, launch action and JavaScript action, etc.
Action Type | Description | Supported by SDK | Supported by Viewer |
---|---|---|---|
Go to a destination in the current document | Yes | Yes | |
(“Go-to remote”) Go to a destination in another document | Yes | Yes | |
(“Go-to embedded”) Go to a destination in an embedded file | Yes | Yes | |
Launch an application, usually to open a file. | Yes | Yes | |
Begin reading an article thread. | Yes | No | |
Resolve a uniform resource identifier | Yes | Yes | |
Play a sound | Yes | No | |
Play a movie | No | No | |
Set an annotation’s Hidden flag | Yes | No | |
Execute an action predefined by the viewer application | Yes | No | |
Send data to a uniform resource locator | Yes | Yes | |
Set fields to their default values | Yes | Yes | |
Import field values from a file | Yes | Yes | |
Execute a JavaScript script | Yes | Yes | |
Set the states of optional content groups | Yes | Yes | |
Controls the playing of multimedia content | No | No | |
Updates the display of a document, using a transition dictionary | Yes | No | |
Set the current view of a 3D annotation | No | No |
![]() |
---|
Lack of support does not mean that these actions are not available at all. You can create, access and manipulate such actions using dictionaries in accordance with the PDF specification. |
The action classes are contained in the Patagames.Pdf.Net.Actions namespace and are grouped into a tree structure
A document-level open action is an action taken when a document is opened.
using (var doc = PdfDocument.Load("sample.pdf")) { //Create a destination int pageIndex = 2; var destination = PdfDestination.CreateFit(doc, pageIndex); //Create a go-to action and insert it into document doc.OpenAction = new PdfGoToAction(doc, destination); //Save a copy of PDF document doc.Save("sample_with_action.pdf", SaveFlags.NoIncremental); }
using (var doc = PdfDocument.Load("sample.pdf")) { var page = doc.Pages[0]; if (page.Annots == null) page.CreateAnnotations(); //Add a text object to the page. var textObject = PdfTextObject.Create("Click me to navigate to https://patagames.com", 0, 0, PdfFont.CreateStock(page.Document, FontStockNames.Arial), 14); textObject.Location = new FS_POINTF(50, page.Height - textObject.BoundingBox.Height - 50); page.PageObjects.Add(textObject); page.GenerateContent(); // Create link annotation over the text object var annot = new PdfLinkAnnotation(page); var bbox = textObject.BoundingBox; bbox.Inflate(new FS_RECTF(10, 10, 10, 10)); annot.Rectangle = bbox; annot.Link.QuadPoints.Add(new FS_QUADPOINTSF(bbox)); //Create a URL action annot.Link.Action = new PdfUriAction(doc, "https://patagames.com"); page.Annots.Add(annot); //Save a copy of PDF document doc.Save("sample_with_action.pdf", SaveFlags.NoIncremental); }
using (var doc = PdfDocument.Load("sample.pdf")) { var page = doc.Pages[0]; if (page.Annots == null) page.CreateAnnotations(); //Add a text object to the page. var textObject = PdfTextObject.Create(@"Click me to go to the 2nd page of the remote document", 0, 0, PdfFont.CreateStock(page.Document, FontStockNames.Arial), 14); textObject.Location = new FS_POINTF(50, page.Height - textObject.BoundingBox.Height - 50); page.PageObjects.Add(textObject); page.GenerateContent(); // Create link annotation over the text object var annot = new PdfLinkAnnotation(page); var bbox = textObject.BoundingBox; bbox.Inflate(new FS_RECTF(10, 10, 10, 10)); annot.Rectangle = bbox; annot.Link.QuadPoints.Add(new FS_QUADPOINTSF(bbox)); //Create file specification var fileSpec = new PdfFileSpecification(doc); fileSpec.FileName = @"c:\other_document.pdf"; //Create a destination int pageIndex = 1; var dest = PdfDestination.CreateXYZ(null, pageIndex); //Create a GoToR action annot.Link.Action = new PdfGoToRAction(doc, fileSpec, dest); page.Annots.Add(annot); //Save a copy of PDF document doc.Save("sample_with_action.pdf", SaveFlags.NoIncremental); }
using (var doc = PdfDocument.Load("sample.pdf")) { var page = doc.Pages[0]; if (page.Annots == null) page.CreateAnnotations(); //Add a text object to the page. var textObject = PdfTextObject.Create(@"Click me to go to the 2nd page of the embedded document", 0, 0, PdfFont.CreateStock(page.Document, FontStockNames.Arial), 14); textObject.Location = new FS_POINTF(50, page.Height - textObject.BoundingBox.Height - 50); page.PageObjects.Add(textObject); page.GenerateContent(); // Create link annotation over the text object var annot = new PdfLinkAnnotation(page); var bbox = textObject.BoundingBox; bbox.Inflate(new FS_RECTF(10, 10, 10, 10)); annot.Rectangle = bbox; annot.Link.QuadPoints.Add(new FS_QUADPOINTSF(bbox)); //Create PDF attachment var attachment = new PdfAttachment(doc, "other_document.pdf", System.IO.File.ReadAllBytes(@"c:\other_document.pdf")); doc.Attachments.Add(attachment); //Create a destination int pageIndex = 1; var dest = PdfDestination.CreateXYZ(null, pageIndex); //Create a GoToE action annot.Link.Action = new PdfGoToEAction(doc, attachment.FileSpecification, dest); page.Annots.Add(annot); //Save a copy of PDF document doc.Save("sample_with_action.pdf", SaveFlags.NoIncremental); }
using (var doc = PdfDocument.Load("sample.pdf")) { //Create a JS action var action = new PdfJavaScriptAction(doc, "app.alert(\"Hello world! \");"); //Insert action into a name tree that maps name strings to document-level JavaScript actions var tree = new PdfNameTreeCollection(doc, "JavaScript"); tree.Add("My Action", action.Dictionary); //Save a copy of PDF document doc.Save("sample_with_action.pdf", SaveFlags.NoIncremental); }
using (var doc = PdfDocument.Load("sample.pdf")) { //Insert action into a name tree that maps name strings to document-level JavaScript actions var tree = new PdfNameTreeCollection(doc, "JavaScript"); foreach (var item in tree) { var action = PdfAction.FromHandle(doc, item.Value.As<PdfTypeDictionary>().Handle); if (action is PdfJavaScriptAction) { var jsAction = action as PdfJavaScriptAction; string js = jsAction.JavaScript; } } }
using (var doc = PdfDocument.Load("sample.pdf")) { //Create a JS action var action = new PdfJavaScriptAction(doc, "var annot = this.addAnnot({ page : 0, type : \"Square\", rect : [ 0, 0, 100, 100 ], name : \"UniqueID\", author : \"A. C. Robat\", contents : \"This section needs revision.\" });"); //Insert action into a name tree that maps name strings to document-level JavaScript actions var tree = new PdfNameTreeCollection(doc, "JavaScript"); tree.Add("Action_CreateAnnotation", action.Dictionary); //Save a copy of PDF document doc.Save("sample_with_action.pdf", SaveFlags.NoIncremental); }