itext setField duplicates the field value

I am having a strange problem with iText and acrofields. I created a PDF and added the acrofields. Now when I do form.setField ('a field name', "a value") and I display or print the PDF, the value gets duplicated (once in smaller font and once in the intended font for that document). I checked the structure of the document and it doesn't look that my Acrofield are duplicated. What could be the cause of this

Thanks in advance

Pascal

Please find link here: https://drive.google.com/file/d/0B8O5n5QFSSNrSGVlNllOcEJHRzQ/edit?usp=sharing

I am on Ubuntu. Maybe that's why! I am using evince to look at the file, however I get the same result when I print it. I included a screenshot of what I see. https://drive.google.com/file/d/0B8O5n5QFSSNrWXJyY2VpSkt5NE0/edit?usp=sharing When I say duplicated, I should say shadowed. The value of the field is first displayed without font styling then overwritten with the required font.

The code I showed is pretty straightforward. The 2 arrrays are the name of the fields and their associated values. If the value is xxxx I set the field value to its index in that array. As you can see on the screenshot it gets shadowed too. My printout looks exactly like the screenshot. I haven't tried it yet on another platform.

Here is the code written in groovy

    File mergeForm (String path, Map fields, Map values, String newFile) {
    println "Merge Form: $path"
    def file = grailsApplication.mainContext.getResource(path)?.inputStream

    if (file == null)
       return null

    def reader = new PdfReader(file)
    def stamper = new PdfStamper(reader, new FileOutputStream(newFile))
    def form = stamper.getAcroFields()

    fields.eachWithIndex { k, v, i ->

        def val = ""
        if (v instanceof Closure) {
            val = v(values)
        }
        else if (v == '_xxxx_') {
           val = "${i + 1}"
        }
        else if (values[v]) {
           val = values."$v"
        }

        println "setting value[$i]: ${val} to: $k"
        form.setField (k, val)
    }

    stamper.close()
    return new File (newFile)
}

Answers


Summing it up

The issue seems to be due to multiple field annotations in the PDF at hand for the each field which differ somewhat, though, and therefore have different appearances.

In detail

Looking at the document version BOE-267-L1-Rev-1.unlocked-with-fields.pdf we will inspect the topmost field on the first page, "This Claim is Filed for Fiscal Year 20". We see that the page object 9 in its annotations array (in object 265) has (among many others) object 304 and object 180 which both are annotations of that field!

304 0 obj
<<
  /Ff 12582912
  /MaxLen 2
  /F 4
  /Type/Annot
  /Subtype/Widget
  /T(This Claim is Filed for Fiscal Year 20)
  /P 9 0 R
  /Q 1
  /MK<<>>
  /FT/Tx
  /Rect[166.765 693.57 188.965 701.479]
  /DA(/Arial 8 Tf 0 g)
  /AA<</F 333 0 R/K 334 0 R>>
>>
endobj 
...
180 0 obj
<<
  /Ff 0
  /F 4
  /Type/Annot
  /Subtype/Widget
  /DR<</Font<</Helv 2 0 R>>>>
  /T(This Claim is Filed for Fiscal Year 20)
  /V()
  /AP<</N 179 0 R>>
  /P 9 0 R
  /BS<</W 0.5/S/S>>
  /FT/Tx
  /Rect[165.4 706.28 187.6 714.19]
  /DA(/Helv 0 Tf 0 g )
>>
endobj 

The definitions of these describe slightly different positions on the page:

  /Rect[166.765 693.57 188.965 701.479]
...
  /Rect[165.4 706.28 187.6 714.19]

and different default appearance strings

  /DA(/Arial 8 Tf 0 g)
...
  /DA(/Helv 0 Tf 0 g )

Thus, it is not a surprise that you get multiple, non-identical appearances of this field. The actual surprise is that the version filled by iText on Adobe Reader does not display double values.

@Bruno someone might want to look into this as soon as there is some time.

The other fields have duplicate appearances, too; most often the page positions are nearly identical, though, but the default appearance streams still differ which results in multiple, non-identical appearances for them, too.


Need Your Help

Installing PostgreSQL on OSX for Rails development

ruby-on-rails macos postgresql installation

I've spent several hours over the past few days trying to get PostgreSQL to play nice with RoR on my Mac.