Search code examples
pdfpdf-generationitextacrobatpdf-form

How do I dynamically give a value to an existing barcode created in acrobat


How do I dynamically give a value to an existing barcode created in acrobat.

I created the bar code in a pdf form by following this guide.

When I type something into the form fields , the bar code does get updated with the values. However the catch is , the pdf is dynamically generated through ruby code, and the form is prefilled. However the barcode doesn't include the data dynamicallly filled into the pdf form. If I make any change in the pdf form data , the bar code is reloaded and now includes the entire form data , but this doesn't happen when the generated pdf is opened for the first time.

To summarize.

1) Added barcode to a pdf form and added javascript to calcuate bar code value.

2) Bar code gets updated with newly calcuated value as form data is changed.

3) Dynamically filled the pdf form created in step 1. All form fields are filled , but bar code doesn't include the data.

4) If I make a change to data in pdf created in 3 , the bar code gets redrawn with all the data filled on the server and also includes the data change I just made.

The problem is in step 3 , when the pdf form is filled with data on the server , I am expecting the bar code to include the data.

P.S , I have used pdf-forms to fill out pdf on server. I have also tried itext java api to fill out the form. In addition, with both pdf-forms and itext , I have tried to provide an initial value for the bar code. However if I do this , the barcode doesn't even show in the generated pdf and instead the provided values show.

P.S 2nd. I have also been able to generate a bar code image using itext. However I need this bar code to be updated as the user changes form data in the generated pdf. Obviously the image doesn't update. and hence I tried the above approch of using a script in the pdf to calcuate barcode value on the fly.


Solution

  • One thing to understand first. In PDF, the value of a field and its appearance on the page are two different things. When created in Acrobat, the value of a Barcode Field is actually calculated based on your selections in the UI when you created it. Acrobat injects some JavaScript into the field so that when new data is entered by the user, the value of the field is recalculated and Acrobat can generate a new appearance for the barcode field (an image). If you only update value of the field on the server but don't set the "needs Appearances" property to true on the document, Acrobat won't know to regenerate the barcode appearance. You could, however, generate the barcode appearance and set that via iText but you'd need to know the JavaScript calculation in order to do so. Once the value and the appearance are set up and the needs appearances property is set, the form should work normally.

    Since you have tried multiple libraries, I'm going to suggest one more. The Datalogics PDF Java Toolkit can actually do all of this for you without needing to know what field data the barcode represents. You just populate the non-barcode fields with data, tell the Toolkit to calculate the form and generate appearances and it just works. The barcode gets rendered exactly the same as it would have been if filled out in Acrobat.