`
alanland
  • 浏览: 641497 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

JasperReport默认字体

阅读更多

You can try to use net.sf.jasperreports.default.font.name property.

Or you can use the default style.

<style name="Base" isDefault="true" ...>

For more details you can read this article, the Default Fonts and Inheritance section.

UPDATED:

The quote from JasperReports Ultimate Guide:

We strongly encourage people to use in their reports only fonts that are provided in font extensions. This is the only way to be sure that the fonts will be available to the application when the reports are executed at runtime. Using system fonts always brings the risk that the reports do not work properly when deployed on a machine that does not have those fonts installed.

http://stackoverflow.com/questions/8080444/jasperreports-default-font

----------

Fonts Documented by Sanda Zaharia
 
  Description / Goal   How to format texts using fonts properties.
  Since   0.1.0
 
  Characters and Fonts

Any text is represented by a character sequence with particular formatting settings. Some of these appearance settings can be specified using the <font/> tag available in the <textElement/> tag. If there are shared font settings among several text elements, we strongly encourage people to group them in report styles defined in style templates (see the Templates sample).

Note: Report fonts are now deprecated. Do not use <reportFont/> elements declared within the document itself. Use the <style/> instead.

The main font settings available in JasperReports are:
  • fontName - the font name, which can be the name of a physical font or a logical one.
  • size - the size of the font measured in points. It defaults to 10.
  • isBold - flag specifying if a bold font is required. It defaults to false.
  • isItalic - flag specifying if an italic font is required. It defaults to false.
  • isUnderline - flag specifying if the underline text decoration is required. It defaults to false.
  • isStrikeThrough - flag specifying if the strikethrough text decoration is required. It defaults to false.
  • pdfFontName - the name of an equivalent PDF font required by the iText library when exporting documents to PDF format.
  • pdfEncoding - the equivalent PDF character encoding, also required by the iText library.
  • isPdfEmbedded - flag that specifies whether the font should be embedded into the document itself. It defaults to false.
Character Encoding

Another important feature to consider when working with texts, especially if they are intended to be internationalized, is the character encoding. That's because different charsets provide their own character representation for the same character code.
The default document encoding value is UTF-8.
For more information about how to set the character encoding, please consult the Unicode sample.

Default Fonts and Inheritance

Another interesting feature is that each text element inherits font and style attributes from its parent element. And each parent element inherits these attributes from its parent, etc. If no styles and/or fonts are defined for elements, the default style (and/or font - but this is now deprecated) declared in the <jasperReport/> root element will be applied.
A default style is characterized by the isDefault flag attribute:
  <style name="Base" isDefault="true" hAlign="Center" vAlign="Middle"
    fontSize="10" pdfFontName="Helvetica" pdfEncoding="Cp1252" isPdfEmbedded="false">
    <box padding="4">
      <pen lineWidth="0.5"/>
    </box>
  </style>
However, defining default styles or fonts in JasperReports is not mandatory. If no font is defined for a given element, the engine looks either for the inherited font attributes, or, if no attributes are found on this way, it looks for the

net.sf.jasperreports.default.font.name

property in the /src/default.jasperreports.properties file. Its value defines the name of the font family to be used when font properties are not explicitly defined for a text element or inherited from its parent.

The main default font properties and their values defined in the /src/default.jasperreports.properties file are:
  • 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
The bold, italic, and all text decorations properties are missing, which means that default fonts are not bold, not oblique and not decorated.

The Fonts Sample

This sample shows some practical examples of using fonts and font attributes in order to get a particular text appearance.
Because this sample uses a font extension based on the Gentium open source font files, and logical JVM font names also, it is strongly recommended to consult first the Font Extensions section below, and then to compile and run the sample.

In the example below, a series of font attributes are defined for the static text element:
  <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>The quick brown fox jumps over the lazy dog.</text>
  </staticText>
One can say that this text will use a monospaced character set, 12 pts sized, underlined and oblique, and when exporting to PDF format, the equivalent fonts will be Courier-Oblique.

Running the Sample

Running the sample requires the Apache Ant library. Make sure that ant is already installed on your system (version 1.5 or later).
In a command prompt/terminal window set the current folder to demo/samples/fonts within the JasperReports source project and run the > ant test view command.
It will generate all supported document types containing the sample report in the demo/samples/fonts/build/reports directory. A font extension sample xml file named fonts.xml will be also generated in the same directory. It contains all font families available in the already installed font extensions.
Then the report will open in the JasperReports internal viewer.
 
top

Font Extensions Documented by Sanda Zaharia
 
  Description / Goal   How to ship the required fonts with your report templates when deploying them in the target application, using font extensions.
  Since   3.1.3
  Other Samples  
/demo/samples/unicode
 
  About Fonts Extension

Formerly used font definitions relied on font files available on the machine. In this case, when defining how a piece of text should look like, one had to take care about the following possible issues:
  • The needed font library might not be available to the JVM at runtime because the font file is not installed on the system.
  • When a font library is not available, the local JVM will replace it with some default fonts, and this could lead to various side effects, such as totally different text appearance or truncated pieces of text.
It's obviously that running a report in this kind of approach becomes completely dependent on the local environment, and one have to ensure that required font files are installed on the machine where the report is run. If they aren't, they should be installed first. And that's what should be done on every machine running the report. Quite a little bit embarrassing, isn't it.
Therefore, this is not the best way to control fonts in JasperReports. A much better one is due to the extension points support, available in JasperReports. Font files can be provided as library extensions. In a very simple manner, making a font extension consists in putting various True Type Font files in a JAR file together with a properties file describing the content of the JAR, and an XML file defining relationships between fonts and locales.

Font Extensions Step By Step

Let's take a look into the /demo/fonts directory. It contains the DejaVu font extension available as default font for all samples shipped with the JasperReports project distribution package.
As known from extensions support, any JasperReports extension provides a jasperreports_extension.properties file in its root package, required by the JasperReports extension mechanism. This file is used to describe the content of the extension JAR file and consists in the following lines:

net.sf.jasperreports.extension.registry.factory.simple.font.families=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.dejavu=net/sf/jasperreports/fonts/fonts.xml


The SimpleFontExtensionsRegistryFactory class represents an implementation of the ExtensionsRegistryFactory interface, used to instantiate a font extension registry.
The extension registry obtained from the factory is able to return a list of actual extension point implementations based on extension point class type.
The second line provides the path to the XML file describing the actual font extension. The XML file in this case is named fonts.xml.

The main unit in the fonts.xml file is the <fontFamily/> element. A font family is an object instance which extends the FontFamily public interface. This is the point where font extensions can be connected with the JasperReports engine.
Font families described in the fonts.xml file consist in up to 4 font faces: normal, bold, italic and bolditalic. A font face is described by the FontFace interface.

In order to completely describe a font family extension, one have to indicate the mapping between the font faces and font files, the pdfEncoding and pdfEmbedded attributes, equivalent font face names available for dedicated exporters, such as the HTML exporter) and a list of supported locales, because font files usually support only certain languages.

The fonts.xml file includes 3 different font families:
  • DejaVu Sans
  • DejaVu Serif
  • DejaVu Sans Mono
and some mappings for logical JVM fonts available for HTML exporters:
  • SansSerif
  • Serif
  • Monospaced

Let's take a look at the DejaVu Sans family. All the font family settings already discussed can be found in the example below:
  <fontFamily name="DejaVu Sans">
    <normal>net/sf/jasperreports/fonts/dejavu/DejaVuSans.ttf</normal>
    <bold>net/sf/jasperreports/fonts/dejavu/DejaVuSans-Bold.ttf</bold>
    <italic>net/sf/jasperreports/fonts/dejavu/DejaVuSans-Oblique.ttf</italic>
    <boldItalic>net/sf/jasperreports/fonts/dejavu/DejaVuSans-BoldOblique.ttf</boldItalic>
    <pdfEncoding>Identity-H</pdfEncoding>
    <pdfEmbedded>true</pdfEmbedded>
    <exportFonts>
      <export key="net.sf.jasperreports.html">'DejaVu Sans', Arial, Helvetica, sans-serif</export>
      <export key="net.sf.jasperreports.xhtml">'DejaVu Sans', Arial, Helvetica, sans-serif</export>
    </exportFonts>
    <!--
    <locales>
      <locale>en_US</locale>
      <locale>de_DE</locale>
    </locales>
    -->
  </fontFamily>
The name attribute and the <normal/> font face represent required elements in a font family definition, while all the others are optional.
The name of the font family will be used as the fontName attribute of the text element or style in the report template. The fontName together with the isBold and isItalic attributes of the text field or style in the report help to locate and load the appropriate font face from the family. If a particular font face is not present or declared in the family, then the normal font face will be used instead.

In the example above we can see the mappings for the bold, italic and bolditalic font styles are also present. The <exportFonts/> tag instructs the HTML exporters to correlate this font family with other HTML supported font families, such as Arial, Helvetica, sans-serif.

The <locales/> contains a list of supported locales. This block being commented, the engine will try to apply this font family for any locale, without taking into account if the font file really provides support for that locale. If a particular locale is not supported, errors might occur at runtime and characters might be misrepresented.
However, if a given font family needs to be represented for locales supported by different font files, one can define separate font families in the XML file, having the same name but with differing <locales/> tag. This feature is very useful when the same report has to be run in both Japanese and Chinese, because there is no TTF file that simultaneously supports these two languages.

The <pdfEncoding/> and <pdfEmbedded/> are used to specify the PDF encoding attribute and the PDF embedding flag, and people are strongly encouraged to use them instead of deprecated pdfEncoding and pdfEmbedded attributes available in the JRXML <font/> tag.

Now, let's take a look at logical JVM fonts mappings:
  <fontFamily name="SansSerif">
    <exportFonts>
      <export key="net.sf.jasperreports.html">'DejaVu Sans', Arial, Helvetica, sans-serif</export>
      <export key="net.sf.jasperreports.xhtml">'DejaVu Sans', Arial, Helvetica, sans-serif</export>
    </exportFonts>
  </fontFamily>
  <fontFamily name="Serif">
    <exportFonts>
      <export key="net.sf.jasperreports.html">'DejaVu Serif', 'Times New Roman', Times, serif</export>
      <export key="net.sf.jasperreports.xhtml">'DejaVu Serif', 'Times New Roman', Times, serif</export>
    </exportFonts>
  </fontFamily>
  <fontFamily name="Monospaced">
    <exportFonts>
      <export key="net.sf.jasperreports.html">'DejaVu Sans Mono', 'Courier New', Courier, monospace</export>
      <export key="net.sf.jasperreports.xhtml">'DejaVu Sans Mono', 'Courier New', Courier, monospace</export>
    </exportFonts>
  </fontFamily>
Here the DejaVu font families are added to the font families list available for HTML at export time.

Once you have the TTF files, the jasperreports_extension.properties and fonts.xml files, you can pack them together in a JAR file, and then put the JAR in your application's classpath, in order to make the new fonts available to your reports, wherever the application might run.

For more details about deploying fonts as extensions, you can take a look at the Fonts sample provided with the JasperReports project distribution package, which adds one more font extension for another open source font called Gentium. Running the sample using the > ant clean javac compile fontsXml command will generate in the demo/samples/fonts/build/reports a font extension xml file named fonts.xml. This file contains all font families available in the already installed font extensions.

 

 

http://jasperreports.sourceforge.net/sample.reference/fonts/index.html

 

分享到:
评论

相关推荐

    jasperReport中文字体和配置.rar

    - **中文字体**:JasperReport默认使用的字体可能不包含中文字符,因此需要引入支持中文的字体库。常用的中文字体有宋体、黑体、微软雅黑等。将这些字体添加到项目中,然后在jrxml文件中通过`&lt;font&gt;`元素指定。例如...

    jasperreport 【中文字体依赖】jars

    2. 然后,在JasperReport的设计文件(jrxml)中,你需要设置报表的默认字体为“微软雅黑”或者其他支持中文的字体。这可以通过修改`&lt;style&gt;`元素的`fontName`属性来实现。 3. 在Java代码中,确保正确设置了...

    jasperReport 亚洲语言包

    然而,对于包含亚洲语言(如中文、日文、韩文等)的报告,jasperReport默认的支持可能不足,因此需要额外的组件来确保正确显示这些字符。描述中的“jasperReport pdf中文支持所需要的包”正是为了解决这个问题,它...

    JasperReport所需要的jar文件

    3. **jasperreports-fonts.jar**:这个`jar`文件包含了`JasperReport`使用的默认字体和符号,确保报表在不同平台上的一致显示。 4. **jcommon.jar**:`JCommon`库提供了一些通用的图形和数据处理工具,包括图表绘制...

    JasperReport初级教程(2.0.3)图文详解

    - **样式设置**:通过右侧的属性面板调整字体大小、颜色等样式。 - **预览**:设计完成后,可以点击预览按钮查看报表效果。 #### 五、小结 本教程详细介绍了如何使用JasperReport 2.0.3版本创建报表模板的基本流程...

    JasperReport+iReport 报表工具详细开发手册

    例如,可以通过设置字体大小、颜色等属性来自定义文本样式;通过设置列宽、单元格合并等方式来优化表格的展示效果。 **3.2. JasperReport入门** **3.2.1. 在WEB中显示报表** 在Web环境中显示JasperReport生成的...

    JasperReport6.7.0必需的lib

    在某些情况下,系统默认的字体可能不足以满足设计的需求,或者需要特殊字符集来显示非英文内容。将字体文件添加到项目中并配置`jasperreports_extension.properties`,可以让报表在渲染时正确地显示这些字体。 在...

    iReport 和 Jasperreport整合开发web报表向导2.doc

    若要使用非默认字体(如黑体、楷体),需要 iTextAsian.jar 支持。在 "Report fonts" 中设置自定义字体,例如设置宋体,之后可在报表元素中引用该字体。 8. **报表集成**: 完成报表设计后,可以通过 ...

    JasperReport+IReport开发Java报表入门级教程.pdf

    首次启动 iReport 时,默认为英文界面。通过菜单栏的【Tools】-【Options】选项可以配置语言和报表输出路径。选择中文界面后,可以设置报表的输出路径,以便于管理和查找。 - **语言**: 在 Language 选项卡中选择 ...

    JasperReport中pdf显示中文最新jar

    在默认情况下,JasperReport使用的是iText库来生成PDF文档,但早期版本的iText对中文字符的支持并不完善,这就会导致在PDF中显示中文时出现乱码或无法显示的情况。 为了解决这个问题,我们需要引入两个特定的jar包...

    JasperReport 与iReport 的配置与使用

    按照下载的安装程序进行安装,通常只需要遵循默认的安装向导即可完成安装过程。 **2.1.3 JDK 的配置** 安装完成后,还需要配置环境变量。这主要包括设置 JAVA_HOME 变量指向 JDK 的安装目录,以及将 JDK 的 bin ...

    解决jasperReports中文无法显示问题.rar

    1. **字体替换**:jasperReports在默认情况下可能不支持某些中文字体,导致中文字符无法正确显示。华文宋体是一种常见的中文TrueType字体,适用于大多数中文环境。因此,解决方案是将报表模板中的字体设置更改为华文...

    iReport 和 Jasperreport整合开发web报表向3.doc

    对于非系统默认字体,需要将其ttf文件打包到应用中,并使用相对路径引用。 5. **编译和输出格式设置**: iReport的`.jrxml`文件需要编译成`.jasper`文件才能由JasperReport引擎处理。点击“编译”按钮即可完成此...

    struts2+jasperReport+ireport做报表总结

    Struts2、JasperReport 和 iReport 是开发Web报表应用中的常用技术栈。本文主要针对使用Struts2框架结合JasperReport5.0与iReport5.0进行报表开发时可能遇到的问题进行总结。 1. **报表预览问题**: - 数据源为空...

    IReport与JasperReport开发报表经验总结

    文档中提到的一个具体案例是:当不显示数据时,默认情况为“All sections, no detail”。这通常是由于设计阶段未正确配置报表模板所致。解决这类问题的关键在于调整报表的设计逻辑,确保即使在没有数据的情况下也能...

    Jasperreports官网包加宋体,jar引入项目即可

    首先,我们需要理解的是,JasperReports默认支持的字体可能不包含中文字符,因此在处理中文报表时,可能会出现乱码问题。为解决这个问题,我们需要自定义字体,并将其集成到项目中。在提供的压缩包文件中,有两个...

    Ireport and JasperReport使用说明.doc

    安装过程比较简单,一般情况下默认安装即可。 - **下载地址**: [http://www.oracle.com/technetwork/java/javase/downloads/index.html](http://www.oracle.com/technetwork/java/javase/downloads/index.html) ...

    报表制作的详细流程(iReport+jasperReports)

    2. **JDK安装**:按照下载后的安装向导步骤进行安装,通常默认设置即可。 3. **JDK配置**:安装完成后,需要在系统环境变量中设置`JAVA_HOME`指向JDK的安装目录,并添加`%JAVA_HOME%\bin`到`PATH`变量,确保系统...

Global site tag (gtag.js) - Google Analytics