Hello friends,
I’m trying to use PDFTron to put a text/string with a hyperlink in a PDF file I created, but I can’t get it to work.
With WriteElement I can’t get it to work somehow. I have already tried a few things, but I can’t find a solution.
Can someone show me a short concrete example, how I can write for example the word “Google” in my PDF, which then leads to “www.google.com ” (web browser) when I click on the word “Google”?
I would be very very grateful.
Thanks in advance.
Hello Frank, welcome to the community.
To add a hyperlink to your text, you would create a link annotation, and overlay it over the text you want to link.
The rectange (bounding box) of the text element can be found using element.GetBBox(urlbbox).
Below is a short example:
element = eb.CreateTextBegin(Font.Create(doc, Font.StandardType1Font.e_times_roman), 12);
writer.WriteElement(element);
string data = "Hello World Google!";
element = eb.CreateTextRun(data);
element.SetTextMatrix(10, 0, 0, 10, 0, 600);
writer.WriteElement(element);
Rect urlbbox = new Rect();
element.GetBBox(urlbbox);
Link hyperlink = Link.Create(doc, urlbbox, pdftron.PDF.Action.CreateURI(doc, "http://google.com"));
hyperlink.RefreshAppearance();
page.AnnotPushBack(hyperlink);
Let me know if this works for you.
It worked very fine. Thanks for your efforts and your help!
I have one more quick question.
How would it be possible in the same construct to underline the text “Google”?
Hi Frank,
See this post:
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.SetP…
Let me know if this works for you.
Hi Joe,
I solved the problem in a similar way.
Thanks again for your help. It was very helpful.