我如何测试如果我的字体在pdf中正确显示? [英] How can I test if my font is rendered correctly in pdf?

查看:142
本文介绍了我如何测试如果我的字体在pdf中正确显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在jasper报告中使用不同的字体时,您需要使用 font-extensions 不能正确呈现的,有没有一种方法,我可以测试,如果该字体是由PDF支持,使我可以理解问题是否与我的font-extensions或我的 .ttf 字体有关?

当从jasper报告导出为PDF格式时,字体显示不正确是一个常见问题示例贾斯珀报告PDF不会导出西里尔值,如清单1所示,使用字体扩展并不总是足够的,字体也需要pdf生成库支持,并能够呈现实际的字符。这就是为什么我决定通过这个QA风格的问题,以便将来的用户打击清单1时可以有一个如何快速测试字体的参考。

由于jasper报告使用了> itext 库最简单的方法来测试你的字体是否将正确地在pdf中呈现是直接用itext进行测试。


$ b 示例 program * ,从 iText:第11章:选择正确的字体
$ b

  import java.io.FileOutputStream; 
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfWriter;

public class FontTest {

/ **生成的PDF文件。 * /
public static final String RESULT =fontTest.pdf;
/ **要呈现的文字。 * /
public static final String TEST =测试以土耳其里拉字符呈现此文本\\\₺;
$ b $ public void createPdf(String filename)throws IOException,DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(filename));
document.open();
BaseFont bf = BaseFont.createFont(
pathToMyFont / myFont.ttf,BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
Font font = new Font(bf,20);
ColumnText column = new ColumnText(writer.getDirectContent());
column.setSimpleColumn(36,730,569,36);
column.addElement(new Paragraph(TEST,font));
column.go();
document.close();


public static void main(String [] args)throws IOException,DocumentException {
new FontTest()。createPdf(RESULT);






一些注释(见例子):




  • 为了渲染特殊字符,使用它的编码值例如
    \\\₺ 来避免在您的文件中编码类型的问题。
  • 考虑到总是使用 Unicode 编码,在新的PDF标准中推荐使用
    (PDF / A ,PDF / UA),并为您提供了混合
    种不同编码类型的可能性,而且只有
    稍大的文件大小是不利的。

$ b

结论:
$ b


如果您的字体正确呈现在fontTest.pdf中,在jasper报告中,您的字体扩展名有
问题。 如果您的字体是在fontTest.pdf中没有正确显示,那么
在碧玉报告中没有任何可以做的事情,您需要找到另一种字体。






最新的jasper-reports发行版使用了一个特殊的版本itext-2.1.7,这个版本的输入是 com.lowagie.text ,如果您使用的是更高版本,那么导入是 com.itextpdf.text ,就像修改后的例子一样


When using different fonts in jasper report, you need to use font-extensions.

However if the font is not rendered correctly is there a way that I can test if the font is supported by pdf so that I can understand if the problem is related to my font-extensions or to my .ttf font?

The incorrect rendering of font when exporting to pdf from jasper reports is a common problem example Jasper Reports PDF doesn't export cyrillic values, as seen in checklist point 1 using font-extensions are not always enough, the font need's also to be supported by pdf generating library and able to render the actual character. This is why I have decided to pass this Q-A style questions, so that future user when hitting checklist 1 can have a reference on how to quickly test the font.

解决方案

Since jasper report use the library the easiest way to test if your font will be rendered correctly in pdf is to test it directly with itext.

Example program*, adapted from iText: Chapter 11: Choosing the right font

import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfWriter;

public class FontTest {

    /** The resulting PDF file. */
    public static final String RESULT = "fontTest.pdf";
    /** the text to render. */
    public static final String TEST = "Test to render this text with the turkish lira character \u20BA";

    public void createPdf(String filename) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
        document.open();
        BaseFont bf = BaseFont.createFont(
            "pathToMyFont/myFont.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font font = new Font(bf, 20);
        ColumnText column = new ColumnText(writer.getDirectContent());
        column.setSimpleColumn(36, 730, 569, 36);
        column.addElement(new Paragraph(TEST, font));
        column.go();
        document.close();
    }

    public static void main(String[] args) throws IOException, DocumentException {
        new FontTest().createPdf(RESULT);
    }
}

Some notes (seen in example):

  • To render special characters use it's encoded value example \u20BA to avoid problems of encoding type on your file.
  • Consider to always use Unicode encoding, this is recommended approach in the newer PDF standard (PDF/A, PDF/UA) and gives you the possibility to mix different encoding types, with the only dis-advantage of slightly larger file size.

Conclusion:

If your font is rendered correctly in the "fontTest.pdf", you have a problem with your font-extensions in jasper report.

If you font is not rendered correctly in the "fontTest.pdf", there is nothing you can do in jasper reports, you need to find another font.


*Latest jasper-reports distribution use a special version itext-2.1.7, the imports in this version is com.lowagie.text, if you are using later version the imports are com.itextpdf.text as in adapted example.

这篇关于我如何测试如果我的字体在pdf中正确显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