Linking an Image to Bookmark

Using DocumentBuilder.InsertHyperLink() Iam not able to link the image to bookmark. How can I link an Image to an existing bookmark?

Unfortunately in the current version we don't have simple method to do that. But it is technically possible to do through node manipulation.

Here is an example of how that could be done when building document dynamically:

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.StartBookmark("Hello");

builder.Write("Test");

builder.EndBookmark("Hello");

builder.Writeln("Some text");

// Specify font formatting for the hyperlink.

builder.Font.Color = System.Drawing.Color.Blue;

builder.Font.Underline = Underline.Single;

// Insert the hyperlink.

builder.InsertHyperlink("Link text", "Hello", true);

// Clear hyperlink formatting.

builder.Font.ClearFormatting();

builder.Writeln("Some text");

builder.InsertHyperlink("Link text", "Hello", true);

string imageFileName = Application.StartupPath + @"\test.gif";

builder.InsertImage(imageFileName);

InlineShape shape = (InlineShape)builder.CurrentParagraph.LastChild;

shape.Remove();

builder.CurrentParagraph.InsertBefore(shape, builder.CurrentParagraph.LastChild);

shape.PreviousSibling.Remove();

When i used the above code it was linking the image to Bookmark but the Display text (link text in the above example) is getting deleted.

This can be worked around with the following trick:

builder.InsertHyperlink("This text does not matter", @"Hello "" \o ""This text will be displayed as a link"" ", true);