本文介绍了使用iText从* .ttf文件创建字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的Resources.class里面的一个方法:
$ b $ pre preublic static Font loadFont(String fontFileName)
{
BaseFont base = null;
$ b $ try
{
base = BaseFont.createFont(Resource.class.getResource(fontFileName +_font.ttf)。toString(),BaseFont.WINANSI,true);

catch(DocumentException | IOException e)
{
e.printStackTrace();
}

Font font = new Font(base,Font.BOLD,15);
返回字体;



$ b我的程序结构是:

  src(文件夹)
核心(包)
//程序使用的所有(但是一个)类
资源(包)
class资源(用于将资源加载到核心类中)
wingding_font.ttf





  p =新短语(一些随机文本 ); 
p.setFont(Resource.loadFont(wingding));
pa =新的段落(p);
pa.setFont(Resource.loadFont(wingding));
document.add(pa);

当我打开PDF文件时,有一些字体,我猜是默认字体注意1:我试图设置字体只有短语(p)和只有段落(pa),但是这并没有改变任何输出注意2:Resource.loadFont(wingding);方法try / catch没有捕捉任何错误。试图创建一个嵌入的字体对象,并使用这种字体来呈现您的文本:

$ $ p $ //这段代码应该在初始化/应用程序启动时运行一次
FontFactory.register(resources /wingding_font.ttf);
Font textFont = FontFactory.getFont(wingding,BaseFont.IDENTITY_H,
BaseFont.EMBEDDED,10); // 10是大小
...
//在渲染文本时重用字体对象的引用
Paragraph p = new Paragraph(someText,textFont);

顺便提一下,iText的 FontFactory 类来帮助加载字体,在 Resources 中不需要 loadFont 方法。



希望有帮助。


This is a method inside my Resources.class:

public static Font loadFont(String fontFileName)
    {
        BaseFont base = null;

        try
        {
            base = BaseFont.createFont(Resource.class.getResource(fontFileName + "_font.ttf").toString(), BaseFont.WINANSI, true);
        }
        catch (DocumentException | IOException e)
        {
            e.printStackTrace();
        }

        Font font = new Font(base, Font.BOLD, 15);
        return font;
    }

Structure of my program is:

src (folder)
    core (package)
        //all (but one) classes used for program
    resources (package)
        class Resources (used to load resources into the "core" classes)
        wingding_font.ttf

This is the snippet of the code which isn't working:

p = new Phrase("some random text");
p.setFont(Resource.loadFont("wingding"));
pa = new Paragraph(p);
pa.setFont(Resource.loadFont("wingding"));
document.add(pa);

When I open the PDF, text is there, but some font, which I guess is the default font, is used.

Note1: I tried to setting font to only Phrase(p), and to only Paragraph(pa), but that didn't change output in any way.

Note2: The Resource.loadFont("wingding"); methods try/catch didn't "catch" any errors.

解决方案

Try to create an embedded font object and use this font to render your text:

//this code should run once at initialization/application startup
FontFactory.register("resources/wingding_font.ttf");
Font textFont = FontFactory.getFont("wingding", BaseFont.IDENTITY_H,
    BaseFont.EMBEDDED, 10); //10 is the size
...
//reuse the reference to the font object when rendering your text
Paragraph p = new Paragraph("someText", textFont);

By the way, iText has the FontFactory class to help load your fonts, you don't need anymore the loadFont method in your Resources.

Hope it helps.

这篇关于使用iText从* .ttf文件创建字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 08:11