Hyperlink in a specific position inside document PDF

I’m working with the aspose API specifically with PDF, I’m trying to insert a HyperLink over PDF document but this one always appears at the top of the page, I need to set a custom position.

I’m using this code
Document doc = new Document();
Page page = doc.Pages.Add();
var text = new TextFragment(“Index”);
text.Position = new Position(300, 300);
LocalHyperlink link = new LocalHyperlink { TargetPageNumber = 2 };
text.Hyperlink = link;
page.Paragraphs.Add(text);
doc.Save(“demo.pdf”);

I want that the hyperlink respects the position indicated in te code above but I can’t make it, specifically that is my problem.

I hope you can help me, I’ll be waiting for your soon answer.

Hi there,


Thanks for your inquiry. I have tested the scenario and noticed that Position property of TextFragment is not being honored. So logged a ticket PDFNEWNET-38818 in our issue tracking system for further investigation and resolution. We will notify you as soon as it is resolved.

We are sorry for the inconvenience caused.

Best Regards,

The issues you have found earlier (filed as PDFNET-38818) have been fixed in Aspose.PDF for .NET 19.3.

I use Aspose.Pdf. 11.6.0,
my code is:

            Page page = (Page)doc.Pages[1];
            var text = new TextFragment("index");
            text.Position = new Position(20, 30);
            Aspose.Pdf.WebHyperlink link = new WebHyperlink("www.google.com");
            text.Hyperlink = link;
            page.Paragraphs.Add(text);

I got the link in my pdf, but the position is not responding to new Position(20, 30); ?

@kristijanpos1

Would you please share your sample PDF document which you are using as input in your code snippet. We will test the scenario in our environment and address it accordingly.

Magenta1Calculator.pdf (2.9 MB)
img.png (221.0 KB)

Here is the pdf, and printscreen of the first page where the hyperlink “index” is. I want to move the positon of the link.

Here is my all code:

      private static bool GenerateCerificatePdf(TrainingActiveParticipatorVIewModel model , string 
      dateInfo)
       {
        var pdfLicence = new Aspose.Pdf.License();
        var asposeLicence = ConfigurationSettings.AppSettings["AsposeLicence"];
        pdfLicence.SetLicense(asposeLicence);
        var date = DateTime.Now;

        var fileLocation = ConfigurationSettings.AppSettings["ConsoleAppDocLocation"];
        var pdfFileStream = new FileStream(fileLocation, FileMode.Open, FileAccess.Read, FileShare.Read);
        var outStream = new MemoryStream();
        try
        {
            var doc = new Aspose.Pdf.Document(pdfFileStream);
            pdfFileStream.Close();
            var pdfForm = new Aspose.Pdf.Facades.Form(doc);
            pdfForm.FillField("Name", model.Name.ToUpper());
            pdfForm.FillField("ISOS", model.NumberISOS.ToUpper());
            pdfForm.FillField("Info", dateInfo);

            //[add hyperlink]
            Page page = (Page)doc.Pages[1];
            var text = new TextFragment("index");
            text.Position = new Position(300, 600);
            Aspose.Pdf.WebHyperlink link = new WebHyperlink("www.google.com");
            text.Hyperlink = link;
            page.Paragraphs.Add(text);
            
            //[end]

            doc.Save(outStream);
            byte[] file = outStream.ToArray();
            
            var contentType = new ContentType(MediaTypeNames.Application.Pdf);
            var reportAttachment = new Attachment(outStream, contentType);

            reportAttachment.ContentDisposition.FileName = "Rafajlovski_Potvrda_Prisustvo.pdf";

            SendMail(reportAttachment,model);
            return true;

        }
        catch (Exception ex)
        {
            LogError(ex.ToString());
            throw;
        }
        finally
        {
            outStream.Close();
            pdfFileStream.Close();
            outStream.Dispose();
            pdfFileStream.Dispose();
        }
    }

@kristijanpos1

Please note that PDF follows a coordinate system where (x,y)=>(0,0) means bottom left. In order to get text over desired position, you can change the values accordingly. Please use following values with Aspose.PDF for .NET 19.7 in order to display text at marked location over PDF page:

text.Position = new Position(470, 490);

outWithLink.pdf (2.9 MB)

I use Aspose.Pdf. 11.6.0, so probably i won’t be able to fix my problem, and i am not able to see the pdf “outWithLink.pdf” i get this error “Sorry, this file is private. Only visible to topic owner and staff members.”

Here is my solution, because position does not work, i move the link with margins. This worked for me.

            Page page = (Page)doc.Pages[1];
            var text = new TextFragment("Линк до вашата Magenta 1");
            text.Position = new Position(300, 300);
            Aspose.Pdf.WebHyperlink link = new WebHyperlink("www.google.com");
            text.Hyperlink = link;
            text.Margin.Left = -48;
            text.Margin.Top = 687;
            text.Margin.Bottom = -150;
            text.TextState.Underline = true;
            text.TextState.FontSize = 11;
            
            text.TextState.ForegroundColor = Aspose.Pdf.Color.DeepSkyBlue;
            text.TextState.BackgroundColor = Aspose.Pdf.Color.White;
            page.Paragraphs.Add(text);

@kristijanpos1

It is good to know that you have managed to resolve your issue by adopting an alternative approach. Please keep using our API and try to use latest version because latest version is always recommended to use as it contains more fixes and improvements. In case of any further assistance, please feel free to let us know.