Skip to main content
Skip table of contents

How to introduce a custom font weight?

Is there a way to set font weight in iTextSharp as a number?

I can set bold font like this:

iTextSharp.text.Font font_main = new iTextSharp.text.Font(someBaseFont, 12, iTextSharp.text.Font.BOLD);
So, only one "boldness" is available. But I want something like this: font_main.weight = 600; Is this possible?


Posted on StackOverflow on Nov 10, 2014 by retif

When you pick a font family, for instance Arial, you also need to pick a font, e.g. Arial regular (arial.ttf), Arial Bold (arialbd.ttf), Arial Italic (ariali.ttf), Arial Bold-Italic (arialbi.ttf),...

When you switch styles the way you do in your code snippet, iText switches fonts. In other words: you don't use Arial regular to which you apply a style, you switch to another font in the same family: Arial Bold.

A font program such as the one stored in arialbd.tff, contains the instructions to draw the outlines (aka path) of a series of glyphs. These glyphs are rendered by filling the path using a fill color. Knowing this is important to achieve your requirement.

You can control the boldness of your font by changing the line-width render-mode. This is especially useful in case you are working with a font family for which there is no bold version of the font available.

iText's Chunk object has a method called setTextRenderMode() that is demonstrated in the SayPeace example. Allow me to show the corresponding method in iTextSharp:

Chunk bold = new Chunk("Bold", font);
    PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 0.5f,

The first method tells the Chunk that the glyphs should be filled (as usual) and stroked (not usual). The strokes should have a width of 0.5 user units (change this to get a different boldness), the final parameter defines the color of the text.

Two extra remarks for the sake of completeness

  1. There is a similar method to mimic an italic font: the setSkew() method. Using this method, you can introduce two angles. For instance setSkew(0, 25) will write the glyphs with an angle of 25 degrees which more or less corresponds with the angle used in italic fonts.
  2. In the old days (a really long time ago), some PDF producers mimicked boldness by writing the same character over and over again at the same position. As a result, this character was rendered as if it were bold. This has some disadvantages: it is not documented in the ISO standard, so you can't expect that all viewers will support this in the same way. When you extract text from the PDF, you get multiple instances of the same character.
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.