iText, What's going on with Font, BaseFont and createFont()?

匿名 (未验证) 提交于 2019-12-03 01:33:01

问题:

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.

回答1:

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


回答2:

The second parameter is the encoding.

Refer to documentation here for more information.



回答3:

This is what worked for me. Old post, but I couldn't find a simple answer.

//Here you setup the font that you want. I put it under the root/Content/fonts folder in my project Font font = FontFactory.GetFont("~/Content/fonts/ARIALN.ttf", BaseFont.CP1252,false, 9);  //Here I create the paragraph then assign the font to it at the end var addressAttn = new Paragraph("Attn:  Manager, Billing Services", font); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!