refermycode.com

Develop your world..

Add new font in Jasper Reports Font Library.

In this blog we are going to add new font in Jasper Font Library to remove font dependency. Hope you have read my previous blog on Remove font dependency from Jasper Report Font Library. As discussed in previous blog if we are using any font which is not present in our Jasper Report Font Library then it will raised following exception while export phase,

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

we can also resolve above exception by adding the font which is missing in our Jasperreports-font.X.X.X.jar Library. So let us have look on this method in detail.

Method 2 : by adding missing font property.
To add missing fonts into the library we can create our own custom library for Jasperreports-font.X.X.X.jar. So follow below given steps to create your custom font library for Jasper Report Font with the help of iReport.

Step 1:
To install missing fonts in iReport go to ” Tools >> Options >> Fonts >> Install Font ” as shown in following screenshot.

Blog2_2

Step 2:
Add your fonts ttf file from browse button and click on Next button as shown in following screenshot.

Blog2_3

Step 3:
After adding fonts ttf file, you need to add family details by adding Bold, Italic and Bold-Italic version of font. If you have not added these version of fonts then it will not change the font style even if we add in our report design. Click on next button after adding the font family version ttf files as shown in following screenshot.

Blog2_4

Step 4:
In this step select the locale for your region, In this example I have added English(India) as shown in following screenshot.

Blog2_5

Step 5:
After that you can also manage the font mapping for avoiding the missing font property in OS.
as it is not mandatory you can skip this and click on finish button as shown in following screenshot.

Blog2_6

Step 6:
You can add multiple fonts by repeating step 2 to 5. After adding all required fonts click on Export as Extension button and save the the library with .jar Extension. as shown in following screenshot.

Blog2_7

Step 7:
After following all above steps you will get the your custom library with all your fonts.

Blog2_8

Now you can use this jar for Jasperreports-font.X.X.X.jar which will be present in your project library or classpath. After replacing this library you will get the your desire output with proper fonts.

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

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

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 🙂