How to use Verdana Font in Stamper (iText PDF)

后端 未结 2 968
忘掉有多难
忘掉有多难 2021-01-20 19:45

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 f

2条回答
  •  -上瘾入骨i
    2021-01-20 20:04

    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)
    

    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.

    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.

提交回复
热议问题