3
votes

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!

2

2 Answers

5
votes

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.

0
votes

Adding this line makes sure the FontFactory actually registers the fonts in the operating systems' default fonts directories:

FontFactory.RegisterDirectories();

After that all the installed fonts can be found using something like

var myfont = FontFactory.GetFont("Verdana", 10f, iTextSharp.text.Font.NORMAL);

I get that this post is old, but it's still relevant I guess.