Create a link in PDF to go back to index page

Hi Team,

We have a need of creating a PDF in a way that we can have each page in PDF contains a link in the footer or at the bottom right in a page. The purpose of this page is to allow user to jump to specific page in PDF (like table of contents page or first page like wise).

We have referred below ASPOSE articles and tried different solutions.

For one of the article we are able to create a link which will allow user to jump to specific page. However, the position is something where we are struggling and not able to set the position to the bottom-right of the page or in footer right side. Please find the code snippet we have tried till now.


            Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(dataDir + fileName);

            for (int index = 1; index <= pdfDocument.Pages.Count(); index++)
            {
                // Add page to pages collection of PDF file
                Page page = pdfDocument.Pages[index];

                // Create local hyperlink instance
                Aspose.Pdf.LocalHyperlink link = new Aspose.Pdf.LocalHyperlink();

                // Set target page for link instance
                link.TargetPageNumber = 1;
                //link.Target.HorizontalAlignment = HorizontalAlignment.Right;
                //link.Target.VerticalAlignment = VerticalAlignment.Bottom;

                // Create Text Fragment instance
                TextFragment text = new TextFragment("back to top", new TabStops());
                //text.BaselinePosition.XIndent = 0;
                //text.BaselinePosition.YIndent = 0;

                text.VerticalAlignment = VerticalAlignment.Bottom;
                text.HorizontalAlignment = HorizontalAlignment.Right;

                text.Position.XIndent = 0;
                text.Position.YIndent = 0;

                // set text properties
                text.TextState.Font = FontRepository.FindFont("Arial");
                text.TextState.FontSize = 10.0F;
                text.TextState.FontStyle = FontStyles.Italic;
                text.TextState.FontStyle = FontStyles.Regular;
                text.TextState.ForegroundColor = Color.Blue;
                text.TextState.Underline = true;

                // Set TextFragment hyperlink
                text.Hyperlink = link;

                page.Paragraphs.Add(text);
            }

            dataDir = dataDir + fileName;

            // Save output document
            pdfDocument.Save(dataDir);

Please let us know if we are missing anything or how we can achieve this, so that we have a link on each page in footer or at bottom-right side of the page.

Please note, we are using latest version of the aspose and having dot framework as .Net Framework 8.0.

Please let us know if you need any specific details from us.

Thank you.

@lpappachen

Please try to use below code snippet and let us know if that helps:

// Load an existing PDF document
Document pdfDocument = new Document();
pdfDocument.Pages.Add();
// Get the first page of the PDF (you can loop through all pages if needed)
Page page = pdfDocument.Pages[1];

// Create a TextFragment with the text to add
TextFragment textFragment = new TextFragment("Text at bottom right");

// Set text properties like font size, etc. (optional)
textFragment.TextState.FontSize = 12;
textFragment.TextState.Font = FontRepository.FindFont("Helvetica");
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.Black;

// Calculate the bottom-right position
double pageWidth = page.GetPageRect(true).Width;
double pageHeight = page.GetPageRect(true).Height;

// Margin to avoid cutting text too close to the page border
double margin = page.PageInfo.Margin.Right;

// Calculate the X position (page width - text width - margin)
double xPosition = pageWidth - textFragment.Rectangle.Width - margin;

// Calculate the Y position (close to the bottom margin)
double yPosition = margin;

// Set the text fragment position (lower-left corner)
textFragment.Position = new Position(xPosition, yPosition);

// Add the text fragment to the page
page.Paragraphs.Add(textFragment);

// Save the modified document
pdfDocument.Save(dataDir + "textposition.pdf");

textposition.pdf (1.9 KB)

Could you please share how we can set the link in the footer for same purpose?

@lpappachen

You can use TextFragment.Hyperlink property and set it using LocalHyperlink() class that can point to any other text fragment on any page inside PDF.

Hi,

The above code snippet works fine but it is not showing in footer. It is using textFragment.Hyperlink and being set with LocalHyperlink() class that can point to any other text fragment on any page inside PDF.

However, the text’s position is not in the footer but rather its position is in the bottom of the page. Could you please share the code which can have text link in the footer it self?

@lpappachen

You mentioned previously that either you want to display in footer or at bottom-right. We shared the code snippet to show the text at bottom-right corner of the page. Nevertheless, please note that there are no such header footer entities in the PDF format. They are only given for the PDF generation in the API. Footer can be of different dimensions and different margins.

Can you please share the a sample expected output PDF for our reference? It can be a simple PDF generated using Adobe Reader. We will try to create it using API in our environment and share the code snippet with you.

Hello,

Please find the attached pdf sample file. This file is being generated using ASPOSE and with the same code you have shared.

sample pdf file with back to top link.pdf (167.7 KB)

If you can see, the link is being added but it is overlapping the actual content of the file. That is the reason why I have asked if we can have this link at the bottom right of the page or in the footer. If you can see there is lots of space between footer and last line of the page but not being utilized properly for this link.

Nevertheless, the client wants the link in a way that it should not be overlapping the content of the page. Hence, could you please share the code snippet or a way so that the link can be provided in footer or in the white gap between footer and last line of the page?

@lpappachen

We are checking it and will get back to you shortly.

@lpappachen

During initial investigation, we tried below approach in our environment and gave position as x=10 and y = 10. But these values are causing 7 new empty pages to be added in the output PDF.

// Load an existing PDF document
Document pdfDocument = new Document(dataDir + "sample pdf file with back to top link.pdf");
//pdfDocument.Pages.Add();
// Get the first page of the PDF (you can loop through all pages if needed)
Page page = pdfDocument.Pages[1];

// Create a TextFragment with the text to add
TextFragment textFragment = new TextFragment("Text at bottom right");

// Set text properties like font size, etc. (optional)
textFragment.TextState.FontSize = 12;
textFragment.TextState.Font = FontRepository.FindFont("Helvetica");
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.Black;
textFragment.Hyperlink = new LocalHyperlink() { TargetPageNumber = 1 };

// Set the text fragment position (lower-left corner)
textFragment.Position = new Position(10, 10);

// Add the text fragment to the page
page.Paragraphs.Add(textFragment);

// Save the modified document
pdfDocument.Save(dataDir + "textposition.pdf");

Therefore, we have generated a task as PDFNET-58256 in our issue tracking system to figure out and resolve this issue. We will investigate in further details and keep you posted with the status of its rectification. Please be patient and spare us some time.

We are sorry for the inconvenience.

Hello Team,

Any further updates on this?

@lpappachen

The ticket has been logged recently in our issue tracking system and will be prioritized on a first come first serve basis. As soon as we make some progress towards its resolution, we will surely inform you via this forum thread. Please be patient and spare us some time. We are sorry for the inconvenience.