Click or drag to resize

Get access, create and manipulate actions

Overview

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

PdfGoToAction

Go to a destination in the current document

Yes

Yes

PdfGoToRAction

(“Go-to remote”) Go to a destination in another document

Yes

Yes

PdfGoToEAction

(“Go-to embedded”) Go to a destination in an embedded file

Yes

Yes

PdfLaunchAction

Launch an application, usually to open a file.

Yes

Yes

PdfThreadAction

Begin reading an article thread.

Yes

No

PdfUriAction

Resolve a uniform resource identifier

Yes

Yes

PdfSoundAction

Play a sound

Yes

No

PdfMovieAction

Play a movie

No

No

PdfHideAction

Set an annotation’s Hidden flag

Yes

No

PdfNamedAction

Execute an action predefined by the viewer application

Yes

No

PdfSubmitFormAction

Send data to a uniform resource locator

Yes

Yes

PdfResetFormAction

Set fields to their default values

Yes

Yes

PdfImportDataAction

Import field values from a file

Yes

Yes

PdfJavaScriptAction

Execute a JavaScript script

Yes

Yes

PdfOCGStateAction

Set the states of optional content groups

Yes

Yes

PdfRenditionAction

Controls the playing of multimedia content

No

No

PdfTransitionAction

Updates the display of a document, using a transition dictionary

Yes

No

Pdf3DViewAction

Set the current view of a 3D annotation

No

No

Note Note

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

Open in full size

Actions
How to create a go-to action and insert to a document as an open action.

A document-level open action is an action taken when a document is opened.

C#
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);

}
How to create a URI action and insert to a link annot
C#
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);
}
How to create a remote go-to action and insert to a link annot
C#
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);
}
How to create an embedded go-to action and insert to a link annot
C#
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);
}
How to add JavaScript Action to Document
C#
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);
}
How to enumerate all the document-level JavaScript Actions
C#
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.As<PdfTypeDictionary>().Handle);
        if (action is PdfJavaScriptAction)
        {
            var jsAction = action as PdfJavaScriptAction;
            string js = jsAction.JavaScript;
        }
    }
}
How to add a new annotation to PDF using JavaScript
C#
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);
}
See Also

Reference

Other Resources