Importing XFDF data

I am having trouble getting xfdf data imported when loading a PDF onto the screen. Sometimes when a user submits data the validation of input fails so we reload the screen with the error messages and allow the user to correct their submission. We are trying to reload the PDF with all the data previously submitted but the fields are always empty and the user must re-fill all the fields.

We are exporting the XFDF using:
annotManager.exportAnnotations({ links: false, widgets: false })

The import is done using importAnnotations:
instance.docViewer.on(‘documentLoaded’, () => {
instance.annotManager.importAnnotations(xfdfData);
})

Sample XFDF data we are testing with:

<?xml version="1.0" encoding="UTF-8" ?>test1test3

Sample document can be found here:

https://esoftwaresol-my.sharepoint.com/:b:/g/personal/jwithers_vergentlms_com/EbgSDZXrolBIhK8XJpz3QIoB0V0dVhqpBPAlh97uR7odmA?e=YpC4Hw

I feel like I am probably missing something that is quite simple.

Hey there!

When calling exportAnnotations, try passing fields: true in the options object.

annotManager.exportAnnotations({ links: false, widgets: false, fields: true })

Hope this helps!
Logan

Hey Logan,

I tried this and the XFDF exported string looks the same as it does without passing fields: true. I didn’t realize the sample XFDF string I pasted didn’t come across in the first post, this is the sample XFDF data:

<?xml version="1.0" encoding="UTF-8" ?><xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve"><fields><field name="FLD_SIG_IMAGE[1000]"><value>test1</value></field><field name="FLD_SIG_IMAGE[1001]"><value></value></field><field name="txtField"><value>test3</value></field></fields><annots /><pages><defmtx matrix="1,0,0,-1,0,792" /></pages></xfdf>

Hey!

You also need to export widgets (form fields) with you XFDF, so you need to remove widgets: false from your code. I should have caught that in my first response, my bad.

Thanks,
Logan

Hey Logan,

After setting widgets: true on the export, it does repopulate some of the fields on the import; however, on the second submission we get duplicated widgets in the new XFDF data, if we submit a third time, then we get triplicated widgets and so on.

It also appears that (probably due to the brackets in the field name?) that fields named such as A_FIELD_NAME[1234] do not get repopulated, even though the data is in the XFDF data string on export/import.

For example using the sample document from earlier, this is the xfdf data on first submission:

<?xml version="1.0" encoding="UTF-8" ?><xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve"><pdf-info xmlns="http://www.pdftron.com/pdfinfo" version="2" import-version="3"><ffield type="Tx" name="FLD_SIG_IMAGE[1000]"><font name="Arial" size="12" /></ffield><ffield type="Tx" name="FLD_SIG_IMAGE[1001]"><font name="Arial" size="12" /></ffield><ffield type="Tx" name="txtField"><font name="Arial" size="12" /></ffield><ffield type="Sig" name="SIG_IMAGE_TEST"><font name="Arial" /></ffield><widget field="FLD_SIG_IMAGE[1000]" modified-date="D:20200629142739-05'00'" page="1"><rect x1="106.388" x2="256.388" y1="708.58" y2="730.58" /><border width="0" style="null"><color a="0" /></border></widget><widget field="FLD_SIG_IMAGE[1001]" modified-date="D:20200629142739-05'00'" page="1"><rect x1="106.936" x2="256.936" y1="657.031" y2="679.031" /><border width="0" style="null"><color a="0" /></border></widget><widget field="txtField" modified-date="D:20200629142739-05'00'" page="1"><rect x1="106.388" x2="256.388" y1="617.547" y2="639.547" /><border width="0" style="null"><color a="0" /></border></widget><widget appearance="_DEFAULT" field="SIG_IMAGE_TEST" modified-date="D:20200629142739-05'00'" page="1"><rect x1="108.033" x2="258.033" y1="543.386" y2="575.386" /><border width="0" style="null"><color a="0" /></border><appearances><aappearance name="_DEFAULT"><Normal>data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyEAAABFCAYAAABdY+kiAAAACXBIWXMAAB7CAAAewgFu0HU+AAAA7UlEQVR4nO3BgQAAAADDoPlTX+EAVQEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABwGWAGAAGtvyc4AAAAAElFTkSuQmCC</Normal></aappearance></appearances></widget></pdf-info><fields><field name="FLD_SIG_IMAGE[1000]"><value>testf1</value></field><field name="FLD_SIG_IMAGE[1001]"><value></value></field><field name="txtField"><value>testf3</value></field><field name="SIG_IMAGE_TEST"><value></value></field></fields><annots /><pages><defmtx matrix="1,0,0,-1,0,792" /></pages></xfdf>

