Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
header.SetStyle(Font.UNDERLINE);

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

like image 736
Animesh Avatar asked Jan 28 '16 06:01

Animesh


2 Answers

Try the following:

Font header = new Font(Font.FontFamily.TIMES_ROMAN, 15f, Font.BOLD | Font.UNDERLINE, BaseColor.BLACK);
like image 146
Jaime Plancarte Avatar answered Oct 07 '22 00:10

Jaime Plancarte


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).

like image 37
Bruno Lowagie Avatar answered Oct 07 '22 00:10

Bruno Lowagie