We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Adding Hyperlink

Hi, what is the best way to add a text hyperlink. I’ve tried many scenarios but they each have a limitation to some extend. Attached is my sample code and explanation of the issues.Aspose Issues.pdf (262.3 KB)

@henryl

Thank you for contacting support.

Would you please share source and generated PDF documents along with the code snippet in text format so that we can investigate further to help you out. Before sharing requested data, please ensure using Aspose.PDF for .NET 18.8 in your environment.

Moreover, you can embed fonts as explained in Embedding Fonts in an existing PDF file.

Hi Farhan, thank you for your prompt response. I am on V16.1 and will upgrade to the latest one in about a month due to my slow procurement process and the licensing.

Attached is a zip that contains the following:
Source code that starts from MarketingStartUp() method calls two methods, AddText and (AddHyperLink - uses TextFragment or AddHyperlink - uses LinkAnnotation). Can I combine the two methods into one, i.e. add text and link?
Marketing Page Blank.pdf is the source file that needs to be populated.
Final.pdf should be the final results with one block populated
TextFragment.pdf is the results of AddHyperLink() method
LinkAnnotation.pdf is the results of AddHyperlink() method (PS, one method has 'L’ink and one is 'l’ink)AsposeIssues.zip (733.1 KB)

all i need is to have a clean Hyperlink that i can have control over it;s position and font without borders.

Kind regards,
Henry.

@henryl

Thank you for sharing requested resources.

Please modify your AddHyperLink method with below code snippet. You can control position of Hyperlink text with TextFragment.Position property and the font size with TextFragment.TextState.FontSize property.

    public static void AddHyperLink(Document document)
    {
        //Get first page of the document
        var page = document.Pages[1];

        //Specify text for the TextFragment
        TextFragment text = new TextFragment("Click here for more info");

        text.Position = new Position(62,588);

        //Set different properties for the text
        text.TextState.ForegroundColor = Aspose.Pdf.Color.Blue;
        text.TextState.Underline = true;
        text.TextState.FontSize = 12;

        //Set Hyperlink 
        text.Hyperlink = new WebHyperlink("https://www.nedbank.co.za/content/nedbank/desktop/gt/en/personal/save-and-invest/investment-accounts/fixed-deposit-account.html");

        TextBuilder textBuilder = new TextBuilder(page);
        //Append the text fragment to the PDF page
        textBuilder.AppendText(text);
        //Save the document
        document.Save("Hyperlink1_18.8.pdf");
    }

Generated PDF document has also been attached for your kind reference Hyperlink1_18.8.pdf. If some part of your requirements is not fulfilled yet then please elaborate with the help of screenshots or a PDF file as expected output of hyperlink. We will be more than happy to assist you further.

Hi Farhan,

I just tested with the code updates provided and the position is fixed thanks however, the link is not working. Even the link of the PDF you provided is not working. One needs to be able to click on the link and be redirected to the specified URL.

Attached contains the code updates from my side as well as the results produced.

Thank you very much.
Henry.
Aspose.zip (341.7 KB)

@henryl

We have further updated the code snippet and the position as well as the hyperlink is working fine. Please use updated AddHyperLink method in your environment and then share your kind feedback with us.

    public static void AddHyperLink(Document document)
    {
        //Add a page to the document
        var page = document.Pages[1];

        //Specify text for the TextFragment
        TextFragment text = new TextFragment("Click here for more info");

        text.Position = new Position(62, 588);

        //Set different properties for the text
        text.TextState.ForegroundColor = Aspose.Pdf.Color.Blue;
        text.TextState.Underline = true;
        text.TextState.FontSize = 10;
        text.TextState.Font = FontRepository.OpenFont( dataDir + @"MarkPro-Light.ttf");

        LinkAnnotation link = new LinkAnnotation(page, text.Rectangle);
        // Create border object for LinkAnnotation
        Border border = new Border(link);
        // Set the border width value as 0
        border.Width = 0;
        // Set the border for LinkAnnotation
        link.Border = border;
        // Specify the link type as remote URI
        link.Action = new GoToURIAction("www.aspose.com");

        // Add link annotation to annotations collection of first page of PDF file
        page.Annotations.Add(link);

        TextBuilder textBuilder = new TextBuilder(page);
        ////Append the text fragment to the PDF page
        textBuilder.AppendText(text);
        //Save the document
        document.Save(@"Hyperlink_18.8.pdf");

    }

We have also attached generated PDF file for your kind reference Hyperlink_18.8.pdf. We hope this will be helpful. Please feel free to contact us if you need any further assistance.

@Farhan.Raza, thank you very much. This is exactly what I am looking for. Great product and exceptional assistance. Thank you.

Hyperlink3.pdf (196.2 KB)

1 Like