Updating Field Data from the internal XFDF

60 views
Skip to first unread message

Matt Parizeau

unread,
May 29, 2015, 4:28:36 PM5/29/15
to pdfnet-w...@googlegroups.com
Q:

I have some user data on the server that I want to use to pre-populate a few form fields in a document when they open it. What would be the best way to do this?

A:

One way to do this would be to use the setInternalAnnotationsTransform function to update the values directly from the internal XFDF file. On the outer page where you create the PDFTron.WebViewer object you would set your data on the custom property of the WebViewer constructor. For example:
custom: JSON.stringify({
    name
: 'John',
    location
: 'Vancouver'
}),

Then you would have a config file where it would update the original XFDF data. For example:
$(document).on('viewerLoaded', function() {

    readerControl
.docViewer.setInternalAnnotationsTransform(function(originalData, callback) {
       
var customData = JSON.parse(window.ControlUtils.getCustomData());

       
function getWidget(id) {
           
return elements.querySelector('widget[title="' + id + '"]');
       
}

       
var parser = new DOMParser();
       
var elements = parser.parseFromString(originalData, 'text/xml');
       
var ele = getWidget('mywidgetID1');
        ele
.setAttribute('V', customData.name);
        ele
= getWidget('myWidgetID2');
        ele
.setAttribute('V', customData.location);

        callback
(elements);
   
});
});

Reply all
Reply to author
Forward
0 new messages