Hi, > Am 30.03.2017 um 15:54 schrieb Evan Williams <[email protected]>: > > I would like to change default font of every field in an existing PDF > form.This suggests to me that I should change the default appearance of the > acroform and, so be on the safe side (since I am not the author of these > forms), change the default appearance of each PDTextField (I don't care > about choice fields &c.) > > All of the examples that I have been able to find of changing field > appearances are PDFBox 1.8 examples that get the COS Dictionary, mess > around with it and reset the field dictionary AND THEN CREATE A NEW > INSTANCE OF THE FIELD (e.g. java - How to set the text of a PDTextbox to a > color? - Stack Overflow > <http://stackoverflow.com/questions/22073137/how-to-set-the-text-of-a-pdtextbox-to-a-color/22082301#22082301>), > which presents me with practical and aesthetic problems. The explanation > given for this is 'The field variable has to be renewed in between because > `PDVariableText` stores the default appearance in a hidden member during > initialization'. I would really like to avoid doing this if I don't have to. > > I would think that, in 2.0, I could get away with something like: > > > * field.setDefaultAppearance("/Helv 0 Tf 0 g");*
you can get away with that IF there is also a font entry with the name /Helv in the AcroForm Default Resources. So a more complete solution looks something like this: // load the new font which shall be used for the form fields // we use a TT font here but could also use other types of fonts // if unicode support is needed stay with the sample font type PDType0Font font = PDType0Font.load(doc, …); PDAcroForm form = doc.getDocumentCatalog().getAcroForm(); PDResources resources = form.getDefaultResources(); String fontName = resources.add(font).getName(); String defaultAppearanceString = "/" + fontName + " 12 Tf 0 g"; form.setDefaultResources(resources); PDTextField field = (PDTextField) form.getField(...); field.setDefaultAppearance(da); Hope there are no typos. BR Maruan > > Can I get away with just that? Or do I need to create a new field instance? > -- > *Evan Williams* > Sr. Software Engineer > [email protected] > > *www.ZappRx.com <http://www.zapprx.com/>* --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

