Q: Thank you for your quick and helpful response. You showed me, how
to create a rectangular region inside the document which links to
another destination inside the document.
But I don't want a rectangular region to represent this link, but a
text, so that a click on this text-link will make the reader jump to
the destination.
Of course, I could draw the text separately, calculate the dimension
of the text and create a link region - but as this is quite
uncomfortable I wonder if there was any possibility to automatically
combine this steps.
---
A: Unfortunately PDF format does not support non-rectangular links -
i.e. the hyperlink region can't be associated with a text outline.
In case you would like to add a link along with text (and possibly
image, vecror art) you need to a) stamp the page with new text, b) add
a link for that region.
The following utility method (in C#) illustrates how this can be
implemented. Please note that you have complete freedom to extend or
modify the code to meet your visual and presentation requirements:
Sample use case:
AddTextLink(first_page, "My LINK", "
http://www.pdftron.com", new
Rect(85, 570, 503, 524), true, true);
static void AddTextLink(Page page, String link_txt, String uri,
Rect pos, bool center_align, bool draw_background)
{
pos.Normalize();
// 1) Add new page content
ElementBuilder builder = new ElementBuilder();
ElementWriter writer = new ElementWriter();
writer.Begin(page);
Element element;
if (draw_background) {
element = builder.CreateRect(pos.x1, pos.y1, pos.Width(),
pos.Height());
element.SetPathFill(true);
element.SetPathStroke(false);
element.GetGState().SetFillColorSpace(ColorSpace.CreateDeviceGray());
element.GetGState().SetFillColor(new ColorPt(0.75));
writer.WriteElement(element);
}
// Draw Link text
writer.WriteElement(builder.CreateTextBegin());
pdftron.SDF.SDFDoc doc = page.GetSDFObj().GetDoc();
double font_size = 12;
element = builder.CreateTextRun(link_txt, Font.Create(doc,
Font.StandardType1Font.e_helvetica_bold), font_size);
element.GetGState().SetFillColor(new ColorPt(0));
if (center_align) {
element.SetTextMatrix(1, 0, 0, 1,
pos.x1+(pos.Width()-element.GetTextLength())/2,
pos.y1+(pos.Height()-font_size)/2);
}
else {
element.SetTextMatrix(1, 0, 0, 1, pos.x1, pos.y1);
}
writer.WriteElement(element);
writer.WriteElement(builder.CreateTextEnd());
writer.End();
// Calling Dispose() on ElementReader/Writer/Builder can result in
// increased performance and lower memory consumption.
writer.Dispose();
builder.Dispose();
// 2) Create the link annotation
page.AnnotPushBack(Annot.CreateLink(doc, pos, Action.CreateURI(doc,
uri)));
// In case you would like to create intra-document links replace the
above line with
// page.AnnotPushBack(Annot.CreateLink(doc.GetSDFDoc(), pos,
//
Action.CreateGoto(Destination.CreateFitH(doc.GetPage(page_number),
0)) ));
}