iTextSharp - Working with Fonts

Following on from my introduction to iTextSharp, the free PDF utility that lets you work with PDF files within ASP.NET, this article looks at working with fonts in PDF documents that you create. If you haven't read the first article in this series, I recommend that you do so now.

iTextSharp has built-in support for the 14 Type 1 fonts: Courier, Courier Bold, Courier Italic, Courier Bold and Italic, Helvetica, Helvetica Bold, Helvetica Italic, Helvetica Bold and Italic, Times Roman, Times Roman Bold, Times Roman Italic, Times Roman Bold and Italic, Symbol, ZapfDingBats®. Helvetica translates more or less to Windows Arial font, while Times Roman has an equivalent in Times New Roman. The default font is Helvetica, 12pt, black in the style typically known as Normal.

There are three principal ways to set the font to work with: one is to use the BaseFont.CreateFont() method, the second is to use the FontFactory.GetFont() method , and the third is to instantiate a new Font object. BaseFont.CreateFont() is a lot more limited, and only sets up the definition of a font. new Font() allows for propogation of font styles from one font to the next. FontFactory.GetFont() returns a valid and new Font object that you can work with directly. It also offers 14 overloaded constructors which gives you a lot more options to work with. For that reason, you are most likely going to use the FontFactory.GetFont() method. But we will get BaseFont.CreateFont() out of the way first.

 

BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);

Font times = new Font(bfTimes, 12, Font.ITALIC, Color.RED);

 

The above lines create a BaseFont object and uses the built-in constant values to set the font family and encoding. It also specifies false for embedding the font within the PDF document. this decreases the overall size of the document, but should be set to true if you are using fonts that are unlikely to be on the user's system, or if you plan to get the PDF printed professionally. A new Font object is created using the BaseFont object, and further setting the font size in points, the style and the colour - again, using iTextSharp's constants for these values. Now the font is put to use in a paragraph:

 

string path = Server.MapPath("PDFs");

Document doc = new Document();

PdfWriter.GetInstance(doc, new FileStream(path + "/Font.pdf", FileMode.Create));

doc.Open();

doc.Add(new Paragraph("This is a Red Font Test using Times Roman", times));

doc.Close();

 


And the result (if all goes well) is as follows:

Now to the FontFactory.GetFont() method. This method has 14 (count 'em!) overloads that allow you to specify anything from font family, size, colour, style, embedding, encoding and caching in pretty much any combination you like. Each time that you call FontFactory.GetFont(), a new Font object is created. This method will work directly with all fonts registered by iTextSharp, which includes all fonts found in the Windows default font directory. On Win XP Pro, that's usually C:/WINDOWS/Fonts. If you want a list of all the registered fonts, the FontFactory.RegisteredFonts collection holds them. This can be useful if you want to find the exact name of each font.

int totalfonts = FontFactory.RegisterDirectory("C:\\WINDOWS\\Fonts");

StringBuilder sb = new StringBuilder();

foreach (string fontname in FontFactory.RegisteredFonts)

{

  sb.Append(fontname + "\n");

}

doc.Add(new Paragraph("All Fonts:\n" + sb.ToString()));

 

Here's a variety of ways to use the GetFont() method:

 

Font arial = FontFactory.GetFont("Arial", 28, Color.GRAY);

Font verdana = FontFactory.GetFont("Verdana", 16, Font.BOLDITALIC, new Color(125, 88, 15));

Font palatino = FontFactory.GetFont(

 "palatino linotype italique",

  BaseFont.CP1252,

  BaseFont.EMBEDDED,

  10,

  Font.ITALIC,

  Color.GREEN

  );

Font smallfont = FontFactory.GetFont("Arial", 7);

Font x = FontFactory.GetFont("nina fett");

x.Size = 10;

x.SetStyle("Italic");

x.SetColor(100, 50, 200);

 

As you can see, some of them use the iTextSharp Color object to set the colour using a constant, while others use the SetColor() method and pass in RGB values or create a new Color object passing in RGB values. Generally, they use a constant value for the font style, but you can pass in an int representing one of the values, or use the SetStyle() method passing in a string. There are also varying numbers of parameters passed, which ilustrates some of the different overloads available. Intellisense, Code Complete and the Object Browser will reveal the full panoply of options.

Registering Fonts

You may have a situation where you cannot install a font you want to use in the default font directory on the web server, so you have to register it explicitly with iTextSharp.

 

string fontpath = Server.MapPath(".");

BaseFont customfont = BaseFont.CreateFont(fontpath + "myspecial.ttf", BaseFont.CP1252, BaseFont.EMBEDDED);

Font font = new Font(customfont, 12);

string s = "My expensive custom font.";

doc.Add(new Paragraph(s, font));

 

You may notice that the above example is embedded in the PDF file (BaseFont.EMBEDDED), as your expensive font is unlikely to exist on users' operating systems.