Calling RunJavaScript from JavaScript in a web page

PDF-XChange Viewer SDK for Developer's
(ActiveX and Simple DLL Versions)

Moderators: PDF-XChange Support, Daniel - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Vasyl - PDF-XChange, Ivan - Tracker Software, Stefan - PDF-XChange

jimcooper0
User
Posts: 5
Joined: Tue Jun 07, 2011 4:27 pm

Calling RunJavaScript from JavaScript in a web page

Post by jimcooper0 »

I'm sure this is a novice question but I'm trying to call RunJavaScript to get a collection of annotation objects for the current PDF document but this is being called from JavaScript in a web page:

var ret = "";
document.all.PDFView.RunJavaScript("this.getAnnots();", ret, 0, 0);

ret is always blank even when I have documents that have annotations.

How do I get results back to JavaScript variables in a web page or ASP.NET aspx page?

Thanks
User avatar
Stefan - PDF-XChange
Site Admin
Posts: 19868
Joined: Mon Jan 12, 2009 8:07 am

Re: Calling RunJavaScript from JavaScript in a web page

Post by Stefan - PDF-XChange »

Hello Jim Cooper,

The second parameter in the RunJavaScript function should be a pointer to the variable you want to store your result in.

Best,
Stefan
jimcooper0
User
Posts: 5
Joined: Tue Jun 07, 2011 4:27 pm

Re: Calling RunJavaScript from JavaScript in a web page

Post by jimcooper0 »

Thanks for getting back to me Stefan.

The trick for me is knowing how to do this in JavaScript. Do I need to create the return variable as an object or is there a way to do this with a standard string variable? If I could get a simple example where this has been applied, I should be able to work out the details myself.
User avatar
Vasyl - PDF-XChange
Site Admin
Posts: 2445
Joined: Thu Jun 30, 2005 4:11 pm

Re: Calling RunJavaScript from JavaScript in a web page

Post by Vasyl - PDF-XChange »

Hi, jimcooper0.

You cannot obtain an annotations list by your ret variable. But you can enumerate all annotations and get any property of each annotation.

The example code for this:

Code: Select all

var script = 
"var res = """";
var annots = this.getAnnots();
for (var i = 0; i < annots.length; i++)
{
     res += ""Type: ""; res += annots[i].type; res += ""\n"";
     res += ""-----\n"";
     res += ""Text: ""; res += annots[i].contents; res += ""\n"";
};
res;"

var result = "";
document.all.PDFView.RunJavaScript(script, result, 0, 0);
// result will contain for example:
Type: Circle
Text: sample text for oval
-----
Type: Square
Text: sample text for rectangle

NOTE: the text of pdf/java script contains some quotemarks, but I don't know exactly how to state the string value in ASP.NET. I have replaced the " by ""...

Best
Regards.
PDF-XChange Co. LTD (Project Developer)

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
User avatar
Vasyl - PDF-XChange
Site Admin
Posts: 2445
Joined: Thu Jun 30, 2005 4:11 pm

Re: Calling RunJavaScript from JavaScript in a web page

Post by Vasyl - PDF-XChange »

Exists another way to get result of Acro-JavaScript execution that is usable for web-developers (by JavaScript on HTML for example).

Code: Select all

PDFView.Property("JavaScript.UseExecResult", 0) = 1; // call it once
...
PDFView.RunJavaScript(script, 0, 0, 0);
// to get result of 'script' execution:
var res = PDFView.Property("JavaScript.ExecResult", 0);
HTH
PDF-XChange Co. LTD (Project Developer)

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.