报告字体

报告包含文本元素,每个元素都可以有自己的字体设置.可以使用< font >指定这些设置. < textElement>中可用的标记标签.报告可以定义多种字体.一旦定义,它们可以用作整个报告中其他字体定义的默认或基本字体设置.

报告字体

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

现在不推荐使用报表字体.不要使用< reportFont/>在文档本身内声明的元素.使用< style/>元素代替.

字体属性

下表总结了< font >的主要属性.元素 :

S.NO属性和描述
1

fontName

字体名称,可以是物理字体的名称,逻辑字体或名称字体系列来自注册的JasperReports字体扩展名.

2

尺寸

以磅为单位测量的字体大小.它默认为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文档而不会出现任何问题.

字体类型

在JasperReports中,字体可以归类为 :

  • 逻辑字体 : 从版本1.0开始,Java平台已经识别出五种字体类型,称为逻辑字体.这些是&减去; Serif,SansSerif,Monospaced,Dialog和DialogInput .这些逻辑字体不是安装在系统上任何位置的实际字体库.它们只是Java运行时识别的字体类型名称.这些必须映射到系统上安装的某些物理字体.

  • 物理字体 : 这些字体是实际的字体库,例如,由TrueType或PostScript Type 1字体组成.物理字体可以是Arial,Time,Helvetica,Courier或任何其他字体,包括国际字体.

  • 字体扩展 :  JasperReports库可以通过其对字体扩展的内置支持,在运行时使用在运行中注册的字体.可以使用字体扩展名为JasperReports提供字体系列列表.这些是由看起来相似的字体和支持特定的区域设置.

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

PDF字体名称

将报表导出为PDF(可移植文档格式)时,JasperReports库使用iText库. PDF文件可以在各种平台上查看,并且看起来总是一样.这部分是因为在这种格式中,有一种处理字体的特殊方法.导出为PDF时, fontName 属性无用.属性 pdfFontName 存在于我们需要指定字体设置的位置.

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

  • Courier

  • Courier-Bold

  • Courier-BoldOblique

  • Courier-Oblique

  • Helvetica

  • Helvetica-Bold

  • Helvetica-BoldOblique

  • Helvetica-Oblique

  • 符号

  • 时报-Roman

  • Times-Bold

  • Times-BoldItalic

  • Times-Italic

  • ZapfDingbats

根据iText库的先决条件,要使用字体,我们需要指定以下其中一项作为字体名称 :

  • 上面列表中的内置字体名称.

  • TTF(True Type Font)文件的名称,它可以在磁盘上找到.

  • 字体的真实名称,省假设包含该字体的TTF文件先前已在iText中注册,或者在注册字体时定义了别名.

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

  • 上面列表中内置PDF字体的名称.

  • 导出时可以在运行时位于磁盘上的TTF文件的名称PDF格式.

  • 注册字体的真实姓名.

  • 后缀对于使用iText作为字体文件注册的字体的键( net.sf.jasperreports.export.pdf.font 之后的部分).

默认字体和继承

每个文本元素都从其父元素继承字体和样式属性,而父元素又从父元素继承这些属性.如果没有为元素定义样式和/或字体,则在< jasperReport/>中声明的默认样式(和/或字体 - 但现在已弃用)将应用根元素.

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

主要的默认字体属性及其定义的值/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

示例

要演示使用字体和字体属性以获得特定的文本外观,让我们编写新的报告模板(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\it1352

 
 package com.it1352; 
 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 .

报告生成

我们将使用常规的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 = com.it1352.JasperFontsReportFill (viewFullReport是默认目标)为 :

C:\tools\jasperreports-5.0.1\test>ant -Dmain-class=com.IT屋.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.IT屋.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窗口打开up如下面给出的屏幕所示 :

Jasper报告字体示例

在这里,我们可以看到文本"Welcome to TutorialsPoint"以不同的字体格式显示.