您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何测试我的字体在pdf中是否正确显示?

如何测试我的字体在pdf中是否正确显示?

由于jasper report使用itext库,因此最简单的测试字体是否将在pdf中正确显示方法是直接使用itext测试它。

程序*,改编自iText:第11章:选择正确的字体

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);
    }
}

一些注意事项(如示例所示):

如果在“ fontTest.pdf”中 显示了字体,则在jasper报告中字体扩展名有问题。

如果您在“ fontTest.pdf”中 字体,则在jasper报告中您无能为力,您需要查找其他字体。

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

其他 2022/1/1 18:14:49 有508人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