Detecting onclick event on annotation from JavaScript

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

Detecting onclick event on annotation from JavaScript

Post by jimcooper0 »

I've been able to detect the onclick event from JavaScript within the PDF-XChange ActiveX control with the following code however I'm also trying to determine which annotation the user has clicked on. Is there any way to do this? The code below doesn't work for me as there seems to be no way to get anything back in the dataO variable.

-Thanks

function PDFXCviewEvent(Type, Name, DataIn, DataOut, Flags)
{
if (Type == 4 && Name == "Notifications.Selection")
{
var dataO = "";
pdfViewer.GetProperty("Notifications.Selection.Document", dataO, 0);
pdfViewer.DoVerb("", "GetSelectionState", "", dataO, 0);
User avatar
Vasyl - PDF-XChange
Site Admin
Posts: 2448
Joined: Thu Jun 30, 2005 4:11 pm

Re: Detecting onclick event on annotation from JavaScript

Post by Vasyl - PDF-XChange »

Hi, jimcooper0

You may do as follows ; (pseudocode):

Code: Select all

youreventhandler OnEvent(Type, Name, ...)
{
	if ((Name == "Notifications.Mouse") && (Type == PXCVA_OnNamedNotify))
	{
		int msg;
		pdfViewer.GetProperty("Notifications.Mouse.Msg", out msg, 0);
		if (msg == 0x0201) // WM_LBUTTONDOWN
		{
			int docID = -1;
			pdfViewer.GetProperty("Notifications.Mouse.Document", out docID, 0);
			if (docID >= 0)
			{
				LONG page = -1;
				pdfViewer.GetProperty("Notifications.Mouse.Page", out page, 0);
				if (page >= 0)
				{
					POINT pt;
					GetCursorPos(out pt);

					int ptScreen[2]; // should be SAFAARRAY-type
					ptScreen[0] = pt.x;
					ptScreen[1] = pt.y;

					string str;
					str = "Pages[" + page + "]";

					double ptPage[2]; // should be SAFAARRAY-type
					HRESULT hr = pdfViewer.DoDocumentVerb(docID, str, "TranslateScreenPoint", ptScreen, out ptPage, PXCVA_Sync);

					int annotsCount = 0;
					pdfViewer.DoDocumentVerb(docID, str, "GetAnnotsCount", empty, out annotsCount, PXCVA_Sync);
					for (int i = 0; i < annotsCount; i++)
					{
						double annotRect[4]; // should be SAFAARRAY-type
						pdfViewer.DoDocumentVerb(docID, str, "GetAnnotRect", i, out annotRect, PXCVA_Sync);
						double left = annotRect[0];
						double top = annotRect[1];
						double right = annotRect[2];
						double bottom = annotRect[3];
						if (ptPage[0] >= left && ptPage[0] <= right && ptPage[1] <= top && ptPage[1] >= bottom)
						{
							// ANNOT Page[page].Annots[i] CLICKED!
							break;
						}
					}
				}
			}
		}
	}
}
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.
jimcooper0
User
Posts: 5
Joined: Tue Jun 07, 2011 4:27 pm

Re: Detecting onclick event on annotation from JavaScript

Post by jimcooper0 »

Thanks Vasyl for getting back to me. I can see how your recommended solution would work from within a C# language environment where I can pass arguments by reference however my problem is that I'm working with JavaScript from within a web page and I've never been able to generally get return values calling Tracker ActiveX methods:

This sort of code works:

pdfViewer.DoVerb("", "ExecuteCommand", "ToggleAllBars", 0, 0);

... but it's not returning any values.

As well, I can know when an event has happened such as:

function PDFXCviewEvent(Type, Name, DataIn, DataOut, Flags)
{
if (Type == 4 && Name == "Notifications.Selection")
{
// get something back doesn't work
var docID = -1;
pdfViewer.GetProperty("Notifications.Mouse.Document", docID, 0);
}
}

however calling the "GetProperty" as above doesn't work from JavaScript because docID must be passed by value.

I've searched this forum for a solution as well as the Adobe JavaScript API reference but still can't get this resolved.

I've also tried:

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

var result = "";
pdfViewer.RunJavaScript(script, result, 0, 0);

but again, I can't get a returned value.

Is there some technique that I'm missing to work with JavaScript and the Tracker ActiveX control such that I can get return values back from the state of the control and specifically annotations?

Thanks,
Jim
User avatar
Vasyl - PDF-XChange
Site Admin
Posts: 2448
Joined: Thu Jun 30, 2005 4:11 pm

Re: Detecting onclick event on annotation from JavaScript

Post by Vasyl - PDF-XChange »

Hi, Jim.
however calling the "GetProperty" as above doesn't work from JavaScript because docID must be passed by value.
...
var result = "";
pdfViewer.RunJavaScript(script, result, 0, 0);

but again, I can't get a returned value.
There is a workaround you can use:

Instead of 'GetProperty' use the:

Code: Select all

var docID = pdfViewer.Property["Notifications.Mouse.Document"];
for 'RunJavaScript', to get result, use:

Code: Select all

pdfViewer.Property["JavaScript.UseExecResult"] = 1; // set it once
...
pdfViewer.RunJavaScript(script, null, 0, 0);
var result = pdfViewer.Property["JavaScript.ExecResult"];
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.
jimcooper0
User
Posts: 5
Joined: Tue Jun 07, 2011 4:27 pm

Re: Detecting onclick event on annotation from JavaScript

Post by jimcooper0 »

Thanks for getting back to me so quickly.

I tried your recommendations but I'm getting a JavaScript run-time error: "Microsoft JScript runtime error: Wrong number of arguments or invalid property assignment"

I'm using version 2.5.204 of the ActiveX control and my browser is IE9

The actual code is:

if (Type == 4 && Name == "Notifications.Selection")
{
var result = -1;
result = pdfViewer.Property["Notifications.Mouse.Document"];
}

with the error happening at: result = pdfViewer.Property["Notifications.Mouse.Document"];

Thanks,
Jim
User avatar
Vasyl - PDF-XChange
Site Admin
Posts: 2448
Joined: Thu Jun 30, 2005 4:11 pm

Re: Detecting onclick event on annotation from JavaScript

Post by Vasyl - PDF-XChange »

Sorry for my mistake, I'm not a JS-dev.
The correct is:

Code: Select all

docID =  pdfViewer.Property("<PropName>", 0);
Look also to:
C:\Program Files\Tracker Software\PDF-XChange Viewer SDK\Examples\WebExamples\HTML_JavaScript\PDFXChangeViewer.html
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.