報表字型



報表包含文字元素,並且每個文字元素都可以擁有自己的字型設定。這些設定可以使用<font>標籤在<textElement>標籤中指定。報表可以定義多個字型。一旦定義,它們就可以用作整個報表中其他字型定義的預設或基本字型設定。

報表字型

報表字型是在報表級別宣告的一組字型設定。在設定文字元素的字型屬性時,可以在整個報表模板中重複使用報表字型。

報表字型現已棄用。不要使用在文件本身中宣告的<reportFont/>元素。請改用<style/>元素。

字型屬性

下表總結了<font>元素的主要屬性:

序號 屬性和描述
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 文件。

字型型別

在 JasperReports 中,字型可以分類為:

  • 邏輯字型 - 自 Java 平臺 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 檔案可以在各種平臺上檢視,並且外觀始終相同。這部分是因為在此格式中,有一種特殊的方法來處理字型。fontName 屬性在匯出到 PDF 時不起作用。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 字型名稱。

  • TTF 檔案的名稱,該檔案在匯出到 PDF 時可以在執行時在磁碟上找到。

  • 已註冊字型的真實名稱。

  • 已在 iText 中註冊為字型檔案的字型的鍵的字尾(net.sf.jasperreports.export.pdf.font 之後的部分)。

預設字型和繼承

每個文字元素都繼承其父元素的字型和樣式屬性,而父元素又繼承其父元素的這些屬性。如果未為元素定義任何樣式和/或字型,則將應用在<jasperReport/>根元素中宣告的預設樣式(和/或字型 - 但此功能現已棄用)。

在 JasperReports 中定義預設樣式或字型不是強制性的。如果未為給定元素定義字型,則引擎會查詢繼承的字型屬性,或者,如果未在此方式找到任何屬性,則會查詢/src/default.jasperreports.properties檔案中net.sf.jasperreports.default.font.name屬性。其值定義了在未為文字元素顯式定義字型屬性或從其父元素繼承時要使用的字體系列的名稱。

/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 字型不會嵌入。

示例

為了演示如何使用字型和字型屬性來獲得特定的文字外觀,讓我們編寫新的報表模板 (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

報表生成

我們將使用常規的 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.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 視窗,如下面的螢幕截圖所示:

Jasper Report Fonts Example

在這裡,我們可以看到文字“歡迎來到 TutorialsPoint”以不同的字型格式顯示。

廣告