How to use a font from JasperSoft Studio when exporting to PDF enabling bold and italic styling?

341 Views Asked by At

In JasperReports, I design a report, fill it from fields using a custom data source and export to PDF. I would like to use a dynamic font given by a field from my custom data source. Referenced in the text field using a property, I would like to thus enable a dynamic font that can change for each exported PDF based on by custom data source. All my text fields use the ,,Styled'' markup.

How can I use the fonts that JasperSoft Studio offers me for textfields (see image below) when exporting to PDF, using bold and italic styled markup?

enter image description here

At first I only added a custom font to my text fields:

<textField>
    <reportElement x="0" y="0" width="185" height="37" isRemoveLineWhenBlank="true" uuid="97...1f">
        <property name="com.jaspersoft.studio.unit.height" value="px"/>
        <propertyExpression name="net.sf.jasperreports.style.fontName"><![CDATA[$F{fontName}]]></propertyExpression>
    </reportElement>
    <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>

The above snippet throws an error when I export to PDF because when, for example, the field fontName equals "Times New Roman", JasperSoft Studio 6.20.1 does preview me the correct font, but the export to PDF fails since "Times New Roman" is not a PDF font.

I attempted to fix this by explicitely setting pdfFontName for a text field as well, using a field provided by a custom data source:

<property name="net.sf.jasperreports.style.isPdfEmbedded" value="true"/>
<propertyExpression name="net.sf.jasperreports.style.fontName"><![CDATA[$F{fontName}]]></propertyExpression>
<propertyExpression name="net.sf.jasperreports.style.pdfFontName"><![CDATA[$F{pdfFontName}]]></propertyExpression>

When I now set fontName = "Times New Roman" and pdfFontName = "Times-Roman", JasperSoft Studio does preview me the report with "Times New Roman" and exports me the report to PDF using "Times-Roman".

However, now the problem is that the exported PDF does not contain bold or italic text. Based on my limited understanding of fonts, there is a separate font for "Times-Roman", "Times-Roman Bold" and "Times-Roman Italic". So when I explicitely set it to "Times-Roman", it is always "Times-Roman" and the bold and italic markup ist lost.

Doing my research, I found a document describing how you can add font extensions to JasperSoft Studio. This seems to enable one to map a font in JasperSoft Studio to a font when exporting to PDF, including what font to use for bold and italic styled text. When one uses "Times New Roman", it would then export to "Times-Roman", as well as respective bold and italic style when defined.

However, since JasperSoft Studio already offers me so many fonts for text fields (see image in the beginning), is there a way to make those work, mapping them to a PDF font and having bold and italic style in texts? By default, JasperSoft Studio only supports DeJaVu, it seems.

Is there a straightforward way to reference the font "Times New Roman" that JasperSoft Studio offers to setup the font extension or is there absolutely no way around getting the TFF file for "Times New Roman" from somewhere (on Windows, they are not stored as TFF it seems) and manually adding the font extension for it?

0

There are 0 best solutions below