Get annotations in javascript

88 views
Skip to first unread message

Samuele Panarotto

unread,
Jan 19, 2015, 10:48:07 AM1/19/15
to pdfnet-w...@googlegroups.com
Good morning, 

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

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


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

unread,
Jan 20, 2015, 1:25:55 PM1/20/15
to pdfnet-w...@googlegroups.com
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: http://stackoverflow.com/a/24176305. 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) {
    console
.log("loaded annotations");
});

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