How to use System Font in iTextSharp with

Tags: fontsfont directoryiText 5

I'm using Itextsharp to convert text files to PDF documents dynamically using However I need to use a system font that is not part of the iTextSharp library. I've seen some code examples using C#. However I'm a newbie in programming and my experience is all in Visual Basic. Can someone help me with writing the code to use a system font?

Posted on StackOverflow on May 2, 2015 by Don Wilson

Suppose that you want to use Arial regular and you have the file arial.ttf in your C:\windows\fonts directory, then creating the Font object is as easy as this:

Dim arial As BaseFont = BaseFont.createFont("c:\windows\fonts\arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED)
font = New Font(arial, 16)

Using the font is easy too:

document.Add(New Paragraph("Hello World, Arial.", font))

This is almost a literal translation of the Java and C# examples that can be found in abundance. If this doesn't solve your problem, please show what you've tried and explain why it doesn't work.

Here's the code I'm going to attempt to use:

Dim fontpath As String = "C:\Windows\Fonts\"
Dim bf As BaseFont = BaseFont.CreateFont(fontpath &
    "Arial monospaced for SAP.ttf", BaseFont.CP1252, BaseFont.EMBEDDED)
Dim ffont As New Font(bf, 7)

You claim that you have a file named Arial monospaced for SAP.ttf in the directory C:\Windows\Fonts\. I am 99% sure that this is not true. I have searched Google for such a font and I found a webpage that says:

Go to c:\windows\fonts and [it] should contain arimon__.ttf and arimonbd.ttf

In other words, you need:

Dim arial As BaseFont = BaseFont.createFont(
    "c:\windows\fonts\arimon__.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED)
Dim arialbd As BaseFont = BaseFont.createFont(
    "c:\windows\fonts\arimonbd.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED)

This is not an iText problem. It is a problem of not understanding the difference between a file containing a font program and the name of that font program.

I'm looking at the system folder (C:\Windows\Fonts) and the file name is called just that.

Allow me to guide you through the basics of Windows Explorer.

You are looking at your font directory like this:

Font directory
Font directory

That view shows your the names of the fonts, NOT the name of the font files. Please select the view icon in the top right corner and change it to view the details. This is what you'll see:

Detailed view font directory
Detailed view font directory

Now right click on the header of the detailed list and select Font file names. This is what you'll see:

Detailed view with file names
Detailed view with file names

Use the path as shown in this overview and your code will work. If it doesn't work post a new question and explain exactly what you're doing, so that we can help you.

I've reviewed your screen shots and have added the "Font File Name" column as suggested. As you thought, there is not file name for "Arial Monospaced for SAP". To complicate matters, I don't see "arimon_.ttf" or "arimonbd.tff" listed either.

If you don't find arimon__.ttf nor arimonbd.ttf under c:\windows\fonts, the fonts probably aren't there. If they aren't there, your code won't work. Another way to check their presence, is by clicking on Run under Windows (in the menu that opens when right-clicking the Start icon) and opening cmd. Then do cd c:\windows\fonts followed by dir ari*. This will show you a list of all the font files that start with ari.

Take a look at the following screen shot that shows what happens when I do this on my machine:

Using cmd to find fonts
Using cmd to find fonts

As you can see, there is no arimon__.ttf or arimonbd.ttf in my c:\windows\fonts directory, hence code that tries to create a BaseFont or Font object using arimon__.ttf or arimonbd.ttf would never work on my computer. If you create an application that requires these fonts, you'll have to ship these TTF files with your application (provided that you are allowed to do so).