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);
font_main.weight = 600;Is this possible?
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 (
Arial Bold-Italic (
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
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.
Chunk object has a method called
setTextRenderMode() that is demonstrated in the
Allow me to show the corresponding method in iTextSharp:
Chunk bold = new Chunk("Bold", font); bold.SetTextRenderMode( PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, 0.5f, GrayColor.GRAYBLACK );
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
- 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.
- 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.