How do I underline text in a PDF?

Q: I am able to create a new PDF and output text using a True Type
font but cannot find any way to set the style to be underlined? Any
assistance would be greatly appreciated.
A: PDF format does not allow underline style as part of the font
definition. To simulate underline PDF producers add a line under text
element using its bounding box. For example:

// C# pseudocode
builder.MoveTo(bbox.x1, bbox.y1);
builder.LineTo(bbox.x2, bbox.y2);
Element line = builder.PathEnd();

// Set color attributes for the line...

GState gs = line.GetGState();
gs.SetStrokeColor(new ColorPt(1, 0, 0)); // red

// Set line thickness to be some fraction of text height

writer.WriteElement(line); // Add line to PDF page ...

Please note that the above pseudocode uses ElementBuilder and
ElementWriter to add new content to exisitng page. For an example of
how to use these classes please take a look at ElementBuilder sample
project (or

In case you would like to create Underline text annotation you could
use the following approach:

// C++ pseudocode

//Create an Underline annotation...
QuadPoint qp;
qp.p1.x=350; qp.p1.y=200;
qp.p2.x=380; qp.p2.y=230;
qp.p3.x=410; qp.p3.y=230;
qp.p4.x=360; qp.p4.y=350;

Underline underline=pdftron::PDF::Annots::Underline::Create(doc, Rect
(350, 200, 450, 390)); underline.SetQuadPoint(0, qp);
// underline.RefreshAppearance(); optional

The above code is in the context of AnnotationTest sample project

For interactive text underlying or highlighting, you may want to use a
built-in underline tool-mode in PDFViewCtrl

Sometimes, the lines appear thicker, is there a way to fix this?

This post is a bit specific to creating underline text during the creation of a PDF.

In general, such as editing a pre-existing PDF, it would be best to underline using an Underline Markup annotation, as that will take care of rendering for you, and be more consistent.

If this doesn’t help, then we would need to see your code for adding the underline.