Jasper-reports-jasper-report-fonts

提供:Dev Guides
移動先:案内検索

レポートフォント

レポートにはテキスト要素が含まれ、それぞれに独自のフォント設定を含めることができます。 これらの設定は、<textElement>タグで使用可能な<* font *>タグを使用して指定できます。 レポートでは、多数のフォントを定義できます。 定義したら、レポート全体で他のフォント定義のデフォルトまたは基本フォント設定として使用できます。

レポートフォント

レポートフォントは、レポートレベルで宣言されたフォント設定のコレクションです。 テキスト要素のフォントプロパティを設定する場合、レポートテンプレート全体でレポートフォントを再利用できます。

レポートフォントは廃止されました。 ドキュメント自体で宣言された<reportFont/>要素を使用しないでください。 代わりに<style/>要素を使用してください。

フォント属性

以下の表は、<* 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ドキュメントを問題なく表示できます。

フォントの種類

JasperReportsでは、フォントは次のように分類できます-

  • 論理フォント-バージョン1.0以降、Javaプラットフォームで認識されている5つのフォントタイプは、論理フォントと呼ばれます。 これらは- Serif、SansSerif、Monospaced、Dialog、およびDialogInput です。 これらの論理フォントは、システムのどこにでもインストールされる実際のフォントライブラリではありません。 これらは、Javaランタイムによって認識される単なるフォントタイプ名です。 これらは、システムにインストールされている物理フォントにマッピングする必要があります。
  • 物理フォント-これらのフォントは、たとえばTrueTypeまたはPostScript Type 1フォントで構成される実際のフォントライブラリです。 物理フォントは、Arial、Time、Helvetica、Courier、または国際フォントを含む他の任意のフォントです。
  • フォント拡張機能-JasperReportsライブラリは、フォント拡張機能の組み込みサポートにより、実行時にオンザフライで登録されたフォントを使用できます。 フォントファミリのリストは、フォント拡張を使用してJasperReportsで利用可能にできます。 これらは、似たような外観のフォントフェースで構成され、特定のロケールをサポートします。

上記の表で説明したように、属性_fontName_で、物理フォントの名前、論理フォントの名前、または登録済みのJasperReportsフォント拡張機能からのフォントファミリの名前を指定する必要があります。

PDFフォント名

JasperReportsライブラリは、レポートをPDF(Portable Document Format)にエクスポートするときにiTextライブラリを使用します。 PDFファイルはさまざまなプラットフォームで表示でき、常に同じように見えます。 これは、この形式にはフォントを処理する特別な方法があるためです。 _fontName_属性は、PDFにエクスポートするときには役に立ちません。 フォント設定を指定する必要のある属性_pdfFontName_が存在します。

iTextライブラリは、組み込みフォントとTTFファイルを処理する方法を知っており、次の組み込みフォント名を認識します-

  • 宅配便
  • Courier-Bold
  • Courier-BoldOblique
  • Courier-Oblique
  • Helvetica
  • Helvetica-Bold
  • Helvetica-BoldOblique
  • Helvetica-Oblique
  • シンボル
  • タイムズ・ローマ
  • タイムズボールド
  • Times-BoldItalic
  • タイムズイタリック
  • ザップ

iTextライブラリの前提条件に従って、フォントを使用するには、フォント名として次のいずれかを指定する必要があります-

  • 上記のリストからの組み込みフォント名。
  • ディスク上にあるTTF(True Type Font)ファイルの名前。
  • フォントを含むTTFファイルが以前にiTextに登録されているか、フォントが登録されたときにエイリアスが定義されていれば、フォントの実際の名前。

上記の前提条件に基づいて、_pdfFontName_属性には次の値のいずれかを含めることができます-

  • 上記のリストからの組み込みPDFフォントの名前。
  • PDFへのエクスポート時に実行時にディスク上に配置できるTTFファイルの名前。
  • 登録済みフォントの本名。
  • フォントファイルとしてiTextに登録されているフォントのキーのサフィックス(_net.sf.jasperreports.export.pdf.font_の後の部分)。

デフォルトのフォントと継承

各テキスト要素は親要素からフォントおよびスタイル属性を継承し、親要素はこれらの属性を親から継承します。 要素にスタイルおよび/またはフォントが定義されていない場合、<jasperReport/>ルート要素で宣言されたデフォルトのスタイル(および/またはフォント-これは廃止されました)が適用されます。

JasperReportsでデフォルトのスタイルまたはフォントを定義することは必須ではありません。 特定の要素にフォントが定義されていない場合、エンジンは継承されたフォント属性を検索するか、この方法で属性が見つからない場合、_で_net.sf.jasperreports.default.font.name_プロパティを検索します/src/default.jasperreports.properties_ファイル。 その値は、フォントプロパティがテキスト要素に対して明示的に定義されていない場合、またはその親から継承されていない場合に使用されるフォントファミリの名前を定義します。

_/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 The default PDF character encoding.
net.sf.jasperreports.default.pdf.embedded=false By default PDF fonts are not embedded.

特定のテキストの外観を得るためにフォントとフォント属性を使用する方法を示すために、新しいレポートテンプレート(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 finddevguides!]]>
         </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 finddevguides!</text></staticText>]]>
         </text>
      </staticText>

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

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

         <text><![CDATA[Welcome to finddevguides!]]></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 finddevguides!</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 finddevguides!]]></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 finddevguides!</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 finddevguides!]]></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 finddevguides!</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 finddevguides!]]></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 finddevguides!</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 finddevguides!]]></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 finddevguides!</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 finddevguides!]]></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 finddevguides!</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 finddevguides!]]></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 finddevguides!</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 finddevguides!]]></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 finddevguides!</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 finddevguides!]]></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 finddevguides!</text></staticText>]]>
         </text>

      </staticText>

   </band>
</title>

</jasperReport>

レポートに入力して生成するJavaコードは以下のとおりです。 このファイル JasperFontsReportFill.java をC:\ tools \ jasperreports-5.0.1 \ test \ src \ com \ finddevguidesディレクトリに保存しましょう。

package com.finddevguides;

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_のインスタンスを使用して、1つのレコードを含むデータソースをシミュレートしますが、すべてのフィールドは_null_です。

レポート生成

通常のANTビルドプロセスを使用して、上記のファイルをコンパイルおよび実行します。 ファイルbuild.xmlの内容(ディレクトリC:\ tools \ jasperreports-5.0.1 \ testに保存)は以下のとおりです。

インポートファイル-baseBuild.xmlは、チャプターリンク:/jasper_reports/jasper_environment_setup [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.finddevguides.JasperFontsReportFill (viewFullReportがデフォルトのターゲットです)を-として実行します

C:\tools\jasperreports-5.0.1\test>ant -Dmain-class=com.finddevguides.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/faql#noconfig for more info.
   [jrc] File : C:\tools\jasperreports-5.0.1\test\jasper_report_template.jrxml ... OK.

run:
   [echo] Runnin class : com.finddevguides.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レポートフォントの例

ここでは、「Welcome to finddevguides」というテキストがさまざまなフォント形式で表示されていることがわかります。