java basefont,iText,Font,BaseFont和createFont()怎么了?

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

I can get this call to work:

BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1257,

BaseFont.EMBEDDED);

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."

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.

解决方案

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值