How to add underline text in PDF in C#

Adding text into the PDF files is one of the most important requirements for developers. With the help of Spire.PDF, developer can draw transform text, alignment text and rotate text in PDF files easily. This tutorial will show you how to add underline text in C#.

By using the method canvas.drawstring offered by Spire.PDF, developers can set the position, font, brush and style for the adding texts. With the PdfFontStyle, developers can set the style to underline, bold, italic, regular and strikeout. Here comes to the code snippet of how to add underline text in PDF.

Step 1: Create a new PDF document.

PdfDocument pdf = new PdfDocument();

Step 2: Add a new page to the PDF file.

PdfPageBase page = pdf.Pages.Add();

Step 3: Create a true type font with size 20f, underline style.

PdfTrueTypeFont font = new PdfTrueTypeFont(@"C:\WINDOWS\Fonts\CONSOLA.TTF", 20f, PdfFontStyle.Underline);

Step 4: Create a blue brush.

PdfSolidBrush brush = new PdfSolidBrush(Color.Blue);

Step 5: Draw the text string at the specified location with the specified Brush and Font objects.

page.Canvas.DrawString("Hello E-iceblue Support Team", font, brush, new PointF(10, 10));

Step 6: Save the PDF file.

pdf.SaveToFile("Result.pdf", FileFormat.PDF);

Effective screenshot:

How to add underline text in PDF in C#

Full codes:

namespace _Addunderlinetext
{
    class Program
    {
        static void Main(string[] args)
        {
           
            PdfDocument pdf = new PdfDocument();
            PdfPageBase page = pdf.Pages.Add();
            PdfTrueTypeFont font = new PdfTrueTypeFont(@"C:\WINDOWS\Fonts\CONSOLA.TTF", 20f, PdfFontStyle.Underline);
            PdfSolidBrush brush = new PdfSolidBrush(Color.Blue);
            page.Canvas.DrawString("Hello E-iceblue Support Team", font, brush, new PointF(10, 10));
            pdf.SaveToFile("Result.pdf", FileFormat.PDF);
        }
    }
}