Jasper Reports 简明教程
Report Fonts
报表含文本元素,而这些元素各自可具有其单独的字体设定,可使用 <textElement> 标签内的 <*font*> 标签来指定这些设定,报表能够定义许多字体,一旦定义后,它们可作为整个报表其他字体定义的默认字体或基本字体设定来使用。
Report Fonts
报表字体是字体设定的集合,在报表级别声明,在设置文本元素的字体属性时,报表字体可以在整个报表模板中重复使用。
报表字体现已被弃用,请勿使用在文档自身中声明的 <reportFont/> 元素,而应改用 <style/> 元素。
Font Attributes
下表概述了 <*font*> 元素的主要属性−
S.NO |
Attribute and Description |
1 |
fontName 字体名称,可以是物理字体、逻辑字体或者来自已注册的 JasperReports 字体扩展的字体家族的名称。 |
2 |
size 以磅为单位度量的字体大小,其默认为 10。 |
3 |
isBold 指定是否需要粗体字的标志,其默认为 false。 |
4 |
isItalic 指定是否需要斜体字的标志,其默认为 false。 |
5 |
isUnderline 指定是否需要下划线文字修饰的标志,其默认为 false。 |
6 |
isStrikeThrough 指定是否需要删除线文字修饰的标志,其默认为 false。 |
7 |
pdfFontName 将文档导出为 PDF 格式时,iText 库所需的同等 PDF 字体的名称。 |
8 |
pdfEncoding 同等的 PDF 字符编码,iText 库也需要它。 |
9 |
isPdfEmbedded 指定是否将字体嵌入文档本身的标志。其默认为 false。如果设置为 true,则有助于毫无问题地查看 PDF 文档。 |
Font Types
在 JasperReports 中,字体可分类为:
-
Logical Fonts −自 Java 平台 1.0 版本以来就已获得认可的五种字体类型称为逻辑字体。这些字体为: Serif, SansSerif, Monospaced, Dialog, and DialogInput 。这些逻辑字体不是安装在系统上的实际字体库。它们只是 Java 运行时认可的字体类型名称。这些字体必须映射到安装在系统上的某些物理字体。
-
Physical Fonts −这些字体是实际字体库,例如,由 TrueType 或 PostScript 1 型字体组成。物理字体可能是 Arial、Time、Helvetica、Courier 或任何数量的其他字体,包括国际字体。
-
Font Extensions −借助于对字体扩展的内置支持,JasperReports 库可以在运行时利用通过动态注册的字体。可以使用字体扩展,向 JasperReports 提供字体系列列表。这些列表由相似外观的字体效果和支持特定的区域构成。
如上表所述,我们需要在 fontName 属性中指定物理字体的名称、逻辑字体的名称或注册的 JasperReports 字体扩展中字体系列的名称。
PDF Font Name
当导出报表为 PDF(便携式文档格式)时,JasperReports 库使用 iText 库。可在各种平台上查看 PDF 文件,并且始终外观相同。这部分是因为在此格式中,有一种处理字体的特殊方式。向 PDF 导出时,fontName 属性没有任何作用。存在 pdfFontName 属性,我们需要在此属性中指定字体设置。
iText 库知道如何处理内置字体和 TTF 文件,并且识别以下内置字体名称:
-
Courier
-
Courier-Bold
-
Courier-BoldOblique
-
Courier-Oblique
-
Helvetica
-
Helvetica-Bold
-
Helvetica-BoldOblique
-
Helvetica-Oblique
-
Symbol
-
Times-Roman
-
Times-Bold
-
Times-BoldItalic
-
Times-Italic
-
ZapfDingbats
根据 iText 库的先决条件,要使用字体,我们需要将以下内容之一指定为字体名称:
-
从上述列表中指定一个内置字体名称。
-
一个 TTF(True Type 字体)文件的名称,它可在磁盘上进行定位。
-
字体的真实名称,前提是包含该字体的 TTF 文件已经向 iText 注册,或者在注册字体时定义了别名。
基于上述先决条件,pdfFontName 属性可包含以下值之一:
-
以上列表中某个内置 PDF 字体的名称。
-
导出到 PDF 时可在运行时位于磁盘上的 TTF 文件的名称。
-
已注册字体的实际名称。
-
对于 iText 作为字体文件注册的字体的键的后缀(net.sf.jasperreports.export.pdf.font 之后的)。
Default Fonts and Inheritance
每个文本元素从其父元素继承字体和样式属性,而其父元素反过来又从其父元素继承这些属性。如果未为元素定义样式和/或字体,那么将在 <jasperReport/> 根元素中声明的默认样式(和/或字体,但现在这不赞扬)被应用。
在 JasperReports 中定义默认样式或字体不是强制性的。如果没有为给定元素定义字体,引擎会查找继承的字体属性,或者如果没有通过这种方式找到属性,则会在 /src/default.jasperreports.properties 文件中查找 net.sf.jasperreports.default.font.name 属性。其值定义了在未为文本元素显式定义字体属性或从其父元素继承字体属性时要使用的字体系列名称。
在 /src/default.jasperreports.properties 文件中定义的主默认字体属性及其值在下表中 -
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 |
默认 PDF 字符编码。 |
net.sf.jasperreports.default.pdf.embedded=false |
默认情况下不嵌入 PDF 字体。 |
Example
为了演示如何使用字体和字体属性以获得特定文本外观,我们编写新的报表模板 (jasper_report_template.jrxml)。JRXML 的内容如下。将其保存到 C:\tools\jasperreports-5.0.1\test 目录中。在这里,我们在报表的标题中以各种字体格式显示文本。
<?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 目录中。
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。
Report Generation
我们将使用常规 ANT 构建流程编译并执行上述文件。build.xml 文件(保存在 C:\tools\jasperreports-5.0.1\test 目录下)的内容如下。
导入文件 - baseBuild.xml 从第 Environment Setup 章节中选取,且应放置在与 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 为默认目标),如下所示 -
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 窗口会打开,如以下给出的屏幕截图所示 -
在这里,我们可以看到文本“欢迎来到 TutorialsPoint”以不同的字体格式显示。