Animesh
Animesh

Reputation: 228

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 ?

Upvotes: 8

Views: 23858

Answers (4)

Wim Kuijpers
Wim Kuijpers

Reputation: 117

Bold

Font1.SetStyle(1)

Italic

Font1.SetStyle(2)

Bold and Italic

Font1.SetStyle(3)

Underline

Font1.SetStyle(4)

Bold and Italic and Underline

Font1.SetStyle(7)

1+2+4=7

Upvotes: 3

Vilmar Oliveira
Vilmar Oliveira

Reputation: 117

I have used like this:

Dim font8Underline As Font = FontFactory.GetFont("ARIAL", 8, Font.BOLD)
font8Underline.SetStyle(Font.UNDERLINE)

Upvotes: 3

Jaime Plancarte
Jaime Plancarte

Reputation: 161

Try the following:

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

Upvotes: 16

Bruno Lowagie
Bruno Lowagie

Reputation: 77606

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

Upvotes: 9

Related Questions