Change AcroFields order in existing PDF with iText? -
i have pdf text form fields @ layered 1 on top of other. when fill fields via itext , flatten form, form field had created on top of other form field on bottom.
for instance, have text field named "number_field" , underneath second text field titled "name_field". when set value fields via itext (so 10 number_field , 'john' name_field), number_field on top of name_field.
how change order on page of these fields itext? possible?
link example pdf: https://freecompany.sharefile.com/d-s84f6d63e7d04fe79
i have made following ticket in issue tracker @ itext group:
a problem caused fact itext reads field items
hashmap
, hence there no way predict in order flattened. isn't problem. don't think problem occurs in case don't flatten pdf, because in case, appearance stored in widget annotations , it's pdf viewer decide field covers 1 in case of overlapping fields.however, if form fields overlap, can't predict field cover when flattening.
suppose we'd use
treemap
instead ofhashmap
, solve problem? not really, becausecomparator
use? tab-order defined, not always. if it's not defined, should order fields in order in appear in/fields
array? or make more sense order them based on order of widget annotations in/annots
array? option order them based on position on page. in short: not decision itext should make.however, if solve problem, create
comparator
member variablepdfstamperimp
. if suchcomparator
provided (we provide implementations), flattening process executed in order definedcomparator
.
this ticket has received low priority (i assume you're not customer of 1 of itext software companies), while writing ticket, had idea.
i referred underline portion of text using itextsharp in comments. in case, you'd field positions (using getfieldpositions()
method) , draw contents in right order using columntext
. approach has several disadvantages: in order font, font size, font color correct, you'd have examine fields. requires programming.
i posting answer, because have better alternative: fill out form in 2 passes! shown in fillformfieldorder example. fill out form src
resulting in flattened form dest
this:
public void manipulatepdf(string src, string dest) throws documentexception, ioexception { go2(go1(src), dest); }
as can see, execute go1()
method first:
public byte[] go1(string src) throws ioexception, documentexception { pdfreader reader = new pdfreader(src); bytearrayoutputstream baos = new bytearrayoutputstream(); pdfstamper stamper = new pdfstamper(reader, baos); acrofields form = stamper.getacrofields(); form.setfield("sunday_1", "1"); form.setfield("sunday_2", "2"); form.setfield("sunday_3", "3"); form.setfield("sunday_4", "4"); form.setfield("sunday_5", "5"); form.setfield("sunday_6", "6"); stamper.setformflattening(true); stamper.partialformflattening("sunday_1"); stamper.partialformflattening("sunday_2"); stamper.partialformflattening("sunday_3"); stamper.partialformflattening("sunday_4"); stamper.partialformflattening("sunday_5"); stamper.partialformflattening("sunday_6"); stamper.close(); reader.close(); return baos.tobytearray(); }
this fills out sunday_x
fields , uses partial form flattening flatten only fields. go1()
method takes src
parameter , returns byte[]
partially flattened form.
the byte[]
used parameter go2()
method, takes dest
second parameter. going fill out sunday_x_notes
fields:
public void go2(byte[] src, string dest) throws ioexception, documentexception { pdfreader reader = new pdfreader(src); pdfstamper stamper = new pdfstamper(reader, new fileoutputstream(dest)); acrofields form = stamper.getacrofields(); form.setfield("sunday_1_notes", "it's sunday today, let's go sea"); form.setfield("sunday_2_notes", "it's sunday today, let's go park"); form.setfield("sunday_3_notes", "it's sunday today, let's go beach"); form.setfield("sunday_4_notes", "it's sunday today, let's go woods"); form.setfield("sunday_5_notes", "it's sunday today, let's go lake"); form.setfield("sunday_6_notes", "it's sunday today, let's go river"); stamper.setformflattening(true); stamper.close(); reader.close(); }
as can see, flatten fields. result looks this:
now, no longer have worry order of fields, not in /fields
array, not in /annots
array. fields filled out in exact order want to. notes cover dates now, instead of other way round.
Comments
Post a Comment