refermycode.com

Develop your world..

Remove font dependency in Jasper Report.

In this blog we are going to learn cool stuff which will help us to Remove font dependency in Jasper Report Font Library. So let us see how we can remove the font dependency from Jasper Report Java Application. First of all let us see why we are going to remove the font dependency from Jasper.
So Answer is here,

Jasper Report requires 2 libraries for generating any report,
1. Jasperreport.X.X.X.jar
2. Jasperreports-font.X.X.X.jar

when we are create any Jasper Report application then it requires some fonts for writing data in export phase, so it will take the font from Jasperreports-font.X.X.X.jar library. Generally Jasperreports-font.X.X.X.jar library has some default fonts shown in below screenshot.

Blog2_1

There are following default fonts in Jasper Report library

  • DejaVu Sans
  • DejaVe Sans Mono
  • DejaVu Serif
  • Monospaced
  • SansSerif
  • Serif

For each above font there are 4 different font’s .ttf files are present for font faces which are used for Normal, Bold, Italic and Bold-Italic style. If we have used any font which is not present in our Jasperreports-font.X.X.X.jar library then it will throw an exception something like

net.sf.jasperreports.engine.util.JRFontNotFoundException:
Font ‘Font_Name’ is not available to the JVM.

This exception occurs due to unavailability of font in Jasper report font library. We can not move further until and unless resolve this exception, So now we will learn how to avoid such types of exception while playing with Jasper Report and different types of Fonts.

There are two method to avoid such a problem.
Method 1 : by setting ignore missing font property and default font property.
Method 2 : by adding missing font property.
We will see Method 2 into next blog. For Now Let us focus on Method 1 in detail.

In this method we can avoid above exception by ignoring the missing font property and using default font instead of missing font from our Jasperreports-font.X.X.X.jar library. In Jasper Report library all properties are already defined in default.jasperreports.properties file. There is one property present “net.sf.jasperreports.awt.ignore.missing.font” which help us to remove font dependency from Jasper Report design but it is by default set to “false”. If we try to export with this setting and at the time of filling data in report, Jasper Report fill manager fails to find the mentioned font in Jasper Report design then it will throws an exception which is mentioned earlier in this blog.
So we can avoid missing font exception by setting following property in Java Code.

JRProperties.setProperty("net.sf.jasperreports.awt.ignore.missing.font", "true");

or we can set this property by entering following line into .jrxml file.

<property name="net.sf.jasperreports.awt.ignore.missing.font" value="true"/>

By using above code we can remove font dependency but for exporting report Jasper Needs default font which will use instead of missing fonts. Default Font property is also predefined into default.jasperreports.properties file with the property “net.sf.jasperreports.default.font.name”. We can use the default font in place of our missing font which is mentioned in Jasper Report library. The value of default font is set to “SansSerif”, If we want any other font for it then we can change it manually.

For setting any default font name, we can set following property in Java Code.

JRProperties.setProperty("net.sf.jasperreports.default.font.name", "Default_Font_Name");

or we can set this property by entering following line into .jrxml file.

<property name="net.sf.jasperreports.default.font.name" value="Default_Font_Name"/>

In this way we can add above properties into our application which help us to remove the font dependency from Jasper Report. In next blog we will see the how to add our fonts in Jasper Report Font Library.

Hope this blog will help you to remove font dependency in Jasper Report Font Library.

Please don’t forget to comment your feedback for this post 🙂

Post a comment