Home > Code > C# > How to Create Local Hyperlink to Pages in Same PDF inside .NET Apps

How to Create Local Hyperlink to Pages in Same PDF inside .NET Apps

by sher azam   on May 27, 2015   Category: C#   |  Views: 687    |  Points: 25   |  Gold 


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.

The following code snippet shows how to add the local hyperlink a PDF file

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")



More about Aspose.Pdf for .NET

- Homepage of Aspose.Pdf for .NET: http://www.aspose.com/.net/pdf-component.aspx

- Download Aspose.Pdf for .NET at: http://www.aspose.com/community/files/51/.net-components/aspose.pdf-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts