Jasper Reports 简明教程

Report Fonts

报表含文本元素,而这些元素各自可具有其单独的字体设定,可使用 <textElement> 标签内的 <*font*> 标签来指定这些设定,报表能够定义许多字体,一旦定义后,它们可作为整个报表其他字体定义的默认字体或基本字体设定来使用。

A report contains text elements and each of these can have its own font settings. These settings can be specified using the <*font*> tag available in the <textElement> tag. A report can define a number of fonts. Once defined, they can be used as default or base font settings for other font definitions throughout the entire report.

Report Fonts

报表字体是字体设定的集合,在报表级别声明,在设置文本元素的字体属性时,报表字体可以在整个报表模板中重复使用。

A report font is a collection of font settings, declared at the report level. A report font can be reused throughout the entire report template when setting the font properties of text elements.

报表字体现已被弃用,请勿使用在文档自身中声明的 <reportFont/> 元素,而应改用 <style/> 元素。

Report fonts are now deprecated. Do not use <reportFont/> elements declared within the document itself. Use the <style/> element instead.

Font Attributes

下表概述了 <*font*> 元素的主要属性−

Table below summarizes the main attributes of the <*font*> element −

S.NO

Attribute and Description

1

fontName The font name, which can be the name of a physical font, a logical one, or the name of a font family from the registered JasperReports font extensions.

2

size The size of the font measured in points. It defaults to 10.

3

isBold The flag specifying if a bold font is required. It defaults to false.

4

isItalic The flag specifying if an italic font is required. It defaults to false.

5

isUnderline The flag specifying if the underline text decoration is required. It defaults to false.

6

isStrikeThrough The flag specifying if the strikethrough text decoration is required. It defaults to false.

7

pdfFontName The name of an equivalent PDF font required by the iText library when exporting documents to PDF format.

8

pdfEncoding The equivalent PDF character encoding, also required by the iText library.

9

isPdfEmbedded The flag that specifies whether the font should be embedded into the document itself. It defaults to false. If set to true, helps view the PDF document without any problem.

Font Types

在 JasperReports 中,字体可分类为:

In JasperReports fonts can be categorized as −

  1. Logical Fonts − Five font types, which have been recognized by the Java platform since version 1.0, are called logical fonts. These are − Serif, SansSerif, Monospaced, Dialog, and DialogInput. These logical fonts are not actual font libraries that are installed anywhere on the system. They are merely font type names recognized by the Java runtime. These must be mapped to some physical font that is installed on the system.

  2. Physical Fonts − These fonts are the actual font libraries consisting of, for example, TrueType or PostScript Type 1 fonts. The physical fonts may be Arial, Time, Helvetica, Courier, or any number of other fonts, including international fonts.

  3. Font Extensions − The JasperReports library can make use of fonts registered on-the-fly at runtime, through its built-in support for font extensions. A list of font families can be made available to the JasperReports using font extension. These are made out of similarly looking font faces and supporting specific locales.

如上表所述,我们需要在 fontName 属性中指定物理字体的名称、逻辑字体的名称或注册的 JasperReports 字体扩展中字体系列的名称。

As described in the table above we need to specify in the attribute fontName the name of a physical font, the name of a logical font, or the name of a font family from the registered JasperReports font extensions.

PDF Font Name

当导出报表为 PDF(便携式文档格式)时,JasperReports 库使用 iText 库。可在各种平台上查看 PDF 文件,并且始终外观相同。这部分是因为在此格式中,有一种处理字体的特殊方式。向 PDF 导出时,fontName 属性没有任何作用。存在 pdfFontName 属性,我们需要在此属性中指定字体设置。

JasperReports library uses the iText library, when exporting reports to PDF(Portable Document Format). PDF files can be viewed on various platforms and will always look the same. This is partially because in this format, there is a special way of dealing with fonts. fontName attribute is of no use when exporting to PDF. Attribute pdfFontName exist where we need to specify the font settings.

iText 库知道如何处理内置字体和 TTF 文件,并且识别以下内置字体名称:

The iText library knows how to deal with built-in fonts and TTF files and recognizes the following built-in font names −

  1. Courier

  2. Courier-Bold

  3. Courier-BoldOblique

  4. Courier-Oblique

  5. Helvetica

  6. Helvetica-Bold

  7. Helvetica-BoldOblique

  8. Helvetica-Oblique

  9. Symbol

  10. Times-Roman

  11. Times-Bold

  12. Times-BoldItalic

  13. Times-Italic

  14. ZapfDingbats

根据 iText 库的先决条件,要使用字体,我们需要将以下内容之一指定为字体名称:

As per iText library pre-requisite, to work with fonts, we need to specify one of the following as the font name −

  1. A built-in font name from the above list.

  2. The name of a TTF (True Type Font) file, which it can locate on disk.

  3. The real name of the font, provided that the TTF file containing the font has been previously registered with iText or that an alias was defined when the font was registered.

基于上述先决条件,pdfFontName 属性可包含以下值之一:

Based on the above pre-requisites, the pdfFontName attribute can contain one of the following values −

  1. The name of a built-in PDF font from the above list.

  2. The name of a TTF file that can be located on disk at runtime when exporting to PDF.

  3. The real name of a registered font.

  4. The suffix of the key (the part after net.sf.jasperreports.export.pdf.font) for a font registered with iText as a font file.

Default Fonts and Inheritance

每个文本元素从其父元素继承字体和样式属性,而其父元素反过来又从其父元素继承这些属性。如果未为元素定义样式和/或字体,那么将在 <jasperReport/> 根元素中声明的默认样式(和/或字体,但现在这不赞扬)被应用。

Each text element inherits font and style attributes from its parent element, which in turn inherits these attributes from its parent. If no styles and/or fonts are defined for elements, the default style (and/or font - but this is now deprecated) declared in the <jasperReport/> root element will be applied.

在 JasperReports 中定义默认样式或字体不是强制性的。如果没有为给定元素定义字体,引擎会查找继承的字体属性,或者如果没有通过这种方式找到属性,则会在 /src/default.jasperreports.properties 文件中查找 net.sf.jasperreports.default.font.name 属性。其值定义了在未为文本元素显式定义字体属性或从其父元素继承字体属性时要使用的字体系列名称。

Defining default styles or fonts in JasperReports is not mandatory. If no font is defined for a given element, the engine looks either for the inherited font attributes, or, if no attributes are found on this way, it looks for the net.sf.jasperreports.default.font.name property in the /src/default.jasperreports.properties file. Its value defines the name of the font family to be used when font properties are not explicitly defined for a text element or inherited from its parent.

在 /src/default.jasperreports.properties 文件中定义的主默认字体属性及其值在下表中 -

The main default font properties and their values defined in the /src/default.jasperreports.properties file are in the table below −

Property

Description

net.sf.jasperreports.default.font.name=SansSerif

The default font name.

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

The default font size.

net.sf.jasperreports.default.pdf.font.name=Helvetica

The default PDF font.

net.sf.jasperreports.default.pdf.encoding=Cp1252

The default PDF character encoding.

net.sf.jasperreports.default.pdf.embedded=false

By default PDF fonts are not embedded.

Example

为了演示如何使用字体和字体属性以获得特定文本外观,我们编写新的报表模板 (jasper_report_template.jrxml)。JRXML 的内容如下。将其保存到 C:\tools\jasperreports-5.0.1\test 目录中。在这里,我们在报表的标题中以各种字体格式显示文本。

To demonstrate using fonts and font attributes in order to get a particular text appearance, let’s write new report template (jasper_report_template.jrxml). The contents of the JRXML are as below. Save it to C:\tools\jasperreports-5.0.1\test directory. Here, we will display a text in the title of the report in various font formats.

<?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 = "jasper_report_template" pageWidth = "595" pageHeight = "842"
   columnWidth = "555" leftMargin = "20" rightMargin = "20" topMargin = "30"
   bottomMargin = "30">

   <title>
      <band height = "682">

      <staticText>
         <reportElement x = "0" y = "50" width = "150" height = "40"/>
         <textElement/>

         <text>
            <![CDATA[Welcome to TutorialsPoint!]]>
         </text>
      </staticText>

      <staticText>
         <reportElement x = "160" y = "50" width = "390" height = "40"/>
         <textElement/>

         <text>
           <![CDATA[<staticText>
           <reportElement x = "0" y = "50" width = "150" height = "40"/>
           <text>Welcome to TutorialsPoint!</text></staticText>]]>
         </text>
      </staticText>

      <staticText>
         <reportElement x = "0" y = "100" width = "150" height = "40"/>

         <textElement>
            <font size = "12"/>
         </textElement>

         <text><![CDATA[Welcome to TutorialsPoint!]]></text>
      </staticText>

      <staticText>
         <reportElement x = "160" y = "100" width = "390" height = "40"/>
         <textElement/>

         <text>
            <![CDATA[<staticText>
            <reportElement x = "0" y = "100" width = "150" height = "40"/>

            <textElement>
               <font size = "14"/>
            </textElement>

            <text> Welcome to TutorialsPoint!</text></staticText>]]>
         </text>
      </staticText>

      <staticText>
         <reportElement x = "0" y = "150" width = "150" height = "40"/>

         <textElement>
            <font fontName = "DejaVu Serif" size = "12" isBold = "false"/>
         </textElement>

         <text><![CDATA[Welcome to TutorialsPoint!]]></text>
      </staticText>

      <staticText>
         <reportElement x = "160" y = "150" width = "390" height = "40"/>
         <textElement/>

         <text>
            <![CDATA[<staticText>
            <reportElement x = "0" y = "250" width = "150" height = "40"/>

            <textElement>
               <font fontName = "DejaVu Serif" size = "12" isBold = "false"/>
            </textElement>

            <text>Welcome to TutorialsPoint!</text></staticText>]]>
         </text>
      </staticText>

      <staticText>
         <reportElement x = "0" y = "200" width = "150" height = "40"/>

         <textElement>
            <font fontName = "DejaVu Serif" size = "12" isBold = "true"/>
         </textElement>

         <text><![CDATA[Welcome to TutorialsPoint!]]></text>
      </staticText>

      <staticText>
         <reportElement x = "160" y = "200" width = "390" height = "40"/>
         <textElement/>

         <text>
            <![CDATA[<staticText>
            <reportElement x = "0" y = "300" width = "150" height = "40"/>

            <textElement>
               <font fontName = "DejaVu Serif" size = "12" isBold = "true"/>
            </textElement>

            <text>Welcome to TutorialsPoint!</text></staticText>]]>
         </text>
      </staticText>

      <staticText>
         <reportElement x = "0" y = "250" width = "150" height = "40"/>

         <textElement>
            <font fontName = "Monospaced" size = "12" isItalic = "true"
               isUnderline = "true" pdfFontName = "Courier-Oblique"/>
         </textElement>

         <text><![CDATA[Welcome to TutorialsPoint!]]></text>
      </staticText>

      <staticText>
         <reportElement x = "160" y = "250" width = "390" height = "40"/>
         <textElement/>

         <text>
            <![CDATA[<staticText>
            <reportElement x = "0" y = "350" width = "150" height = "40"/>

            <textElement>
               <font fontName = "Monospaced" size = "12" isItalic = "true"
                  isUnderline = "true" pdfFontName = "Courier-Oblique"/>
            </textElement>

            <text>Welcome to TutorialsPoint!</text></staticText>]]>
         </text>
      </staticText>

      <staticText>
         <reportElement x = "0" y = "300" width = "150" height = "40"/>

         <textElement>
            <font fontName = "Monospaced" size = "12" isBold = "true"
               isStrikeThrough = "true" pdfFontName = "Courier-Bold"/>
         </textElement>
         <text><![CDATA[Welcome to TutorialsPoint!]]></text>
      </staticText>

      <staticText>
         <reportElement x = "160" y = "300" width = "390" height = "40"/>
         <textElement/>

         <text>
            <![CDATA[<staticText>
            <reportElement x = "0" y = "400" width = "150" height = "40"/>

            <textElement>
               <font fontName = "Monospaced" size = "12" isBold = "true"
                  isStrikeThrough = "true" pdfFontName = "Courier-Bold"/>
            </textElement>

            <text>Welcome to TutorialsPoint!</text></staticText>]]>
         </text>
      </staticText>

      <staticText>
         <reportElement x = "0" y = "350" width = "150" height = "40"
            forecolor = "#FF0000"/>

         <textElement>
            <font size = "14"/>
         </textElement>

         <text><![CDATA[Welcome to TutorialsPoint!]]></text>
      </staticText>

      <staticText>
         <reportElement x = "160" y = "350" width = "390" height = "40"/>
         <textElement/>

         <text>
            <![CDATA[<staticText>
            <reportElement x = "0" y = "450" width = "150" height = "40"
               forecolor = "red"/>

            <textElement><font size = "14"/></textElement>
            <text>Welcome to TutorialsPoint!</text></staticText>]]>
         </text>
      </staticText>

      <staticText>
         <reportElement x = "0" y = "400" width = "150" height = "40" mode = "Opaque"
            forecolor = "#00FF00" backcolor = "#FFFF00"/>

         <textElement>
            <font fontName = "Serif" size = "12" isBold = "true"
               pdfFontName = "Times-Bold"/>
         </textElement>

         <text><![CDATA[Welcome to TutorialsPoint!]]></text>
      </staticText>

      <staticText>
         <reportElement x = "160" y = "400" width = "390" height = "40"/>
         <textElement/>

         <text>
            <![CDATA[<staticText>
            <reportElement x = "0" y = "500" width = "150" height = "40"
               forecolor = "green" backcolor = "#FFFF00" mode = "Opaque"/>

            <textElement>
               <font fontName = "Serif" size = "12" isBold = "true"
                  pdfFontName = "Times-Bold"/>
            </textElement>

            <text>Welcome to TutorialsPoint!</text></staticText>]]>
         </text>
      </staticText>

      <staticText>
         <reportElement x = "0" y = "450" width = "150" height = "40" mode = "Opaque"
            forecolor = "#0000FF" backcolor = "#FFDD99"/>

         <textElement textAlignment = "Center" verticalAlignment = "Middle">
            <font fontName = "SansSerif" size = "12" isBold = "false"
            isItalic = "true" pdfFontName = "Sans.Slanted" isPdfEmbedded = "true"/>
         </textElement>

         <text><![CDATA[Welcome to TutorialsPoint!]]></text>
      </staticText>

      <staticText>
         <reportElement x = "160" y = "450" width = "390" height = "40"/>
         <textElement/>

         <text>
            <![CDATA[<staticText>
            <reportElement x = "0" y = "550" width = "150" height = "90"
               forecolor = "blue" backcolor = "#FFDD99" mode = "Opaque"/>

            <textElement textAlignment = "Center" verticalAlignment = "Middle">
               <font fontName = "SansSerif" size = "12" isBold = "false"
                  pdfFontName = "Sans.Slanted" isPdfEmbedded = "true"/>
            </textElement>

            <text>Welcome to TutorialsPoint!</text></staticText>]]>
         </text>
      </staticText>

      <staticText>
         <reportElement mode = "Opaque" x = "0" y = "500" width = "150" height = "40"
            forecolor = "#FF0000" backcolor = "#99DDFF"/>

         <textElement textAlignment = "Right" verticalAlignment = "Bottom">
            <font fontName = "SansSerif" size = "12" isBold = "true"
               pdfFontName = "DejaVu Sans Bold" isPdfEmbedded = "true"/>
         </textElement>

         <text><![CDATA[Welcome to TutorialsPoint!]]></text>
      </staticText>

      <staticText>
         <reportElement x = "160" y = "500" width = "390" height = "40"/>
         <textElement/>

         <text>
            <![CDATA[<staticText>
            <reportElement x = "0" y = "650" width = "150" height = "90"    forecolor = "red"
               backcolor = "#99DDFF" mode = "Opaque"/>

            <textElement textAlignment = "Right" verticalAlignment = "Bottom">
               <font fontName = "SansSerif" size = "12" isBold = "true"
                  pdfFontName = "DejaVu Sans Bold" isPdfEmbedded = "true"/>
            </textElement>

            <text>Welcome to TutorialsPoint!</text></staticText>]]>
         </text>

      </staticText>

   </band>
