Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Verdana Font in Stamper (iText PDF)

Tags:

pdf

itext

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

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!

like image 461
Alain Sarti Avatar asked Sep 20 '25 09:09

Alain Sarti


1 Answers

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.

like image 89
Bruno Lowagie Avatar answered Sep 23 '25 08:09

Bruno Lowagie