I have a pdf with text form fields at are layered one on top of the other. When I fill the fields via iText and flatten the form, the form field that I had created on top of the other form field is now on the bottom.
For instance, I have a text field named "number_field" and that is underneath a second text field that is titled "name_field". When I set the value for those fields via iText (so 10 for number_field and 'John' for name_field), the number_field is now on top of the name_field.
How do I change the order on the page of these fields with iText? Is it possible?
Link to example PDF: https://freecompany.sharefile.com/d-s84f6d63e7d04fe79
I have made the following ticket in the issue tracker at iText Group:
This ticket has received a very low priority (I assume that you're not a customer of one of the iText Software companies), but while writing this ticket, I had another idea.
I already referred to underline portion of text using iTextSharp in the comments. In this case, you'd get all the field positions (using the
getFieldPositions()method) and draw all the contents in the right order usingColumnText. This approach has several disadvantages: in order for the font, font size, font color to be correct, you'd have to examine the fields. That requires some programming.I am now posting this as an answer, because I have a much better alternative: fill out the form in two passes! This is shown in the FillFormFieldOrder example. We fill out the form
srcresulting in the flattened formdestlike this:As you can see, we execute the
go1()method first:This fills out all the
sunday_xfields and uses partial form flattening to flatten only those fields. Thego1()method takessrcas parameter and returns abyte[]will the partially flattened form.The
byte[]will be used as a parameter for thego2()method, that takesdestas its second parameter. Now we are going to fill out thesunday_x_notesfields:As you can see, we now flatten all the fields. The result looks like this:
Now, you no longer have to worry about the order of the fields, not in the
/Fieldsarray, not in the/Annotsarray. The fields are filled out in the exact order you want to. The notes cover the dates now, instead of the other way round.