Rectangle for hyperlink

Hi

We need to add hyperlink annotation at right bottom with 10 as margin from both side. The rectangle gives only position from left and top.

It will be very kind if someone help us to get this code, how to set position for botton right with 10 margin.

Thanks
Chandra

@aroc

To add a hyperlink annotation at the bottom right of a PDF page with a margin of 10 units from both sides, you can calculate the position of the rectangle based on the page dimensions. Here’s how you can do it in C#:

  1. First, determine the width and height of the page.
  2. Calculate the rectangle’s coordinates for the hyperlink annotation by subtracting the desired margins from the page dimensions.

Here’s a sample code snippet to achieve this:

using System;
using Aspose.Pdf;
using Aspose.Pdf.Annotations;

namespace AsposePdfExample
{
    public class AddHyperlink
    {
        public static void Run()
        {
            // Open the document
            Document document = new Document("input.pdf");
            Page page = document.Pages[1];

            // Get the page dimensions
            float pageWidth = page.Rect.Width;
            float pageHeight = page.Rect.Height;

            // Define margins
            float margin = 10;

            // Calculate the rectangle for the hyperlink annotation
            float llx = pageWidth - margin; // Lower-left x
            float lly = margin;              // Lower-left y
            float urx = pageWidth;           // Upper-right x
            float ury = pageHeight - margin; // Upper-right y

            // Create the link annotation
            LinkAnnotation link = new LinkAnnotation(page, new Aspose.Pdf.Rectangle(llx, lly, urx, ury));
            link.Action = new GoToURIAction("http://www.aspose.com");

            // Add the link annotation to the page
            page.Annotations.Add(link);

            // Save the updated document
            document.Save("output.pdf");
            Console.WriteLine("Hyperlink added successfully.");
        }
    }
}

In this code:

  • The rectangle for the hyperlink is defined using the calculated coordinates based on the page dimensions and the specified margins.
  • The hyperlink is set to navigate to “http://www.aspose.com” when clicked.

Make sure to adjust the input and output file paths as necessary. This will place the hyperlink annotation at the bottom right corner of the page with a 10-unit margin from both the right and bottom edges.