How to introduce a custom font weight?

Tags: bolditalicmimic boldmimic italicfontsiText 7

Is there a way to set font weight in iTextSharp as a number? Now 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 7’s Text object has a method called setTextRenderingMode() that is demonstrated in the SayPeace example:

PdfFont font = PdfFontFactory.createFont("c:/windows/fonts/verdana.ttf", PdfEncodings.IDENTITY_H, true);
Text bold = new Text("Bold")
Paragraph p = new Paragraph(bold);

It tells the Text 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).

Click this link if you want to see how to answer this question in iText 5.