How do I underline text in a PDF?

255 views
Skip to first unread message

Support

unread,
Sep 11, 2009, 6:07:32 PM9/11/09
to PDFTron PDFNet SDK
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
...
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).


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);
page.AnnotPushBack(underline);
// underline.RefreshAppearance(); optional

The above code is in the context of AnnotationTest sample project
(http://www.pdftron.com/pdfnet/samplecode.html#Annotation)

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

Dennis Van Acker

unread,
Jul 17, 2016, 2:36:37 AM7/17/16
to PDFTron PDFNet SDK
Sometimes, the lines appear thicker, is there a way to fix this?

Ryan

unread,
Jul 18, 2016, 4:46:28 PM7/18/16
to PDFTron PDFNet SDK
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.

Reply all
Reply to author
Forward
0 new messages