Java PDFBox setting custom font for a few fields in PDF Form -


i using apache pdfbox read fillable pdf form , fill fields based on data. using below code (as per suggestions other answers) default appearance string , changing (as can see below, changing font size 10 12 if field name "field1".

  1. how bold field? documentation on order /helv 10 tf 0 g arranged? need set bold field?
  2. if understand right, there 14 basic fonts can use in pdfbox out of box (pun unintended). use 1 or more fonts signatures (cursive). out of box fonts that? if not, if have own font, how set in method written pdf?

please note, below code works fine filling specific 'value' passed in method parameter in specific 'name' field of method parameter.

thank !

public static void setfield(string name, string value ) throws     ioexception {     pddocumentcatalog doccatalog = _pdfdocument.getdocumentcatalog();     pdacroform acroform = doccatalog.getacroform();     pdfield field = acroform.getfield( name );      cosdictionary dict = ((pdfield)field).getdictionary();     cosstring defaultappearance = (cosstring) dict.getdictionaryobject(cosname.da);     if (defaultappearance != null)     {         dict.setstring(cosname.da, "/helv 10 tf 0 g");         if(name.equalsignorecase("field1"))         {             dict.setstring(cosname.da, "/helv 12 tf 0 g");         }     }     if(field instanceof pdtextbox)     {         field= new pdtextbox(acroform, dict);         ((pdfield)field).setvalue(value);     } 

as per mkl's answer, use 2 fonts in same pdf, used following method: not default font , custom font working together, added 2 fonts resources , used them.

public list<string> preparefont(pddocument _pdfdocument) throws ioexception {     pddocumentcatalog doccatalog = _pdfdocument.getdocumentcatalog();     pdacroform acroform = doccatalog.getacroform();      pdresources res = acroform.getdefaultresources();     if (res == null)         res = new pdresources();      inputstream fontstream = getclass().getresourceasstream("liberationsans-regular.ttf"); inputstream fontstream2 = getclass().getresourceasstream("font2.ttf");     pdtruetypefont font = pdtruetypefont.loadttf(_pdfdocument, fontstream); pdtruetypefont font2 = pdtruetypefont.loadttf(_pdfdocument, fontstream2);     string fontname = res.addfont(font);  string fontname2 = res.addfont(font2);     acroform.setdefaultresources(res);     list<string> fontlist = new arraylist<string>();    fontlist.add(font1);fontlist.add(font2);     return fontlist; } 

(you can find runnable example here: fillformcustomfont.java)

using poor-man's-bold

  1. how bold field? ... need set bold field?

in pdf make text bold using font bold glyphs, see second question. if don't have such bold font @ hands, may instead use poor-man's-bold technique, e.g. not filling letter stroking line along borders:

public static void setfieldbold(string name, string value) throws ioexception {     pddocumentcatalog doccatalog = _pdfdocument.getdocumentcatalog();     pdacroform acroform = doccatalog.getacroform();     pdfield field = acroform.getfield(name);      cosdictionary dict = ((pdfield) field).getdictionary();     cosstring defaultappearance = (cosstring) dict             .getdictionaryobject(cosname.da);     if (defaultappearance != null)     {         dict.setstring(cosname.da, "/helv 10 tf 2 tr .5 w 0 g");         if (name.equalsignorecase("field1")) {             dict.setstring(cosname.da, "/helv 12 tf 0 g");         }     }     if (field instanceof pdtextbox)     {         field = new pdtextbox(acroform, dict);         ((pdfield) field).setvalue(value);     } } 

(2 tr .5 w = use rendering mode 2, i.e. fill , stroke, , use line width of .5)

instead of

using op's method

you get

using setfieldbold method

using custom fonts

  1. if understand right, there 14 basic fonts can use in pdfbox out of box (pun unintended). use 1 or more fonts signatures (cursive). out of box fonts that? if not, if have own font, how set in method written pdf?

if want use own font, first need register in acroform default resources this:

public string preparefont(pddocument _pdfdocument) throws ioexception {     pddocumentcatalog doccatalog = _pdfdocument.getdocumentcatalog();     pdacroform acroform = doccatalog.getacroform();      pdresources res = acroform.getdefaultresources();     if (res == null)         res = new pdresources();      inputstream fontstream = getclass().getresourceasstream("liberationsans-regular.ttf");     pdtruetypefont font = pdtruetypefont.loadttf(_pdfdocument, fontstream);     string fontname = res.addfont(font);     acroform.setdefaultresources(res);      return fontname; } 

this method returns font name use in

public static void setfield(string name, string value, string fontname) throws ioexception {     pddocumentcatalog doccatalog = _pdfdocument.getdocumentcatalog();     pdacroform acroform = doccatalog.getacroform();     pdfield field = acroform.getfield(name);      cosdictionary dict = ((pdfield) field).getdictionary();     cosstring defaultappearance = (cosstring) dict             .getdictionaryobject(cosname.da);     if (defaultappearance != null)     {         dict.setstring(cosname.da, "/" + fontname + " 10 tf 0 g");         if (name.equalsignorecase("field1")) {             dict.setstring(cosname.da, "/" + fontname + " 12 tf 0 g");         }     }     if (field instanceof pdtextbox)     {         field = new pdtextbox(acroform, dict);         ((pdfield) field).setvalue(value);     } } 

you get

using setfieldbold method font parameter

the difference not big because fonts quite similar. use font of choice more effect.

using /helv, /hebo, ...

the op found list of font names /helv, /hebo, ..., in pdfbox issue pdfbox-1234, appear usable without defining them in resource dictionary.

these names not pdf feature, i.e. pdf specification not know them, on contrary:

the default appearance string (da) contains graphics state or text state operators needed establish graphics state parameters, such text size , colour, displaying field’s variable text. operators allowed within text objects shall occur in string (see figure 9). @ minimum, string shall include tf (text font) operator along 2 operands, font , size. the specified font value shall match resource name in font entry of default resource dictionary (referenced dr entry of interactive form dictionary; see table 218).

(section 12.7.3.3 field dictionaries / variable text in iso 32000-1)

thus, specification not know default font names.

nonetheless, adobe reader/acrobat seem support them, because @ time in distant past form generating tool assumed them there , support forms kept due compatibility reasons.

using feature, therefore, might not best choice mileage may vary.

using custom , standard fonts

in comments op indicated wanted use both custom , standard fonts in forms.

to generalized method preparefont bit , refactored ttf import separate method:

public list<string> preparefont(pddocument _pdfdocument, list<pdfont> fonts) throws ioexception {     pddocumentcatalog doccatalog = _pdfdocument.getdocumentcatalog();     pdacroform acroform = doccatalog.getacroform();      pdresources res = acroform.getdefaultresources();     if (res == null)         res = new pdresources();      list<string> fontnames = new arraylist<string>();     (pdfont font: fonts)     {         fontnames.add(res.addfont(font));     }      acroform.setdefaultresources(res);      return fontnames; }  public pdfont loadtruetypefont(pddocument _pdfdocument, string resourcename) throws ioexception {     try ( inputstream fontstream = getclass().getresourceasstream(resourcename); )     {         return pdtruetypefont.loadttf(_pdfdocument, fontstream);     } } 

using these methods can mix custom , standard fonts this:

pddocument doc = pddocument.load(originalstream); list<string> fontnames = preparefont(doc, arrays.aslist(loadtruetypefont(doc, "liberationsans-regular.ttf"), pdtype1font.helvetica_bold));  setfield(doc, "firstname", "my first name", fontnames.get(0)); setfield(doc, "lastname", "my last name", fontnames.get(1));  doc.save(new file(result_folder, "acroform-setfieldcustomstandard.pdf")); doc.close(); 

(fillformcustomfont.testsetfieldcustomstandard_acroform)

resulting in

using mixed custom , standard fonts

pdtype1font has constants 14 standard fonts. thus, can use standard fonts (mixed custom fonts if desired) in form fields in way generates proper font entries in default resources, i.e. without relying on proprietary default font names hebo.

ps

any documentation on order /helv 10 tf 0 g arranged?

yes, there is, cf. specification iso 32000-1.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -