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

19,771

Solution 1

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

Solution 2

Try the following:

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

Solution 3

I have used like this:

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

Solution 4

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

Share:
19,771
Animesh
Author by

Animesh

Technical Test Lead at Infosys Limited.

Updated on June 14, 2022

Comments

  • Animesh
    Animesh almost 2 years

    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 ?