How do I attach Actions (JavaScript etc) to different items?

Q: Through your assistance, I have been able to figure out how to add
a JavaScript
to be executed as soon as the document is opened. I found the function
doc.SetOpenAction() in the documentation, and you can see its usage in
the
attached source code. I do have one concern, however. The script being
inserted
by PDFNet seems to be of an anonymous kind. All my recent versions of
Acrobat
seem to prefer named scripts. I wonder whether Adobe has decided to
deprecate
those anonymous JavaScripts (since I cannot create them with Acrobat).
Since I
intend my PDFNet code to be able to run for years, I would like to
make sure that
the type of named scripts that are created by Acrobat, are the same
type that I
can generate programmatically with PDFNet.

The "/OpenAction" construct in the Catalog (see last example in the
PDF file contained
in my previous message) is perhaps a remnant of the past?

Bottom line:

Can you please let me know how to add named "Document JavaScripts"
using PDFNet?
---
A: Storing ‘named’ JavaScript actions in a document’s ‘name tree’ is
a good way to keep all JavaScript code easily accessible from a single
location, however there is nothing else to it. There is no requirement
in PDF spec that all JavaScript must be listed under JavaScript and
fundamentally it is essentially the same mechanism.

In case you would like to list your JavaScript code in a document’s
‘name tree’ you can use pdftron.SDF.NameTree as follows:

// In C# (VB.NET/JAVA is along the same lines):

PDFNet.Initialize();

// Open PDF
PDFDoc doc = new PDFDoc("my.pdf");
doc.InitSecurityHandler();

// Get JavaScript ‘Name Tree’.
NameTree js_map = NameTree.Find(doc, "JavaScript");
if (!js_map.IsValid()) {
  js_map = NameTree.Create(doc, "JavaScript");
}

// Create a JavaScript action.
Obj act_dict = doc.CreateIndirectDict();
act_dict.PutName("S", "JavaScript");
act_dict.PutString("JS", "app.alert(' Hello, -With Open Action- World!
');");

// Add the JavaScript action to the name tree
js_map.Put(new System.Text.UTF8Encoding().GetBytes("MyKey"),
act_dict);

doc.SetOpenAction(new Action(act_dict));
doc.Save("my.pdf", SDFDoc.SaveOptions.e_linearized);
doc.Close();

In C++:

PDFNet::Initialize();

// Open PDF
PDFDoc doc("my.pdf");
doc.InitSecurityHandler();

// Get JavaScript ‘Name Tree’.
NameTree js_map = NameTree::Find(doc, "JavaScript");
if (!js_map.IsValid()) {
  js_map = NameTree::Create(doc, "JavaScript");
}

// Create a JavaScript action.
Obj act_dict = doc.CreateIndirectDict();
act_dict.PutName("S", "JavaScript");
act_dict.PutString("JS", "app.alert(' Hello, -With Open Action- World!
');");

// Add the JavaScript action to the name tree
const char* key = "MyKey";
js_map.Put((UChar*)key, strlen(key), act_dict);

Action open_act(act_dict);
doc.SetOpenAction(open_act);
doc.Save("my.pdf", 0, 0);

------------------

To associate a JavaScript action with a Widget or an annotation (e.g.
a link) you could use the following line:

annot.GetSDFObj().Put("A", act_dict);