iText,Font,BaseFont和createFont()怎么了? [英] iText, What's going on with Font, BaseFont and createFont()?

查看:3531
本文介绍了iText,Font,BaseFont和createFont()怎么了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于字体和基本字体的情况,我有很多谜团.特别是在构造函数方面. iText网站将该行作为新字体的示例代码

There is a lot of mystery to me about what is going on with font and basefont. Especially when it comes to the constructor. The iText website gives this line as example code for new fonts

 BaseFont unicode = BaseFont.createFont("c:/windows/fonts/arialuni.ttf", 
                        BaseFont.IDENTITY_H, 
                        BaseFont.EMBEDDED);

我可以打这个电话:

BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1257, 
                  BaseFont.EMBEDDED);

但是,如果我将BaseFont.CP1257替换为BaseFont.HELVETICA,则它将无法正常工作,并且会显示一个页面,提示无法加载pdf文档".

But if I replace BaseFont.CP1257 with say BaseFont.HELVETICA then it doesn't work and I get a page that says "failed to load pdf document."

我试图浏览类文件,但似乎无法弄清楚第二个参数是什么(我假设它类似于备用字体,以防第一种字体不起作用,例如HTML).我不知道为什么某些字体会起作用而其他字体不能起作用.

I tried looking through the class file and I can't seem to figure out what that second parameter is (I'm assuming it is something like a backup font in case the first font doesn't work, like in HTML) and I can't figure out why some fonts would work and not others.

推荐答案

要从jar内加载,请使用前导斜线否则,只需使用您的字体("C:[...] \ fonts \ Sansation_Regular.ttf").例如:

To load it from inside your jar use the leading slash otherwise, just use the absolute path of your font ("C:[...]\fonts\Sansation_Regular.ttf"). For example:

Font font = FontFactory.getFont("/fonts/Sansation_Regular.ttf",
    BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 0.8f, Font.NORMAL, BaseColor.BLACK);
BaseFont baseFont = font.getBaseFont();

  • 字体的相对路径为:"src/main/resources/fonts"
  • 使用itext 5.4.5

    • The relative path of the font is: 'src/main/resources/fonts'
    • Using Itext 5.4.5

      示例: 查看全文

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