问题描述
我想在用iText PDF标记PDF文件时将Verdana用作字体.原始文件使用Verdana,这是Basefont类中的一个选项.
I want to use Verdana as a font while stamping a PDF file with iText PDF. The original file uses Verdana, which isn't an option in the class Basefont.
这是立即创建我的字体的功能:
Here is the function to create my font right now:
def standardStampFont() {
return BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false)
}
我想将其更改为Verdana字体,但是仅将部分BaseFont.HELVETICA
与"Verdana"
交换是行不通的.
I'd like to change that to the Verdana Font, but simply exchanging the Part BaseFont.HELVETICA
with "Verdana"
doesn't work.
有什么主意吗?预先感谢!
Any idea? Thanks in advance!
推荐答案
如前所述,iText支持Standard Type 1字体,因为iText随附AFM文件(Adobe Font Metrics文件). iText不了解其他字体的字体规格(Verdana不是Standard Type 1字体).您需要提供Verdana字体文件的路径.
As documented, iText supports the Standard Type 1 fonts, because iText ships with AFM file (Adobe Font Metrics files). iText has no idea about the font metrics of other fonts (Verdana isn't a Standard Type 1 font). You need to provide the path to the Verdana font file.
BaseFont.createFont("c:/windows/fonts/verdana.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED)
请注意,我将false
更改为BaseFont.EMBEDDED
,因为与您相同的问题也会出现在查看文件的人的身边:他的PDF查看器可以呈现Standard Type 1字体,但是可能无法渲染其他字体,例如Verdana.
Note that I change false
to BaseFont.EMBEDDED
because the same problem you have on your side, will also occur on the side of the person who looks at your file: his PDF viewer can render Standard Type 1 fonts, but may not be able to render other fonts such as Verdana.
注意事项:硬编码路径"c:/windows/fonts/verdana.ttf"
在我的本地计算机上对我有用,因为可以在本地计算机上使用该路径找到字体文件.但是,此代码在托管iText站点的服务器上不起作用(这是Linux服务器,甚至没有c:/windows/fonts
目录).我通过示例使用此硬编码路径.部署应用程序时,应确保字体存在并且可用.
Caveat: The hard coded path "c:/windows/fonts/verdana.ttf"
works for me on my local machine because the font file can be found using that path on my local machine. This code won't work on the server where I host the iText site, though (which is a Linux server that doesn't even have a c:/windows/fonts
directory). I am using this hard coded path by way of example. You should make sure that the font is present and available when you deploy your application.
这篇关于如何在Stamper中使用Verdana字体(iText PDF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!