Q: How can I set a specific font to the pdf graphics in jPDFWriter?
– There are three basic fonts that are supported by all PDF viewers that do not need to be embedded in the document, they are Helvetica, Courier and Times Roman. To use these fonts, you should use the pre-defined objects in the PDFGraphics class, PDFGraphics.HELVETICA, PDFGraphics.COURIER and PDFGraphics.TIMESROMAN.
These objects are Font objects, so you should use their deriveFont() methods to use fonts of different size and style, i.e.:
g.setFont (PDFGraphics.HELVETICA.deriveFont (Font.BOLD, 12f);
– We also map some standard names of fonts back to the PDF fonts. For instance, if you use Java’s dialog font, it is mapped to Helvetica.
The mappings are:
- dialog -> Helvetica
- dialoginput -> Courier
- serif -> Times-Roman
- sansserif -> Helvetica
- monospaced -> Courier
- timesroman, times new
- roman -> Times-Roman
- arial -> Helvetica
– If none of the mappings apply, then by default we use the font “by reference”. This means that we put the font information in the document, including the name and character metrics, but we do not embed the font. If the document is viewed in an OS where that font is installed, then it should display correctly.
– Finally, you can embed fonts if you choose to. There is a method in the PDFDocument object called embedFont() to embed a font from a file. The method returns a Font object, which you can then use in a setFont() call to use it in the document.