Add a link to redirect to Table of Content page

Hi there,

We are creating a PDF with Aspose tool. We have a requirement to provide a sort of link at the end of every page to return back Table of Contents page (for example page 1). Do you have any feature to achieve this?

@sanjaybk

You can add local hyperlinks in the PDF in order to achieve your requirements. Please check the below documentation article:

@asad.ali, Thanks.
May i know how can i add the Hyperlink to the bottom of the page. I tried to use below code, but the text is appearing on top left corner of the page.

Document doc = new Document(file.FullName);
// Add page to pages collection of PDF file
Page page = doc.Pages[5];
// Create Text Fragment instance
Aspose.Pdf.Text.TextFragment text = new Aspose.Pdf.Text.TextFragment(“link page number test to page 7”);
text.TextState.FontSize = 12;
text.Position.XIndent = 50;
text.Position.YIndent = 700;
// set text properties
text.TextState.ForegroundColor = Color.Blue;
text.TextState.BackgroundColor = Color.LightGray;

            // 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<a class="attachment" href="/uploads/default/64070">Screenshot.PNG</a> (46.6 KB)

            page.Paragraphs.Add(text);
            // Create new TextFragment instance
            
          
            //dataDir = dataDir + "CreateLocalHyperlink_out.pdf";
            // Save updated document
            doc.Save(filePath + file.Name);<a class="attachment" href="/uploads/default/64070">Screenshot.PNG</a> (46.6 KB)

@sanjaybk

Aspose.PDF follows a coordinating system where X and Y coordinates start from bottom left. In other words (0,0) means bottom-left. You can assign values to XIndent and YIndent accordingly to display the text at the bottom of the page. Please feel free to let us know in case you face any issues.

@asad.ali,
Thanks. Unfortunately, the program is taking forever to run if i define the new Position attribute.
I see that LinkAnnotation can provide me solution to my need. However, can you let me know how to define a text to be displayed inside this Rectangle? I’m able to draw a rectangle in a page. But, i need to put a custom text inside like “Click here”. How can i do that?

// Open document
Document document = new Document(file.FullName);
// Create link
Page page = document.Pages[7];
Page destPage = document.Pages[1];
LinkAnnotation link = new LinkAnnotation(page, new Aspose.Pdf.Rectangle(5, 5, 600, 30));
link.Name = “Testing”;
link.Color = Color.Red;
link.Action = new GoToAction(destPage);
page.Annotations.Add(link);

            TextSearchOptions textSearchOptions = new TextSearchOptions(true);
            String find = @"(?i)\b" + "Notice to Investors" + @"\b";
            TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(find, textSearchOptions);

            // Create TextAbsorber object to find all instances of the input search phrase
            //TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("Notice to investors");

            // Accept the absorber for desired
            document.Pages[3].Accept(textFragmentAbsorber);

            // Get the extracted text fragments
            TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;

            // Save updated document
            document.Save(filePath + "output_annotation.pdf");

Also, if i add below line, the program takes forever, not sure what is the problem here.
text.Position = new Position(0, 0);

@sanjaybk

You can use LinkAnnotation.Contents property to specify the text of Link Annotation. However, if it does not help, please share your sample PDF document for our reference so that we can test the scenario in our environment and address it accordingly.