关于font和basefont到底是怎么回事,对我来说有很多不解之处。尤其是当涉及到构造函数时。iText网站给出了这一行作为新字体的示例代码
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.HELVETICA替换BaseFont.CP1257,它就不能工作了,我得到一个页面说"failed to load pdf document“。
我试着查看了类文件,但我似乎找不出第二个参数是什么(我假设它是备用字体,以防第一个字体不起作用,就像在HTML中一样),我也不明白为什么有些字体可以工作,而另一些则不行。
发布于 2013-12-18 04:29:35
要从jar中加载,请使用前导斜杠,否则使用,只需使用您的字体的绝对路径("C:...\fonts\Sansation_Regular.ttf")。例如:
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'
发布于 2012-08-15 22:37:33
发布于 2015-07-08 20:51:19
这就是对我有效的方法。老帖子,但我找不到一个简单的答案。
//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);
https://stackoverflow.com/questions/11366354
复制相似问题