Q: How do I underline text?
I know that PDF does not support underline style for text, but I would
like to achieve the same effect by drawing a line (i.e. path) under
the text.
A: You could add a line under text element using its bounding box. For
example:
…
text_element.GetBBox(bbox);
…
builder.PathBegin();
builder.MoveTo(bbox.x1, bbox.y1);
builder.LineTo(bbox.x2, bbox.y2);
Element line = builder.PathEnd();
// Set color attributes for the line…
line.SetPathFill(false);
line.SetPathStroke(true);
GState gs = line.GetGState();
gs.SetStrokeColorSpace(ColorSpace.CreateDeviceRGB());
gs.SetStrokeColor(new ColorPt(1, 0, 0)); // red
// Set line thickness to be some fraction of text height
gs.SetLineWidth(bbox.Height()/100.0);
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 http://www.pdftron.com/pdfnet/faq.html#how_watermark).