JAG
JAG

Reputation: 1781

pdfjs: Fill out a form and get the fieldValues

I'm using pdfjs to display a PDF form (acroforms example). If I fill in some fields, how do I get those annotations.

I've tried:

pdfPage.getAnnotations().then(function(a) {
for(var i = 0;i < a.length; i++) {
  console.log(a[i].id + ' - ' + a[i].fieldValue);
  } 
});

But all I get are empty fieldValues. I guess I'm getting them from the original PDF. How do I access the live document which includes the new annotations?

Upvotes: 1

Views: 4107

Answers (2)

Charly Guirao
Charly Guirao

Reputation: 489

    async getAnnotations() {
    var result = {};                
    var pages = PDFViewerApplication.pdfDocument.numPages;
    for (var i = 1; i <= pages; i++) {
        var page = await PDFViewerApplication.pdfDocument.getPage(i);
        var annotations = await page.getAnnotations();
        for (var j = 0; j < annotations.length; j++) {
            var element = annotations[j];
            // Field Name
            if (result[element.fieldName] == null) {
                result[element.fieldName] = null;
            }
        
                var val = PDFViewerApplication.pdfDocument.annotationStorage.getOrCreateValue(element.id);
                if (element.radioButton) {
                    if (val.value) {
                        result[element.fieldName] = element.buttonValue;
                    }
                } else if (element.checkBox) {
                    result[element.fieldName] = val.value ? "On" : "Off";
                } else {
                    result[element.fieldName] = val.value;
                }                                
        }             
    };              

    return result;
}

Upvotes: 3

atrebbi
atrebbi

Reputation: 553

You should save values written by user in a local variable

Then you should create a xfdf file ( xml file by Adobe ) , look for Example nere : https://developer.salesforce.com/page/Adobe_XFDF . The xfdf should then be merged with original pdf to produce a new compiled pdf

Upvotes: 1

Related Questions