</title>

</jasperReport>

填充并生成报表的 java 代码如下所示。让我们将此文件 JasperFontsReportFill.java 保存到 C:\tools\jasperreports-5.0.1\test\src\com\tutorialspoint 目录中。

The java code to fill and generate the report is as given below. Let’s save this file JasperFontsReportFill.java to C:\tools\jasperreports-5.0.1\test\src\com\tutorialspoint directory.

package com.tutorialspoint;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;

public class JasperFontsReportFill {
   public static void main(String[] args) {
      String sourceFileName = "C://tools/jasperreports-5.0.1/test/" +
         "jasper_report_template.jasper";

      try {
         JasperFillManager.fillReportToFile(sourceFileName, null,
            new JREmptyDataSource());
      } catch (JRException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }

   }
}

在这里,我们在填充报表时使用 JREmptyDataSource 实例来模拟一个包含一条记录的数据源,但所有字段都为 null。

Here, we use an instance of JREmptyDataSource when filling reports to simulate a data source with one record in it, but with all the fields being null.

Report Generation

我们将使用常规 ANT 构建流程编译并执行上述文件。build.xml 文件(保存在 C:\tools\jasperreports-5.0.1\test 目录下)的内容如下。

We will compile and execute the above file using our regular ANT build process. The contents of the file build.xml (saved under directory C:\tools\jasperreports-5.0.1\test) are as given below.

