How to Create Local Hyperlink to Pages in Same PDF inside .NET Apps
Snippet Code
Rate this page :
[ 0 votes]
This technical tip shows how .NET developers can create hyperlink to pages in same PDF using Aspose.Pdf for .NET. Aspose.Pdf for .NET provides great feature to PDF creation as well as its manipulation. It also offers the feature to add links to PDF pages and a link can either direct to pages in another PDF file, a web URL, link to launch an Application or even link to pages in same PDF file. In order to add local hyperlinks (links to pages in same PDF file), a class named LocalHyperlink is added to Aspose.Pdf namespace and this class has a property named TargetPageNumber, which is used to specify the target/destination page for hyperlink. In order to add the local hyperlink, we need to create a TextFragment so that link can be associated with the TextFragment. The TextFragment class has a property named Hyperlink which is used to associate LocalHyperlink instance. The following code snippet shows the steps to accomplish this requirement.
//C# Code Sample
// create Document instance
Document doc = new Document();
// add page to pages collection of PDF file
Page page = doc.Pages.Add();
// create Text Fragment instance
Aspose.Pdf.Text.TextFragment text = new Aspose.Pdf.Text.TextFragment("link page number test to page 7");
// create local hyperlink instance
Aspose.Pdf.LocalHyperlink link = new Aspose.Pdf.LocalHyperlink();
// set target page for link instance
link.TargetPageNumber = 7;
// set TextFragment hyperlink
text.Hyperlink = link;
// add text to paragraphs collection of Page
page.Paragraphs.Add(text);
// create new TextFragment instance
text = new TextFragment("link page number test to page 1");
// TextFragment should be added over new page
text.IsInNewPage = true;
// create another local hyperlink instance
link = new LocalHyperlink();
// set Target page for second hyperlink
link.TargetPageNumber = 1;
// set link for second TextFragment
text.Hyperlink = link;
// add text to paragraphs collection of page object
page.Paragraphs.Add(text);
// create a loop instance
for (int i = 0; i < 10; i++)
// add blank pages to PDF file
doc.Pages.Add();
// save resultant file
doc.Save("c:/pdftest/LocalHyperLink.pdf");
//VB.NET Code Sample
' create Document instance
Dim doc As Document = New Document()
' add page to pages collection of PDF file
Dim page As Page = doc.Pages.Add()
' create Text Fragment instance
Dim text As Aspose.Pdf.Text.TextFragment = New Aspose.Pdf.Text.TextFragment("link page number test to page 7")
' create local hyperlink instance
Dim link As Aspose.Pdf.LocalHyperlink = New Aspose.Pdf.LocalHyperlink()
' set target page for link instance
link.TargetPageNumber = 7
' set TextFragment hyperlink
text.Hyperlink = link
' add text to paragraphs collection of Page
page.Paragraphs.Add(text)
' create new TextFragment instance
text = New TextFragment("link page number test to page 1")
' TextFragment should be added over new page
text.IsInNewPage = True
' create another local hyperlink instance
link = New LocalHyperlink()
' set Target page for second hyperlink
link.TargetPageNumber = 1
' set link for second TextFragment
text.Hyperlink = link
' add text to paragraphs collection of page object
page.Paragraphs.Add(text)
' create a loop instance
Dim i As Integer
For i = 0 To 10
' add blank pages to PDF file
doc.Pages.Add()
Next
' save resultant file
doc.Save("c:/pdftest/LocalHyperLink.pdf")