6

I am using Jasper jar to generate report on my J2EE project. I am able to generate the PDFs successfully without any issues. However I want the font name to be dynamically changed for all my PDFs based on settings which we configure on one place.

I came to know Conditional style which is useful to accomplish this. However I do have hundreds of fonts which cannot be conditionally styled on each and every place. This will make the report more worst.

I am looking for a suitable solution which helps me to change the fonts on jasper PDFs dynamically.

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Srinath Mahe
  • 91
  • 1
  • 7
  • Conditional styles is special for such cases. You don't need to recompile the templates to use new changed style(s) – Alex K Oct 20 '17 at 12:33

2 Answers2

6

The simplest way I can come up with is to use default style in report and change its font name via java before you fill the report.

Example

Set a style (I will use default style) in jrxml, since this way I don't need to assign it to textField and it's quicker to get from JasperReport object

<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="reputation" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a88bd694-4f90-41fc-84d0-002b90b2d73e">
    .....
   <style name="myStyle" isDefault="true" fontName="DejaVu Sans"/>
    ....
</jasperReport>

in java loaded your report (jrxml), change default style font name and then fill the report

JasperReport report = JasperCompileManager.compileReport("jmyReport.jrxml");
report.getDefaultStyle().setFontName("NewFontName");
JasperPrint jasperPrint = JasperFillManager.fillReport(report, paramMap,datasource);

Hower remember you need to add all your font's in Font Extensions!, if you like to ensure that they are rendered correctly in your pdf export on client machine.

You can also use non default style, in this case you need to assign it to textField and find it in the JRStyle[] styles = report.getStyles(); by comparing on JRStyle#getName()

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
  • Thank you for your inputs. report.getDefaultStyle().setFontName("NewFontName"); What are all the font name can i assign here, can you give some examples. – Srinath Mahe Oct 26 '17 at 06:04
  • @SrinathMahe Name of a font like "SansSerif", "DejaVu Sans", "Arial" etc, however watch out if you like to render it correctly in pdf you need [font-extensions](https://stackoverflow.com/questions/3811908/font-is-not-available-to-the-jvm-with-jasper-reports/35549391#35549391). You need to study careful what this means before you start to generate pdf with fonts that user may not have installed on pc. – Petter Friberg Oct 26 '17 at 08:06
0
report.getDefaultStyle().setFontName("NewFontName");

Incase using jasperreports-fonts-6.10.0.jar, "NewFontName" is family font name (ex : sans-serif) which is declared in jasperreports_extension.properties, isnt't it?

ex:

net.sf.jasperreports.extension.simple.font.families.sans-serif=fonts/sans-serif.xml
Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
hungncv
  • 21
  • 1
  • 3
  • I found right way : "NewFontName" is font name which is setting in fonts.xml inner font extention (jar). ex : report.getDefaultStyle().setFontName("Helvetica"); and fonts.xml has Helvetica : <![CDATA[fonts/Helvetica/Helvetica.ttf]]> <![CDATA[Identity-H]]> <![CDATA[true]]> – hungncv Nov 15 '19 at 12:13