Search code examples
fontsjasper-reportsexport-to-pdf

How to change default font size in report


How can I change the default font size in JasperReports's report?

I tried net.sf.jasperreports.default.pdf.font.size but it didn't work.

What I really want is to keep the same font and size when I export my report to pdf format using my application.

Pure JasperReports or even Java solutions are welcome.


Solution

  • This task can be solved at least with help of two approaches:

    Using default style

    We can create the default style in report and in case we did not specified attributes like font size for report's elements (for example, textField or staticText) the font size from default style will be applied.

    Example

    The jrxml will be like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <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="Default font size" pageWidth="842" pageHeight="595" orientation="Landscape" whenNoDataType="AllSectionsNoDetail" columnWidth="583" leftMargin="2" rightMargin="10" topMargin="2" bottomMargin="2" >
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
        <style name="defaultStyle" isDefault="true" fontSize="20"/>
        <title>
            <band height="383">
                <textField>
                    <reportElement x="10" y="30" width="100" height="50"/>
                    <textFieldExpression><![CDATA["Text Field"]]></textFieldExpression>
                </textField>
                <staticText>
                    <reportElement x="10" y="100" width="802" height="55"/>
                    <textElement textAlignment="Center" verticalAlignment="Middle"/>
                    <text><![CDATA[Static Text]]></text>
                </staticText>
            </band>
        </title>
    </jasperReport>
    

    In this report we have declaration of default style with font size: <style name="defaultStyle" isDefault="true" fontSize="20"/>

    The output result

    For template with default style the result generated in Jaspersoft Studio will be:

    The preview in JSS. Using default style

    Without using the default style the result will be:

    The preview in JSS. Without default style

    Using net.sf.jasperreports.default.font.size property

    If we have a Java code for generating reports we can use our own copy of jasperreports.properties file.

    I changed the default value with my own in jasperreports.properties file:

    net.sf.jasperreports.default.font.size=20
    

    and rebuild report with Java code which use JRPdfExporter exporter.

    For the same template, but without any style the result will be:

    The view of generated pdf file

    If we tried to change value of net.sf.jasperreports.default.font.size property for example with 10 (this is a default value) the result will be:

    The view of generated pdf file


    Notes:

    The jasperreports.properties file should be placed at classpath.