Fonts
报告包含文本元素,每个元素都可以有自己的字体设置。 可以使用“textElement”标记中提供的< font “标记指定这些设置。 报告可以定义许多字体。 定义后,它们可以用作整个报表中其他字体定义的默认或基本字体设置。
报告字体
报告字体是在报告级别声明的字体设置的集合。 在设置文本元素的字体属性时,可以在整个报表模板中重用报表字体。
报告字体现已弃用。 不要使用文档本身声明的元素。 请改用元素。
字体属性
下表总结了< font “元素的主要属性 -
S.NO | 属性和描述 |
---|---|
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 iText库也需要等效的PDF字符编码。 |
9 | isPdfEmbedded 指定字体是否应嵌入文档本身的标志。 它默认为false。 如果设置为true,则有助于查看PDF文档而不会出现任何问题。 |
字体类型
在JasperReports中,字体可以归类为 -
Logical Fonts - 自1.0版以来Java平台已识别的五种字体类型称为逻辑字体。 它们是 - Serif, SansSerif, Monospaced, Dialog, and DialogInput 。 这些逻辑字体不是安装在系统上任何位置的实际字体库。 它们只是Java运行时识别的字体类型名称。 这些必须映射到系统上安装的某些物理字体。
Physical Fonts - 这些字体是实际的字体库,例如,由TrueType或PostScript Type 1字体组成。 物理字体可以是Arial,Time,Helvetica,Courier或任何其他字体,包括国际字体。
Font Extensions - JasperReports库可以通过其对字体扩展的内置支持,在运行时使用在运行中注册的字体。 可以使用字体扩展名为JasperReports提供字体系列列表。 这些是由类似的字体面和支持特定的区域设置。
如上表所述,我们需要在属性fontName指定物理字体的名称,逻辑字体的名称或注册的JasperReports字体扩展名中的字体系列的名称。
PDF字体名称
在将报告导出为PDF(可移植文档格式)时,JasperReports库使用iText库。 PDF文件可以在各种平台上查看,并且看起来总是一样。 这部分是因为在这种格式中,有一种处理字体的特殊方法。 导出为PDF时, fontName属性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 Font)文件的名称,它可以在磁盘上找到。
字体的真实名称,前提是包含字体的TTF文件先前已使用iText注册,或者在注册字体时定义了别名。
根据上述先决条件, pdfFontName属性可以包含以下值之一 -
上面列表中内置PDF字体的名称。
导出为PDF时可在运行时位于磁盘上的TTF文件的名称。
注册字体的真实姓名。
对于使用iText注册为字体文件的字体,键的后缀( net.sf.jasperreports.export.pdf.font之后的部分)。
默认字体和继承
每个文本元素都从其父元素继承字体和样式属性,而父元素又从其父元素继承这些属性。 如果没有为元素定义样式和/或字体,则将应用在根元素中声明的默认样式(和/或字体 - 但现在已弃用)。
在JasperReports中定义默认样式或字体不是必需的。 如果没有为给定元素定义字体,引擎会查找继承的字体属性,或者,如果没有找到这种方式的属性,它会在/src/default.jasperreports.properties查找net.sf.jasperreports.default.font.name属性。 /src/default.jasperreports.properties文件。 其值定义在未为文本元素显式定义字体属性或从其父元素继承字体属性时要使用的字体系列的名称。
/src/default.jasperreports.properties文件中定义的主要默认字体属性及其值位于下表中 -
属性 | 描述 |
---|---|
net.sf.jasperreports.default.font.name=SansSerif | 默认字体名称。 |
net.sf.jasperreports.default.font.size=10 | 默认字体大小。 |
net.sf.jasperreports.default.pdf.font.name=Helvetica | 默认的PDF字体。 |
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 xnip!]]>
</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 xnip!</text></staticText>]]>
</text>
</staticText>
<staticText>
<reportElement x = "0" y = "100" width = "150" height = "40"/>
<textElement>
<font size = "12"/>
</textElement>
<text><![CDATA[Welcome to xnip!]]></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 xnip!</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 xnip!]]></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 xnip!</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 xnip!]]></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 xnip!</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 xnip!]]></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 xnip!</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 xnip!]]></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 xnip!</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 xnip!]]></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 xnip!</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 xnip!]]></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 xnip!</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 xnip!]]></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 xnip!</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 xnip!]]></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 xnip!</text></staticText>]]>
</text>
</staticText>
</band>
</title>
</jasperReport>
填写和生成报告的java代码如下所示。 让我们将此文件JasperFontsReportFill.java保存到C:\tools\jasperreports-5.0.1\test\src\com\xnip目录。
package cn.xnip;
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是从环境设置一章中挑选出来的,应该与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=cn.xnip.JasperFontsReportFill (viewFullReport是默认目标)为 -
C:\tools\jasperreports-5.0.1\test>ant -Dmain-class=cn.xnip.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 : cn.xnip.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窗口打开,如下面的屏幕所示 -

在这里,我们可以看到文本“Welcome to xnip”以不同的字体格式显示。