Offline annotation database

174 views
Skip to first unread message

Donald Gordon

unread,
Jun 2, 2016, 8:02:50 PM6/2/16
to PDFTron WebViewer
Hi

We're embedding the WebViewer into an offline application.  We'd like to use the push/pull annotation functionality available with the "serverUrl" parameter to PDFTron.WebViewer, without a server.

Is there a way to intercept these calls before they turn into XMLHttpRequests?  Then we can serve them via local JavaScript (maybe in the parent frame) that e.g. deals with a local IndexedDb.

Also, is there a way we can push "live" annotation changes into the WebViewer, and get a stream of updates out of the WebViewer? 

Regards

Donald

Anatoly Kudrevatukh

unread,
Jun 3, 2016, 4:34:37 PM6/3/16
to pdfnet-w...@googlegroups.com
For saving annotations you can call annotManager.exportAnnotations() which is the data normally sent to the serverUrl. For loading the initial annotations you can use docViewer.setInternalAnnotationsTransform(function(originalData, callback) { and pass in a function which just calls the callback with the saved data

This tutorial goes into more detail about setInternalAnnotationsTransform https://www.pdftron.com/webviewer/demo/tutorials/advanced-loading-annotations.html

Please let us know if that helps.

Ashish Deshpande

unread,
Jun 7, 2016, 12:27:43 PM6/7/16
to PDFTron WebViewer
Hii Anatoly 

I too have same requirement as Donald we are running an offline application and we want to save the annotations in a similar manner as the server url
but i could not understand your reply can you please explain in detail or is there a running code available 

Donald Gordon

unread,
Jun 8, 2016, 4:56:47 PM6/8/16
to PDFTron WebViewer
Does the setInternalAnnotationsTransform hook get called even when the XOD has no annotations in it at all?

For saving, it looks like annotManager.getAnnotCommand() is what I want.

If I
  1. add an annotation
  2. call annotManager.getAnnotCommand()
  3. call annotManager.getAnnotCommand() again
will the two getAnnotCommand() calls return the same response each time, or will the new annotation be returned by the first call but not the second?

Thanks

Donald

Matt Parizeau

unread,
Jun 9, 2016, 4:53:28 PM6/9/16
to PDFTron WebViewer
Hi Donald and Ashish,

Assuming you have a config file passed to WebViewer you could have code like the following:
var annotManager;

$
(document).on('viewerLoaded', function() {
 
var docViewer = readerControl.docViewer;
  docViewer
.setInternalAnnotationsTransform(function(originalData, callback) {
   
var savedXfdf = getAnnotationDataFromMyOfflineLocation();
    callback
(savedXfdf);
 
});
});

$
(document).on('documentLoaded', function() {
  annotManager
= readerControl.docViewer.getAnnotationManager();
});

$
('#mySaveButton').on('click', function() {
 
var xfdf = annotManager.exportAnnotations();
  saveAnnotationsToMyOfflineLocation
(xfdf);
});

For getAnnotCommand() you'll only get the new annotation on the first call, the second should be an empty command. It returns whatever annotation changes have happened since the last time you called the function.

Matt Parizeau
Software Developer
PDFTron Systems Inc.

Donald Gordon

unread,
Jun 13, 2016, 12:36:40 PM6/13/16
to PDFTron WebViewer
Thanks Matt.

Is it possible to get a callback whenever the annotations are changed, so we can sync them in real-time rather than waiting for the user to click a button?  Would it be OK to just call exportAnnotations() every few seconds or will that cause problems?

Also, is there a way we can pass new/modified annotations in after the document and initial annotations have been loaded?

Regards

Donald

Matt Parizeau

unread,
Jun 13, 2016, 2:34:50 PM6/13/16
to PDFTron WebViewer
Hi Donald,

Yep, you can use the annotationChanged event to be notified any time an annotation changes (add, modify, remove). You can also use the imported attribute on the event parameter to check if the change happened because annotations were being imported from an initial load or a call to importAnnotations or importAnnotCommand. You may want to try using the getAnnotCommand and importAnnotCommand for syncing changes as getAnnotCommand will return only the changes since the last time you called the function. For example:

annotManager.on('annotationChanged', function(e, annotations, action) {
 
if (e.imported) {
   
return;
 
}

 
var newCommand = annotManager.getAnnotCommand();
  sendCommandToServer
(newCommand);
});

Then on a receiving client:

onReceiveNewCommand(function(command) {
  annotManager
.importAnnotCommand(command);
});

You can call importAnnotCommand and importAnnotations any time after the initial annotations have been loaded and it will automatically update existing annotations in the viewer if they have the same id (name attribute in XFDF) as the ones in the XFDF.

Matt Parizeau
Software Developer
PDFTron Systems Inc.

Reply all
Reply to author
Forward
0 new messages