Animesh Animesh - 3 months ago 39
C# Question

iTextSharp - C# - Make a font bold as well as underlined

This is the code which I am trying to make a bold and underlined text.

Font header = new Font(Font.FontFamily.TIMES_ROMAN, 15f, Font.BOLD, BaseColor.BLACK);

But all I get is underline and not bold. Is there any way I can get both underline and bold font ?


As an alternative to using the Font to underline text, you can also use the setUnderline() method that is available for the Chunk class. When you use the solution explained in the answer by Joachim Isaksson, you can choose the line width of the line, nor the distance from the baseline of the text. The setUnderline() method gives you all that freedom.

Read my answer to the question How to strike through text using iText? for more info.

Take a look at these examples:

Chunk chunk1 = new Chunk("0123456789");
chunk1.SetUnderline(2, -3);
document.Add(new Phrase(chunk1));
Chunk chunk2 = new Chunk("0123456789");
chunk2.SetUnderline(2, 3);
document.Add(new Phrase(chunk2));

In both cases, the line that is drawn will be 2 user units thick instead of the default 1 user unit. In chunk1 the line will be drawn 3 user units under the text (this is underline functionality). In chunk2, the line will be drawn above the baseline (this is strikethrough functionality).