Free Support Forum - aspose.com

How to Insert Hyperlink to Nth Page of Document using .NET

I’m trying to find a simple example of how to use Aspose.Words in order to create a Hyperlink that will take you to Page N of the document you’re creating. Can you provide a simple code snippet here?

@kidkeogh

You can insert hyperlink to bookmark within the document to achieve your requirement. Following code example shows how to insert bookmark at 10th page of document and insert hyperlink at the start of document. Hope this helps you.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

for (int i = 0; i < 9; i++)
{
    builder.InsertBreak(BreakType.PageBreak);
}
builder.StartBookmark("page10");
builder.EndBookmark("page10");

builder.Writeln("Text outside of the bookmark.");

builder.MoveToDocumentStart();
                
// Insert a HYPERLINK field that links to the bookmark. We can pass field switches
// to the InsertHyperlink method as part of the argument containing the referenced bookmark's name.
builder.Font.Color = Color.Blue;
builder.Font.Underline = Underline.Single;
builder.InsertHyperlink("Link to Bookmark", @"page10"" \o ""Hyperlink Tip", true);

doc.Save(MyDir + "output.docx");

If you still face problem, please ZIP and attach your expected output Word document here for our reference. We will then provide you code example according to your requirement.

Hello there… and sorry for the late reply.

Our problem is that by the time we create the bit of the document where the hyperlink should go, the bit that it’s pointing to does not exist yet.

Is there a way to add bookmarks and then move them to another location at a later stage?

@kidkeogh

Please note that Aspose.Words mimics the behavior of MS Word. You can create hyperlink into document using Aspose.Words as MS Word does.

Yes, you can insert bookmark into document and later move their position into another location in the document.

Please ZIP and attach your input and expected output Word documents here for our reference. We will then provide you more information about your query. Please manually create your expected Word document using Microsoft Word.

Thanks Tahir,

Could you provide a code sample to find a bookmark and moving it to another page in the document?

@kidkeogh

You can get the bookmark from the document using Range.Bookmarks property as shown below.

Document doc = new Document(MyDir + "input.docx");
Bookmark bm = doc.Range.Bookmarks["bookmarkname"]; 

To insert the bookmark at any location in the document, you need to move the cursor to the desired location and insert the bookmark as shown below.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.StartBookmark("My Bookmark");
builder.Writeln("Text inside a bookmark.");
builder.EndBookmark("My Bookmark");

We suggest you please read the following articles.
Working with Bookmarks
Navigation with Cursor

Following code example shows how to find a bookmark and insert it at the start of 7th page. We have attached the input and output documents in this post for your kind reference. Hope this helps you.

Docs.zip (18.5 KB)

Document doc = new Document(MyDir + "input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

Bookmark bm = doc.Range.Bookmarks["mybookmark"];

LayoutCollector layoutCollector = new LayoutCollector(doc);

NodeCollection nodes = doc.GetChildNodes(NodeType.Paragraph, true);

Node node = nodes.Cast<Node>().Where(p => layoutCollector.GetStartPageIndex(p) == 7).FirstOrDefault<Node>();
if (node != null)
{
    builder.MoveTo(node);
    bm.Remove();
    builder.StartBookmark(bm.Name);
    builder.Write("Text inside bookmark");
    builder.EndBookmark(bm.Name);
}    

doc.Save(MyDir + "21.1.docx");