HOW-TO Use custom fonts
JasperReports gives the possibility to use custom fonts (like true type fonts) and also embed them to the resulting pdf report. DynamicJasper also allows this feature.
|ar.com.fdvs.dj.test.FontReportTest||Shows how to create reports using custtom fonts|
The code example
Font font = new Font(28,"Colonna MT","/fonts/COLONNA.TTF", Font.PDF_ENCODING_Identity_H_Unicode_with_horizontal_writing, true); Style titleStyle = new StyleBuilder(false).setFont(font).build();
The constructor of the Font class has the parameters for embedding the font
public Font(int fontSize, String fontName, String pdfFontName, String pdfFontEncoding, boolean pdfFontEmbedded)
- fontSize is the size of the font is points
- fontName is the name of the font (as shown by the system font viewer, not just the filename)
- pdfFontName is the relative path inside the classpath to the ttf font. You can also point to other file types supported by JasperReports
- pdfFontEncoding specifies the encoding to use. Use one of the constants provided in the ar.com.fdvs.dj.domain.constants.Font class
- pdfFontEmbedded if true, the font file will be embedded into the pdf file. This ensures the pdf file to be shown correctly, but the size of the file will be increased depending the size of the ttf file.
Note that the ttf file must be in the classpath
Other things you should know about JasperReport and Fonts
1.- The recommendation is to package your fonts
2.- If the ttf is not installed in your system (e.g. Windows), you will not see in the preview screen the font. Just copy the desired ttf to Windows fonts folder and run again the report.
3.- If you embed the font into the PDF, it doesn’t matter if you don’t see it in the previewer, the final pdf will be OK.