导入文件 - baseBuild.xml 从第 Environment Setup 章节中选取,且应放置在与 build.xml 相同的目录中。

The import file - baseBuild.xml is picked from chapter Environment Setup and should be placed in the same directory as the build.xml.

<?xml version = "1.0" encoding = "UTF-8"?>
<project name = "JasperReportTest" default = "viewFillReport" basedir = ".">
   <import file = "baseBuild.xml" />

   <target name = "viewFillReport" depends = "compile,compilereportdesing,run"
      description = "Launches the report viewer to preview the report
      stored in the .JRprint file.">

      <java classname = "net.sf.jasperreports.view.JasperViewer" fork = "true">
         <arg value = "-F${file.name}.JRprint" />
         <classpath refid = "classpath" />
      </java>

   </target>

   <target name = "compilereportdesing" description = "Compiles the JXML file and
      produces the .jasper file.">

      <taskdef name = "jrc" classname = "net.sf.jasperreports.ant.JRAntCompileTask">
         <classpath refid = "classpath" />
      </taskdef>

      <jrc destdir = ".">
         <src>
            <fileset dir = ".">
               <include name = "*.jrxml" />
            </fileset>
         </src>
         <classpath refid = "classpath" />
      </jrc>

   </target>

</project>

接下来,让我们打开命令行窗口并转到放置 build.xml 的目录。最后,执行命令 ant -Dmain-class=com.tutorialspoint.JasperFontsReportFill (viewFullReport 为默认目标),如下所示 -

Next, let’s open command line window and go to the directory where build.xml is placed. Finally, execute the command ant -Dmain-class=com.tutorialspoint.JasperFontsReportFill (viewFullReport is the default target) as −

C:\tools\jasperreports-5.0.1\test>ant -Dmain-class=com.tutorialspoint.JasperFontsReportFill
Buildfile: C:\tools\jasperreports-5.0.1\test\build.xml

clean-sample:
   [delete] Deleting directory C:\tools\jasperreports-5.0.1\test\classes
   [delete] Deleting: C:\tools\jasperreports-5.0.1\test\jasper_report_template.jasper
   [delete] Deleting: C:\tools\jasperreports-5.0.1\test\jasper_report_template.jrprint

compile:
   [mkdir] Created dir: C:\tools\jasperreports-5.0.1\test\classes
   [javac] C:\tools\jasperreports-5.0.1\test\baseBuild.xml:28:
   warning: 'includeantruntime' was not set, defaulting to build.
   [javac] Compiling 5 source files to C:\tools\jasperreports-5.0.1\test\classes

compilereportdesing:
   [jrc] Compiling 1 report design files.
   [jrc] log4j:WARN No appenders could be found for logger
   (net.sf.jasperreports.engine.xml.JRXmlDigesterFactory).
   [jrc] log4j:WARN Please initialize the log4j system properly.
   [jrc] log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
   [jrc] File : C:\tools\jasperreports-5.0.1\test\jasper_report_template.jrxml ... OK.

run:
   [echo] Runnin class : com.tutorialspoint.JasperFontsReportFill
   [java] log4j:WARN No appenders could be found for logger
   (net.sf.jasperreports.extensions.ExtensionsEnvironment).
   [java] log4j:WARN Please initialize the log4j system properly.

viewFillReport:
   [java] log4j:WARN No appenders could be found for logger
   (net.sf.jasperreports.extensions.ExtensionsEnvironment).
   [java] log4j:WARN Please initialize the log4j system properly.

BUILD SUCCESSFUL
Total time: 45 minutes 3 seconds

作为上述编译的结果,一个 JasperViewer 窗口会打开,如以下给出的屏幕截图所示 -

As a result of above compilation, a JasperViewer window opens up as shown in the screen given below −

report fonts example

在这里,我们可以看到文本“欢迎来到 TutorialsPoint”以不同的字体格式显示。

Here, we can see that the text "Welcome to TutorialsPoint" is displayed in different font formats.