Q: I would like to programatically insert Javascript in the click
event of a checkbox of a pdf file generated with Adobe Livecycle
Designer 8.2. In addition I would like to add a string variable
(which creates a Variables node) followed by
inserting a script object under the created Variable node.
This allows me to create javascript functions, global variables for
those functions, and allows me to reference these
functions from widget actions.
Can PDFNet do this, or at least some of this?
I tried to use your example snipit of adding a javascript \'mouse up\'
action trigger event on a checkbox widget in a pdf
generated by Adobe Livecycle Designer 8.2. This did not create the
app.alert when the checkbox was checked in Preview Mode.
The script was also not found in the script editor of Livecycle
Designer.
The code I used:
PDFNet.Initialize();
doc = new PDFDoc(@\"C:\\a\\PdfScript\\PdfScript\\test.pdf\");
doc.InitSecurityHandler();
PageIterator itr = doc.GetPageIterator(); for (; itr.HasNext();
itr.Next()) {
Page page = itr.Current();
int pageNumber = page.GetIndex();
int num_annots = page.GetNumAnnots();
for (int i = 0; i < num_annots; ++i)
{
Annot annot = page.GetAnnot(i);
if (annot.IsValid() == false) continue;
if (annot.GetType() == Annot.Type.e_Widget)
{
Obj aa_dict = annot.GetSDFObj().PutDict(\"AA\");
Obj alert_js = doc.CreateIndirectDict();
alert_js.PutName(\"S\", \"JavaScript\");
alert_js.PutString(\"JS\", \"app.alert(\' Hello World! \');
\");
// Associate this JS action with \'On Mouse Up\' event.
aa_dict.Put(\"U\", alert_js);
}
}
}
doc.Save(@\"C:\\a\\PdfScript\\PdfScript\\test_out.pdf\",
SDFDoc.SaveOptions.e_linearized); doc.Close();
Thanks for your help.
--------------
A: It is possible that XFA information is not in sync with AcroForms.
In this case removing XFA from AcroForms dictionary or sync XFA with
AcroForms. For example:
Obj acro_form = pdfdoc.GetAcroForm();
if (acro_form != null)
{
// This PDF document contains forms...
if (acro_form.FindObj("XFA") != null) {
// This PDF document contains XFA forms...
acro_form.Erase("XFA");
}
}