Jasper Reports Report Fonts - JasperReports

The text settings of the report facilitate the font settings of its own. The <font> tag under the <textElement> tag specifies the font settings. Any number of fonts can be defined by a report and which once defined are used as base font settings.

What are the Jasper Report Fonts?

The font settings combine together to form report font. While setting the font properties of the text elements, the report font can be reused throughout the report.

It is suggested not to use the <reportFont/> elements within the document and instead to use the <style/> element.

What are the attributes of Jasper Reports Fonts?

The attributes of the <font> element are as follows:

S.NO
Attribute and Description
1
fontName
The font name, which can be the name of a physical font, a logical one, or the name of a font family from the registered JasperReports font extensions.
2
size
The size of the font measured in points. It defaults to 10.
3
isBold
The flag specifying if a bold font is required. It defaults to false.
4
isItalic
The flag specifying if an italic font is required. It defaults to false.
5
isUnderline
The flag specifying if the underline text decoration is required. It defaults to false.
6
isStrikeThrough
The flag specifying if the strikethrough text decoration is required. It defaults to false.
7
pdfFontName
The name of an equivalent PDF font required by the iText library when exporting documents to PDF format.
8
pdfEncoding
The equivalent PDF character encoding, also required by the iText library.
9
isPdfEmbedded
The flag that specifies whether the font should be embedded into the document itself. It defaults to false. If set to true, helps view the PDF document without any problem.

What are the different font types provided by Jasper Reports?

Jasper Reports provide the following types of fonts:

  • Logical Fonts – The fonts that are recognized by the Java like Serif, SansSerif, Monospaced, Dialog, and DialogInput are known as logical fonts. They are the fonts that are being recognized by Java and are not included in the font library. Hence they are required to be mapped to any physical font installed in the system.
  • Physical Fonts – The fonts which are actually present in the font library such as Arial, Time, Helvetica, Courier, or any other international fonts.
  • Font Extensions – The font extension is used for making available the font families. The font faces that look similar make up the font families.

In the font name attribute it is essential to specify the name of the physical font, logical font and the font family from the font extensions.

PDF Font Name

To export the reports to PDF, the iText library is used by Jasper Reports. When viewed on various platforms, the PDF files look the same. The fontname attribute is not required when the report is exported to PDF. The font settings are specified in the pdffontname attribute.

The following are some of the built-in font names:

  • Courier
  • Courier-Bold
  • Courier-BoldOblique
  • Courier-Oblique
  • Helvetica
  • Helvetica-Bold
  • Helvetica-BoldOblique
  • Helvetica-Oblique
  • Symbol
  • Times-Roman
  • Times-Bold
  • Times-BoldItalic
  • Times-Italic
  • ZapfDingbats

To use these fonts, it is essential to specify the font name from one among them:

  • A built-in font name from the above list.
  • The name of a TTF (True Type Font) file, which it can locate on disk.
  • The real name of the font, that is provided by the iText library.
  • The pdffontname attribute contains one of the values from
  • The name of a built-in PDF font from the above list.
  • The name of a TTF file that can be located on disk at runtime when exporting to PDF.
  • The real name of a registered font.
  • The suffix of the key (the part after net.sf.jasperreports.export.pdf.font) for a font registered with iText as a font file.

Whatare the default fonts and their inheritance?

The font and the style attributes are inherited from the parent element. If style and font are not defined for the elements, the default style and font are applied, that is declared in the <jasperReport/> root element.

The font properties and the values defined in the file /src/default.jasperreports.properties are as follows:

Property
Description
net.sf.jasperreports.default.font.name=SansSerif
The default font name.
net.sf.jasperreports.default.font.size=10
The default font size.
net.sf.jasperreports.default.pdf.font.name=Helvetica
The default PDF font.
net.sf.jasperreports.default.pdf.encoding=Cp1252
The default PDF character encoding.
net.sf.jasperreports.default.pdf.embedded=false
By default PDF fonts are not embedded.

Example

For instance, a new report template jasper_report_template.jrxml is written. The contents of the JRXML are as follows, which are saved to C:\tools\jasperreports-5.0.1\test directory. In various font formats, in the title of the report, a text is displayed.

The report is filled and generated by the following Java code. The file JasperFontsReportFill.java is saved to C:\tools\jasperreports-5.0.1\test\src\com\wisdomjobs directory.

An instance of the JREmptyDataSource is used for filling reports when all the fields are being null.

How the report is generated using Fonts in Jasper Reports?

The above file is compiled and executed by the ANT build process. The content of the file are saved under C:\tools\jasperreports-5.0.1\test as below. The import file is to be placed in the same directory as build.xml which is extracted from the chapter Environment Setup.

Open command line window and go to the directory where build.xml is placed. Execute the command ant -Dmain-class=com.wisdomjobs.JasperFontsReportFill

The compilation of the above code results in a JasperViewer window that appears as follows:

Report Fonts Example

It is observed that the text “Welcome to Wisdomjobs” is displayed using fonts of different formats.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JasperReports Topics