gpt4 book ai didi

java - 如何测试我的字体是否在 pdf 中正确呈现?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:01:18 25 4
gpt4 key购买 nike

jasper报表中使用不同字体时,需要使用font-extensions .

但是,如果字体未正确呈现,有没有一种方法可以测试 pdf 是否支持该字体,这样我就可以了解问题是与我的字体扩展有关还是与我的 .ttf 字体?

从 jasper 报告导出为 pdf 时字体呈现不正确是一个常见问题示例 Jasper Reports PDF doesn't export cyrillic values ,如 list 第 1 点所示,使用字体扩展并不总是足够的,字体也需要 pdf 生成库支持并能够呈现实际字符。这就是为什么我决定通过这种问答式的问题,以便 future 的用户在点击 list 1 时可以引用如何快速测试字体。

最佳答案

由于 jasper 报告使用了 库 测试您的字体是否会在 pdf 中正确呈现的最简单方法是直接使用 itext 对其进行测试。

示例程序*,改编自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);
}
}

一些注释(见示例):

  • 要渲染特殊字符,请使用它的编码值示例\u20BA 以避免文件的编码类型问题。
  • 考虑始终使用Unicode 编码,这是推荐的方法在较新的 PDF 标准(PDF/A、PDF/UA)中,让您可以混合不同的编码类型,唯一的缺点是更大的文件大小。

结论:

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.


*最新的 jasper-reports 发行版使用特殊版本 itext-2.1.7,此版本中的导入是 com.lowagie.text,如果您使用更高版本,则导入是com.itextpdf.text 作为改编示例。

关于java - 如何测试我的字体是否在 pdf 中正确呈现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35127956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com