When we import this string back into the viewer, it does populate field “txtField” but does not populate FLD_SIG_IMAGE[1000]. When we submit the data again, this is the exported data string, notice the duplicated widget entries:

<?xml version="1.0" encoding="UTF-8" ?><xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve"><pdf-info xmlns="http://www.pdftron.com/pdfinfo" version="2" import-version="3"><ffield type="Tx" name="FLD_SIG_IMAGE[1000]"><font name="Arial" size="12" /></ffield><ffield type="Tx" name="FLD_SIG_IMAGE[1001]"><font name="Arial" size="12" /></ffield><ffield type="Tx" name="txtField"><font name="Arial" size="12" /></ffield><ffield type="Sig" name="SIG_IMAGE_TEST"><font name="Arial" /></ffield><widget field="FLD_SIG_IMAGE[1000]" modified-date="D:20200629142739-05'00'" page="1"><rect x1="106.388" x2="256.388" y1="708.58" y2="730.58" /><border width="0" style="null"><color a="0" /></border></widget><widget field="FLD_SIG_IMAGE[1001]" modified-date="D:20200629142739-05'00'" page="1"><rect x1="106.936" x2="256.936" y1="657.031" y2="679.031" /><border width="0" style="null"><color a="0" /></border></widget><widget field="txtField" modified-date="D:20200629142739-05'00'" page="1"><rect x1="106.388" x2="256.388" y1="617.547" y2="639.547" /><border width="0" style="null"><color a="0" /></border></widget><widget appearance="_DEFAULT" field="SIG_IMAGE_TEST" modified-date="D:20200629142739-05'00'" page="1"><rect x1="108.033" x2="258.033" y1="543.386" y2="575.386" /><border width="0" style="null"><color a="0" /></border><appearances><aappearance name="_DEFAULT"><Normal>data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyEAAABFCAYAAABdY+kiAAAACXBIWXMAAB7CAAAewgFu0HU+AAAA7UlEQVR4nO3BgQAAAADDoPlTX+EAVQEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABwGWAGAAGtvyc4AAAAAElFTkSuQmCC</Normal></aappearance></appearances></widget><widget index="1" field="FLD_SIG_IMAGE[1000]" modified-date="D:20200629142751-05'00'" page="1"><rect x1="106.388" x2="256.388" y1="708.58" y2="730.58" /><border width="0" style="null"><color a="0" /></border></widget><widget index="1" field="FLD_SIG_IMAGE[1001]" modified-date="D:20200629142751-05'00'" page="1"><rect x1="106.936" x2="256.936" y1="657.031" y2="679.031" /><border width="0" style="null"><color a="0" /></border></widget><widget index="1" field="txtField" modified-date="D:20200629142751-05'00'" page="1"><rect x1="106.388" x2="256.388" y1="617.547" y2="639.547" /><border width="0" style="null"><color a="0" /></border></widget><widget appearance="_DEFAULT" index="1" field="SIG_IMAGE_TEST" modified-date="D:20200629142751-05'00'" page="1"><rect x1="108.033" x2="258.033" y1="543.386" y2="575.386" /><border width="0" style="null"><color a="0" /></border><appearances><aappearance name="_DEFAULT"><Normal>data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAyEAAABFCAYAAABdY+kiAAAACXBIWXMAAB7CAAAewgFu0HU+AAAA7UlEQVR4nO3BgQAAAADDoPlTX+EAVQEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABwGWAGAAGtvyc4AAAAAElFTkSuQmCC</Normal></aappearance></appearances></widget></pdf-info><fields><field name="FLD_SIG_IMAGE[1000]"><value>testf1 - new value</value></field><field name="FLD_SIG_IMAGE[1001]"><value></value></field><field name="txtField"><value>a new value in f3</value></field><field name="SIG_IMAGE_TEST"><value>_DEFAULT</value></field></fields><annots /><pages><defmtx matrix="1,0,0,-1,0,792" /></pages></xfdf>

The duplicated entries wouldn’t be so bad except on import for the third submission it seems it is stacking the widgets on top of each other so when you type in the field again it appears you are typing over the previously entered values:

Hey!

Okay I dug into this a bit more. You actually DO need to have {widgets: false} in your export call. We dont need to export them because they are already part of the document.

const xfdf = await instance.annotManager.exportAnnotations({ widgets: false, links: false });

When you import XFDF we need to make sure the annotations in the document are already loaded. We can do that by listening for the annotationsLoaded event like so:

instance.docViewer.on('annotationsLoaded', () => {
    instance.annotManager.importAnnotations(xfdfData)
})

Can you try that and let me know if that works for you?

Thanks,
Logan

Hey!

Binding the importAnnotations action to the annotationsLoaded event works great!

Thanks for all your help on this! Great product!

1 Like