Get annotations in javascript

Skip to first unread message

Samuele Panarotto

Jan 19, 2015, 10:48:07 AM1/19/15
Good morning, 

I can't understand why this part of code doesn't works

var amgr = documentViewer.GetAnnotationManager();
var annotations = amgr.GetAnnotationsList();
for (var i = 0; i < annotations.length; i++) {
var annotation = annotations[i];

In terminal I see the first log like this image:

But why I cannot see the log inside the for loop? Why annotations array length is 0?

Can you please help me?

Thank you

Matt Parizeau

Jan 20, 2015, 1:25:55 PM1/20/15
Hi Samuele,

It looks like this is because the log in Chrome doesn't take a snapshot of the array at the time of logging. See this link: The key from there is: As of today, dumping an object (array) into console will result in objects' properties being read upon console object expansion (i.e. lazily). This means that dumping the same object while mutating it will be hard to debug using the console.

So at the time of the logging there are no annotations in the list, however at the time you expanded the array in the console there were two annotations in the list. You can verify this by adding console.log(annotations.length) immediately after your console.log(annotations) and you'll see that the length is zero at that time.

If you want to do something with the annotations right after they're loaded you can find the call to ImportAnnotationsAsync inside ReaderControl.js and add a callback as the second parameter. This callback will be called when the initial annotations have been loaded. e.g.
am.ImportAnnotationsAsync(data, function(annots) {
.log("loaded annotations");

Matt Parizeau
Software Developer
PDFTron Systems Inc.
Reply all
Reply to author
0 new messages