java - xmlworker create annotations in form fields for further searches -


i create pdf , need post-processing in workflow update content, best way find create pdfformfield annotation.

after create fields annotations can search positions throught acrofield object , obtain page , positions.

public static list<fieldposition> getpositions(pdfreader pdfreader, string name) {         acrofields acrofields = pdfreader.getacrofields();         list<fieldposition> fieldpositions = acrofields.getfieldpositions(name);         return fieldpositions;     } 

and can draw text in positions this:

public static void writestring(pdfreader pdfreader, pdfstamper pdfstamper, string name, string valuestr, font fonttype, float leftdiff,             float bottomdiff) throws ioexception, documentexception {         phrase pagephrase = new phrase(valuestr, fonttype);          float left;         float bottom;         list<fieldposition> fieldpositions = getpositions(pdfreader, name);         if (fieldpositions != null) {             (fieldposition fieldpos : fieldpositions) {                 left = fieldpos.position.getleft() + leftdiff;                 bottom = fieldpos.position.getbottom() + bottomdiff;                 pdfcontentbyte cover = pdfstamper.getovercontent(fieldpos.page);                 columntext.showtextaligned(cover, element.align_left, pagephrase, left, bottom, 0);             }         }     } 

you can check sample create formfields here http://itextpdf.com/examples/iia.php?id=157 sample above using simple itext api table , cells (you can see cell used obtain rectangle field)

i try crea same fields in 2 ways. create processor cell (td) or field (input or whatever)

tagprocessorfactory tagprocessorfactory = tags.gethtmltagprocessorfactory(); tagprocessorfactory.addprocessor(new xhtmltabletagprocessor(), new string[] { "table" }); tagprocessorfactory.addprocessor(new xhtmlcellmarktagprocessor(writer), new string[] { "td" }); tagprocessorfactory.addprocessor(new xhtmlinputmarktagprocessor(writer), new string[] { "input" }); htmlpipelinecontext htmlcontext = new htmlpipelinecontext(null); htmlcontext.settagfactory(tagprocessorfactory); 

the cell processor

public class xhtmlcellmarktagprocessor extends abstracttagprocessor {      private pdfwriter writer;      public xhtmlcellmarktagprocessor(pdfwriter writer) {         this.writer = writer;     }      @override     public list<element> end(workercontext ctx, tag tag, list<element> currentcontent) {         string mark = "mark";         string strmark = tag.getattributes().get(mark);         string strname = tag.getattributes().get("name");          boolean ismark = stringutils.isalpha(strmark) && strmark.equalsignorecase("true");          system.out.println(string.format(currentcontent.size() + "<<< %s %s ", strname, strmark));          list<element> retval = super.end(ctx, tag, currentcontent);         if (ismark && stringutils.isnotblank(strname)) {             (element element : retval) {                 if (element instanceof pdfpcell) {                     final textfield namefield = new textfield(writer, new rectangle(0, 0, 1, 5), strname);                     try {                          final pdfformfield formfield = namefield.gettextfield();                         final int width = 1;                          pdfpcellevent cellevent = new pdfpcellevent() {                             @override                             public void celllayout(pdfpcell cell, rectangle position, pdfcontentbyte[] canvases) {                                 try {                                     formfield.setwidget(new rectangle(position.getleft(), position.getbottom(), position.getleft() + width,                                             position.gettop()), pdfannotation.highlight_none);                                     writer.addannotation(formfield);                                 } catch (exception e) {                                     throw new runtimeexception(e);                                 }                             }                         };                          ((pdfpcell) element).setcellevent(cellevent);                      } catch (exception e) {                         throw new runtimeexception(e);                     }                 }             }         }          return retval;     } } 

the elements 0 if parent invoked or not, no exist default processor cells tables or divs error: java.lang.illegalargumentexception: number of columns in pdfptable constructor must greater zero.

the input processor

public class xhtmlinputmarktagprocessor extends abstracttagprocessor {      private pdfwriter writer;      public xhtmlinputmarktagprocessor(pdfwriter writer) {         this.writer = writer;     }      @override     public list<element> start(final workercontext ctx, final tag tag) {         list<element> elements = super.start(ctx, tag);         try {             string mark = "mark";             string strmark = tag.getattributes().get(mark);             string strtype = tag.getattributes().get("type");             string strname = tag.getattributes().get("name");             string strsize = tag.getattributes().get("size");             string strvalue = tag.getattributes().get("value");              boolean ismark = stringutils.isalpha(strmark) && strmark.equalsignorecase("true");             int size = stringutils.isnumeric(strsize) ? new integer(strsize.trim()) : 0;              system.out.println(string.format("<<< %s %s %s %s %s", strname, strtype, strmark, strsize, size));              if (ismark && stringutils.isnotblank(strtype) && strtype.equals("text")) {                 textfield namefield = new textfield(writer, new rectangle(100, 100, 100, 50), strname);                 namefield.setbackgroundcolor(basecolor.green);                 pdfformfield formfield = namefield.gettextfield();                 formfield.setvalueasstring(strvalue);                 formfield.setwidget(new rectangle(100, 100, 100, 100), pdfannotation.highlight_push);                 writer.addannotation(formfield);                 //system.out.println("adddd");             }         } catch (exception e) {             throw new runtimeexception(e);         }         return elements;     } } 

i dont see nothing in result pdf. textfield object can not added elements list because no extends of element interface

the solution use td tagprocessor extending tabledata processor this:

public class xhtmlcellmarktagprocessor extends tabledata {      private pdfwriter writer;      public xhtmlcellmarktagprocessor(pdfwriter writer) {         this.writer = writer;     }      @override     public list<element> end(workercontext ctx, tag tag, list<element> currentcontent) {         string mark = "mark";         string strmark = tag.getattributes().get(mark);         string strname = tag.getattributes().get("name");         boolean ismark = stringutils.isalpha(strmark) && strmark.equalsignorecase("true");          list<element> retval = super.end(ctx, tag, currentcontent);          if (ismark && stringutils.isnotblank(strname)) {             (element element : retval) {                 if (element instanceof pdfpcell) {                     final textfield namedfield = new textfield(writer, new rectangle(0, 0, 10, 5), strname);                     try {                          final pdfformfield formfield = namedfield.gettextfield();                         final int width = 0;                          pdfpcellevent cellevent = new pdfpcellevent() {                             @override                             public void celllayout(pdfpcell cell, rectangle position, pdfcontentbyte[] canvas) {                                 try {                                     formfield.setwidget(new rectangle(position.getleft(), position.getbottom(), position.getleft() + width,                                             position.gettop()), pdfannotation.highlight_none);                                     writer.addannotation(formfield);                                  } catch (exception e) {                                     throw new runtimeexception(e);                                 }                             }                         };                          ((pdfpcell) element).setcellevent(cellevent);                      } catch (exception e) {                         throw new runtimeexception(e);                     }                 }             }         }          return retval;     } } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -