Java JasperReports with custom font

JasperReports provides a feature to custom fonts that support UTF-8.

Scenario

The developer team gets the requirements from the clients.
The client wants to use the custom font for the company’s PDF file.

Create JasperReports.

<!-- Created with Jaspersoft Studio version 7.0.0.final using JasperReports Library version 7.0.0-b478feaa9aab4375eba71de77b4ca138ad2f62aa  -->
<jasperReport name="Blank_A4" language="java" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="2385a4f5-3779-4dca-9895-c6682d2a919c">
 <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
 <parameter name="P_MONTH" class="java.lang.String"/>
 <parameter name="P_YEAR" class="java.lang.String"/>
 <query language="sql"><![CDATA[]]></query>
 <field name="userId" class="java.lang.String"/>
 <field name="activityType" class="java.lang.String"/>
 <field name="activityTime" class="java.lang.String"/>
 <field name="ipAddress" class="java.lang.String"/>
 <field name="deviceType" class="java.lang.String"/>
 <field name="location" class="java.lang.String"/>
 <field name="browserType" class="java.lang.String"/>
 <field name="operatingSystem" class="java.lang.String"/>
 <background splitType="Stretch"/>
 <title height="48" splitType="Stretch">
  <element kind="staticText" uuid="c89c2c18-55e5-4446-9a82-25e6f5c0f06b" x="170" y="0" width="250" height="40" fontSize="26.0">
   <text><![CDATA[User Activity Report]]></text>
  </element>
 </title>
 <pageHeader height="20" splitType="Stretch">
  <element kind="textField" uuid="f98a5196-d2e6-44f9-97aa-65e3ce5c92fc" x="0" y="0" width="120" height="20">
   <expression><![CDATA["MONTH "+$P{P_MONTH}]]></expression>
  </element>
  <element kind="textField" uuid="264db46d-a662-4b6a-b659-89912f4e7093" x="120" y="0" width="120" height="20">
   <expression><![CDATA["YEAR "+$P{P_YEAR}]]></expression>
  </element>
 </pageHeader>
 <columnHeader height="30" splitType="Stretch">
  <element kind="staticText" uuid="33d063a8-53cd-47c2-ac6e-fc5e10c0e1fa" x="0" y="10" width="70" height="20" fontName="SansSerif" fontSize="11.0" hTextAlign="Center" vTextAlign="Middle">
   <text><![CDATA[User ID]]></text>
   <box padding="0">
    <topPen lineWidth="0.5"/>
    <leftPen lineWidth="0.5"/>
    <bottomPen lineWidth="0.5"/>
    <rightPen lineWidth="0.5"/>
   </box>
  </element>
  <element kind="staticText" uuid="096f62de-e9cf-4eb1-9569-b474dd2bc5d0" x="70" y="10" width="80" height="20" fontName="SansSerif" fontSize="11.0" hTextAlign="Center" vTextAlign="Middle">
   <text><![CDATA[Activity Type]]></text>
   <box padding="0">
    <topPen lineWidth="0.5"/>
    <leftPen lineWidth="0.5"/>
    <bottomPen lineWidth="0.5"/>
    <rightPen lineWidth="0.5"/>
   </box>
  </element>
  <element kind="staticText" uuid="e144d215-722b-42a0-9c4c-e9d4ad1e3a08" x="230" y="10" width="70" height="20" fontName="SansSerif" fontSize="11.0" hTextAlign="Center" vTextAlign="Middle">
   <text><![CDATA[IP Address]]></text>
   <box padding="0">
    <topPen lineWidth="0.5"/>
    <leftPen lineWidth="0.5"/>
    <bottomPen lineWidth="0.5"/>
    <rightPen lineWidth="0.5"/>
   </box>
  </element>
  <element kind="staticText" uuid="d32f3554-1c78-43dd-9618-f02f8ecca6b3" x="300" y="10" width="70" height="20" fontName="SansSerif" fontSize="11.0" hTextAlign="Center" vTextAlign="Middle">
   <text><![CDATA[Device Type]]></text>
   <box padding="0">
    <topPen lineWidth="0.5"/>
    <leftPen lineWidth="0.5"/>
    <bottomPen lineWidth="0.5"/>
    <rightPen lineWidth="0.5"/>
   </box>
  </element>
  <element kind="staticText" uuid="e44b90e9-3c69-43e3-a8d8-262c1af32d08" x="370" y="10" width="70" height="20" fontName="SansSerif" fontSize="11.0" hTextAlign="Center" vTextAlign="Middle">
   <text><![CDATA[Location]]></text>
   <box padding="0">
    <topPen lineWidth="0.5"/>
    <leftPen lineWidth="0.5"/>
    <bottomPen lineWidth="0.5"/>
    <rightPen lineWidth="0.5"/>
   </box>
  </element>
  <element kind="staticText" uuid="30a6bd46-013a-42dd-8d02-0046b54df694" x="440" y="10" width="74" height="20" fontName="SansSerif" fontSize="11.0" hTextAlign="Center" vTextAlign="Middle">
   <text><![CDATA[Browser Type]]></text>
   <box padding="0">
    <topPen lineWidth="0.5"/>
    <leftPen lineWidth="0.5"/>
    <bottomPen lineWidth="0.5"/>
    <rightPen lineWidth="0.5"/>
   </box>
  </element>
  <element kind="staticText" uuid="1f18e96c-5bca-4a9d-9192-f8caacb60d2d" x="514" y="10" width="41" height="20" fontName="SansSerif" fontSize="11.0" hTextAlign="Center" vTextAlign="Middle">
   <text><![CDATA[OS]]></text>
   <property name="com.jaspersoft.studio.unit.width" value="px"/>
   <box padding="0">
    <topPen lineWidth="0.5"/>
    <leftPen lineWidth="0.5"/>
    <bottomPen lineWidth="0.5"/>
    <rightPen lineWidth="0.5"/>
   </box>
  </element>
  <element kind="staticText" uuid="b150b5c6-b1d0-4142-b366-897aad526336" x="150" y="10" width="80" height="20" fontName="SansSerif" fontSize="11.0" hTextAlign="Center" vTextAlign="Middle">
   <text><![CDATA[Activity Time]]></text>
   <box padding="0">
    <topPen lineWidth="0.5"/>
    <leftPen lineWidth="0.5"/>
    <bottomPen lineWidth="0.5"/>
    <rightPen lineWidth="0.5"/>
   </box>
  </element>
 </columnHeader>
 <detail>
  <band height="20" splitType="Stretch">
   <element kind="textField" uuid="54d90e0d-3ebd-49e2-ad11-c1177887bb42" x="0" y="0" width="70" height="20" fontSize="8.0" hTextAlign="Center" vTextAlign="Middle">
    <expression><![CDATA[$F{userId}]]></expression>
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7c61c228-89fd-4310-b514-0522110739f0"/>
    <box padding="0">
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
   </element>
   <element kind="textField" uuid="a85f2022-27a6-44cc-bedc-8dcfae88041f" x="70" y="0" width="80" height="20" fontSize="8.0" hTextAlign="Center" vTextAlign="Middle">
    <expression><![CDATA[$F{activityType}]]></expression>
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7c61c228-89fd-4310-b514-0522110739f0"/>
    <box padding="0">
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
   </element>
   <element kind="textField" uuid="59d4a2f7-8724-4eaf-b9df-2ddcb434ae67" x="150" y="0" width="80" height="20" fontSize="8.0" hTextAlign="Center" vTextAlign="Middle">
    <expression><![CDATA[$F{activityTime}]]></expression>
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7c61c228-89fd-4310-b514-0522110739f0"/>
    <box padding="0">
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
   </element>
   <element kind="textField" uuid="598eff31-517f-42a2-9285-8a7304f5898e" x="230" y="0" width="70" height="20" fontSize="8.0" hTextAlign="Center" vTextAlign="Middle">
    <expression><![CDATA[$F{ipAddress}]]></expression>
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7c61c228-89fd-4310-b514-0522110739f0"/>
    <box padding="0">
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
   </element>
   <element kind="textField" uuid="09dcad81-7f6d-47ae-b26e-64560565dd5a" x="300" y="0" width="70" height="20" fontSize="8.0" hTextAlign="Center" vTextAlign="Middle">
    <expression><![CDATA[$F{deviceType}]]></expression>
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7c61c228-89fd-4310-b514-0522110739f0"/>
    <box padding="0">
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
   </element>
   <element kind="textField" uuid="8ee606b7-fa2b-4517-9ba2-16e6b111eee3" x="370" y="0" width="70" height="20" fontSize="8.0" hTextAlign="Center" vTextAlign="Middle">
    <expression><![CDATA[$F{location}]]></expression>
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7c61c228-89fd-4310-b514-0522110739f0"/>
    <box padding="0">
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
   </element>
   <element kind="textField" uuid="2c15f86e-d3c2-48e8-bfa8-79a4e52b2d4b" x="440" y="0" width="74" height="20" fontSize="8.0" hTextAlign="Center" vTextAlign="Middle">
    <expression><![CDATA[$F{browserType}]]></expression>
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7c61c228-89fd-4310-b514-0522110739f0"/>
    <box padding="0">
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
   </element>
   <element kind="textField" uuid="32f657d8-0867-4df7-9c3e-16fcff6ea6d0" x="514" y="0" width="41" height="20" fontSize="8.0" hTextAlign="Center" vTextAlign="Middle">
    <expression><![CDATA[$F{operatingSystem}]]></expression>
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7c61c228-89fd-4310-b514-0522110739f0"/>
    <property name="com.jaspersoft.studio.unit.width" value="px"/>
    <box padding="0">
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
   </element>
  </band>
 </detail>
 <columnFooter splitType="Stretch"/>
 <pageFooter height="30" splitType="Stretch">
  <element kind="textField" uuid="51dd90d5-1007-42db-9e97-7131585b309f" x="355" y="0" width="100" height="30" hTextAlign="Right">
   <expression><![CDATA["Page " + $V{PAGE_NUMBER}]]></expression>
  </element>
  <element kind="textField" uuid="80945c25-fde6-4116-8c2f-2e7d252c69f7" x="455" y="0" width="100" height="30" evaluationTime="Report" hTextAlign="Left">
   <expression><![CDATA[" of " + $V{PAGE_NUMBER}]]></expression>
  </element>
 </pageFooter>
 <summary height="7" splitType="Stretch"/>
</jasperReport>

Change fonts in JasperReports.

Font file.

The developer can add a font by selecting the menu
“Windows -> Preferences”.

Search Font In the search bar, select Fonts in the sub-menu Jaspersoft Studio.

Click the Add button in the right menu.

Name the font and select the ttf file in every tab.

PDF Encoding select “Identity-H (Unicode with horizontal writing)”.
Check Embed this font in a PDF document.

Click the “Finish” button.

The developer changed the fonts from “SansSerif” to “TH SarabunPSK” and the words to Thai.

<!-- Created with Jaspersoft Studio version 7.0.0.final using JasperReports Library version 7.0.0-b478feaa9aab4375eba71de77b4ca138ad2f62aa  -->
<jasperReport name="Blank_A4" language="java" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="2385a4f5-3779-4dca-9895-c6682d2a919c">
 <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
 <parameter name="P_MONTH" class="java.lang.String"/>
 <parameter name="P_YEAR" class="java.lang.String"/>
 <query language="sql"><![CDATA[]]></query>
 <field name="userId" class="java.lang.String"/>
 <field name="activityType" class="java.lang.String"/>
 <field name="activityTime" class="java.lang.String"/>
 <field name="ipAddress" class="java.lang.String"/>
 <field name="deviceType" class="java.lang.String"/>
 <field name="location" class="java.lang.String"/>
 <field name="browserType" class="java.lang.String"/>
 <field name="operatingSystem" class="java.lang.String"/>
 <background splitType="Stretch"/>
 <title height="48" splitType="Stretch">
  <element kind="staticText" uuid="c89c2c18-55e5-4446-9a82-25e6f5c0f06b" key="" x="80" y="0" width="380" height="40" fontName="TH SarabunPSK" fontSize="26.0" pdfFontName="" pdfEncoding="" hTextAlign="Center">
   <text><![CDATA[รายงานกิจกรรมผู้ใช้งานรายเดือน]]></text>
  </element>
 </title>
 <pageHeader height="20" splitType="Stretch">
  <element kind="textField" uuid="f98a5196-d2e6-44f9-97aa-65e3ce5c92fc" x="0" y="0" width="120" height="20" fontName="TH SarabunPSK">
   <expression><![CDATA["เดือน "+$P{P_MONTH}]]></expression>
  </element>
  <element kind="textField" uuid="264db46d-a662-4b6a-b659-89912f4e7093" x="120" y="0" width="120" height="20" fontName="TH SarabunPSK">
   <expression><![CDATA["ปี "+$P{P_YEAR}]]></expression>
  </element>
 </pageHeader>
 <columnHeader height="30" splitType="Stretch">
  <element kind="staticText" uuid="33d063a8-53cd-47c2-ac6e-fc5e10c0e1fa" x="0" y="10" width="70" height="20" fontName="SansSerif" fontSize="11.0" hTextAlign="Center" vTextAlign="Middle">
   <text><![CDATA[User ID]]></text>
   <box padding="0">
    <topPen lineWidth="0.5"/>
    <leftPen lineWidth="0.5"/>
    <bottomPen lineWidth="0.5"/>
    <rightPen lineWidth="0.5"/>
   </box>
  </element>
  <element kind="staticText" uuid="096f62de-e9cf-4eb1-9569-b474dd2bc5d0" x="70" y="10" width="80" height="20" fontName="SansSerif" fontSize="11.0" hTextAlign="Center" vTextAlign="Middle">
   <text><![CDATA[Activity Type]]></text>
   <box padding="0">
    <topPen lineWidth="0.5"/>
    <leftPen lineWidth="0.5"/>
    <bottomPen lineWidth="0.5"/>
    <rightPen lineWidth="0.5"/>
   </box>
  </element>
  <element kind="staticText" uuid="e144d215-722b-42a0-9c4c-e9d4ad1e3a08" x="230" y="10" width="70" height="20" fontName="SansSerif" fontSize="11.0" hTextAlign="Center" vTextAlign="Middle">
   <text><![CDATA[IP Address]]></text>
   <box padding="0">
    <topPen lineWidth="0.5"/>
    <leftPen lineWidth="0.5"/>
    <bottomPen lineWidth="0.5"/>
    <rightPen lineWidth="0.5"/>
   </box>
  </element>
  <element kind="staticText" uuid="d32f3554-1c78-43dd-9618-f02f8ecca6b3" x="300" y="10" width="70" height="20" fontName="SansSerif" fontSize="11.0" hTextAlign="Center" vTextAlign="Middle">
   <text><![CDATA[Device Type]]></text>
   <box padding="0">
    <topPen lineWidth="0.5"/>
    <leftPen lineWidth="0.5"/>
    <bottomPen lineWidth="0.5"/>
    <rightPen lineWidth="0.5"/>
   </box>
  </element>
  <element kind="staticText" uuid="e44b90e9-3c69-43e3-a8d8-262c1af32d08" x="370" y="10" width="70" height="20" fontName="SansSerif" fontSize="11.0" hTextAlign="Center" vTextAlign="Middle">
   <text><![CDATA[Location]]></text>
   <box padding="0">
    <topPen lineWidth="0.5"/>
    <leftPen lineWidth="0.5"/>
    <bottomPen lineWidth="0.5"/>
    <rightPen lineWidth="0.5"/>
   </box>
  </element>
  <element kind="staticText" uuid="30a6bd46-013a-42dd-8d02-0046b54df694" x="440" y="10" width="74" height="20" fontName="SansSerif" fontSize="11.0" hTextAlign="Center" vTextAlign="Middle">
   <text><![CDATA[Browser Type]]></text>
   <box padding="0">
    <topPen lineWidth="0.5"/>
    <leftPen lineWidth="0.5"/>
    <bottomPen lineWidth="0.5"/>
    <rightPen lineWidth="0.5"/>
   </box>
  </element>
  <element kind="staticText" uuid="1f18e96c-5bca-4a9d-9192-f8caacb60d2d" x="514" y="10" width="41" height="20" fontName="SansSerif" fontSize="11.0" hTextAlign="Center" vTextAlign="Middle">
   <text><![CDATA[OS]]></text>
   <property name="com.jaspersoft.studio.unit.width" value="px"/>
   <box padding="0">
    <topPen lineWidth="0.5"/>
    <leftPen lineWidth="0.5"/>
    <bottomPen lineWidth="0.5"/>
    <rightPen lineWidth="0.5"/>
   </box>
  </element>
  <element kind="staticText" uuid="b150b5c6-b1d0-4142-b366-897aad526336" x="150" y="10" width="80" height="20" fontName="SansSerif" fontSize="11.0" hTextAlign="Center" vTextAlign="Middle">
   <text><![CDATA[Activity Time]]></text>
   <box padding="0">
    <topPen lineWidth="0.5"/>
    <leftPen lineWidth="0.5"/>
    <bottomPen lineWidth="0.5"/>
    <rightPen lineWidth="0.5"/>
   </box>
  </element>
 </columnHeader>
 <detail>
  <band height="20" splitType="Stretch">
   <element kind="textField" uuid="54d90e0d-3ebd-49e2-ad11-c1177887bb42" x="0" y="0" width="70" height="20" fontSize="8.0" hTextAlign="Center" vTextAlign="Middle">
    <expression><![CDATA[$F{userId}]]></expression>
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7c61c228-89fd-4310-b514-0522110739f0"/>
    <box padding="0">
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
   </element>
   <element kind="textField" uuid="a85f2022-27a6-44cc-bedc-8dcfae88041f" x="70" y="0" width="80" height="20" fontSize="8.0" hTextAlign="Center" vTextAlign="Middle">
    <expression><![CDATA[$F{activityType}]]></expression>
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7c61c228-89fd-4310-b514-0522110739f0"/>
    <box padding="0">
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
   </element>
   <element kind="textField" uuid="59d4a2f7-8724-4eaf-b9df-2ddcb434ae67" x="150" y="0" width="80" height="20" fontSize="8.0" hTextAlign="Center" vTextAlign="Middle">
    <expression><![CDATA[$F{activityTime}]]></expression>
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7c61c228-89fd-4310-b514-0522110739f0"/>
    <box padding="0">
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
   </element>
   <element kind="textField" uuid="598eff31-517f-42a2-9285-8a7304f5898e" x="230" y="0" width="70" height="20" fontSize="8.0" hTextAlign="Center" vTextAlign="Middle">
    <expression><![CDATA[$F{ipAddress}]]></expression>
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7c61c228-89fd-4310-b514-0522110739f0"/>
    <box padding="0">
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
   </element>
   <element kind="textField" uuid="09dcad81-7f6d-47ae-b26e-64560565dd5a" x="300" y="0" width="70" height="20" fontSize="8.0" hTextAlign="Center" vTextAlign="Middle">
    <expression><![CDATA[$F{deviceType}]]></expression>
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7c61c228-89fd-4310-b514-0522110739f0"/>
    <box padding="0">
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
   </element>
   <element kind="textField" uuid="8ee606b7-fa2b-4517-9ba2-16e6b111eee3" x="370" y="0" width="70" height="20" fontSize="8.0" hTextAlign="Center" vTextAlign="Middle">
    <expression><![CDATA[$F{location}]]></expression>
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7c61c228-89fd-4310-b514-0522110739f0"/>
    <box padding="0">
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
   </element>
   <element kind="textField" uuid="2c15f86e-d3c2-48e8-bfa8-79a4e52b2d4b" x="440" y="0" width="74" height="20" fontSize="8.0" hTextAlign="Center" vTextAlign="Middle">
    <expression><![CDATA[$F{browserType}]]></expression>
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7c61c228-89fd-4310-b514-0522110739f0"/>
    <box padding="0">
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
   </element>
   <element kind="textField" uuid="32f657d8-0867-4df7-9c3e-16fcff6ea6d0" x="514" y="0" width="41" height="20" fontSize="8.0" hTextAlign="Center" vTextAlign="Middle">
    <expression><![CDATA[$F{operatingSystem}]]></expression>
    <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7c61c228-89fd-4310-b514-0522110739f0"/>
    <property name="com.jaspersoft.studio.unit.width" value="px"/>
    <box padding="0">
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
   </element>
  </band>
 </detail>
 <columnFooter splitType="Stretch"/>
 <pageFooter height="30" splitType="Stretch">
  <element kind="textField" uuid="51dd90d5-1007-42db-9e97-7131585b309f" x="355" y="0" width="100" height="30" hTextAlign="Right">
   <expression><![CDATA["Page " + $V{PAGE_NUMBER}]]></expression>
  </element>
  <element kind="textField" uuid="80945c25-fde6-4116-8c2f-2e7d252c69f7" x="455" y="0" width="100" height="30" evaluationTime="Report" hTextAlign="Left">
   <expression><![CDATA[" of " + $V{PAGE_NUMBER}]]></expression>
  </element>
 </pageFooter>
 <summary height="7" splitType="Stretch"/>
</jasperReport>
public static void generateReportCustomFont(){
    try {
        String jasperFile = "C:/JaspersoftWorkspace/MyReports/demo_custom_font.jasper";
        JasperReport jasperReport = (JasperReport) JRLoader.loadObjectFromFile(jasperFile);

        // Generate sample data
        List<UserActivity> activities = generateUserActivities();

        // Create a JRSwapFileVirtualizer
        JRSwapFile jrSwapFile = new JRSwapFile("C:/JaspersoftWorkspace/MyReports/",1024,1);
        JRSwapFileVirtualizer virtualizer = new JRSwapFileVirtualizer(20, jrSwapFile, true);


        // Create a JRDataSource from the list
        JRDataSource jrDataSource = new JRBeanCollectionDataSource(activities);

        // Parameters for the report
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("P_MONTH","กรกฎาคม");
        parameters.put("P_YEAR","2567");
        parameters.put(JRParameter.REPORT_VIRTUALIZER, virtualizer);

        // Fill the report
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, jrDataSource);

        // Export the report to a PDF file
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String outputFileName = "D:/pdf/report_"+df.format(new Date())+".pdf";
        JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);


        System.out.println("Report generated successfully!");
    } catch (JRException e) {
        e.printStackTrace();
    }
}

public static List<UserActivity> generateUserActivities() {
    List<UserActivity> activities = new ArrayList<>();

    for (int i = 1; i <= 20; i++) {
        UserActivity activity = new UserActivity();
        activity.setUserId(String.valueOf(i));
        activity.setActivityType("activity");
        activity.setActivityTime("2024-07-07 10:00:00");
        activity.setIpAddress("192.168.0.1");
        activity.setDeviceType("PC");
        activity.setLocation("US");
        activity.setBrowserType("CHROME");
        activity.setOperatingSystem("WINS");

        activities.add(activity);
    }

    return activities;
}
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class UserActivity {
    private String userId;
    private String activityType;
    private String activityTime;
    private String ipAddress;
    private String deviceType;
    private String location;
    private String browserType;
    private String operatingSystem;
}

Generate a PDF with JasperReports.

The developer gets an error message or a blank element.
The developer must export and add a font in a jar file to the Java application project.

Caused by: com.lowagie.text.DocumentException: Font '' with 'Cp1252' is not recognized.
 at com.lowagie.text.pdf.BaseFont.createFont(BaseFont.java:863)
 at com.lowagie.text.pdf.BaseFont.createFont(BaseFont.java:738)
 at com.lowagie.text.pdf.BaseFont.createFont(BaseFont.java:659)
 at net.sf.jasperreports.pdf.classic.ClassicFontRecipient.setFont(ClassicFontRecipient.java:87)
 ... 18 more

The PDF has missing text elements that use a custom font.

Blank element.

JasperReports with custom fonts.

The developer must export the font from JasperReport studio by going to the “Fonts” menu in “Preferences” and clicking the “Export” button.

Name the export jar file “THSarabun-1.0.jar” and save.

Inside a jar file.

Add the jar file to the Java application.

Jaspersoft iReport Designer.

For developers who use iReport, it is like version 5.6.0.
The developer must change the “PDF embedded” and “PDF encoding” in the elements and every element that uses a custom font.

PDF Encoding select “Identity-H (Unicode with horizontal writing)”.
Check Embed this font in a PDF document.

Add font jar file to local maven dependency.

The developer using Maven must register the font jar file as a dependency.

mvn install:install-file -Dfile=D:/workspaces/demo/libs/THSarabun-1.0.jar -DgroupId=com.example.fonts -DartifactId=example-fonts -Dversion=1.0.0 -Dpackaging=jar

Explain the details about a command.

mvn install:install-file This is the Maven command to install a file into the local repository.
dfile This specifies the path to the JAR file you want to install.
DgroupId This defines the artifact’s group ID. A group ID is a unique identifier for a project or group of projects.
DartifactId This defines the artifact ID for the JAR file. It is the name of the JAR without the version.
dversion This specifies the version of the JAR file.
Dpackaging This defines the packaging type of the artifact, which, in this case, is a JAR file.

In the “pom.xml” file.

<dependency>
  <groupId>com.example.fonts</groupId>
  <artifactId>example-fonts</artifactId>
  <version>1.0.0</version>
</dependency>

Generate a PDF with JasperReports using a custom font.

When the developer completes all of the steps, they generate a report.

Report generated successfully!
report_20240723162055.pdf

The developer gets a PDF file without any error message or blank elements.

Finally

JasperReports uses a custom font, which requires adding a font to JasperReports Studio and exporting the font into a jar file. Then, manually add a font jar to the Java application project or use a local Maven dependency.
The developer must complete all of the steps from the example. Otherwise, it gets an error or a blank space element.

Leave a Comment

Your email address will not be published. Required fields are marked *