Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Thu Dec 29, 2016 6:44 am

I would like to find strings in a pdf file and add a hyperlink to the strings.
The function is just like "Hyperlinks.Add" of VBA.
I would like to insert a hyperlink into a pdf file.
Can I insert a hyperlink with Spire.PDF?

[email protected]
 
Posts: 2
Joined: Thu Dec 29, 2016 6:13 am

Thu Dec 29, 2016 8:00 am

Hi,

Thanks for your inqury.
Spire.PDF supports to insert hyperlink, here is a guide for your reference.
https://www.e-iceblue.com/Tutorials/Spi ... B.NET.html
But by investigation, I found that there is little issue about finding strings and then adding hyperlink to the string, and I have posted the issue to our Dev team. Once it is fixed, we will inform you and provide you corresponding code soon.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Fri Dec 30, 2016 8:43 am

Hi,

By investigation, we found there is a workaround which can meet your requirement.
Here is sample code for your kind reference.
Code: Select all
            PdfDocument pdf = new PdfDocument(@"F:\testing\9439.pdf");
            PdfTextFind[] result = null;
            foreach (PdfPageBase page in pdf.Pages)
            {
                result = page.FindText("Hello").Finds;
                foreach (PdfTextFind find in result)
                {
                  PointF location = find.Position;
                  //cover the original text
                  page.Canvas.DrawRectangle(PdfBrushes.White, location.X, location.Y, find.Size.Width, find.Size.Height);
                  //draw the hyperlink to the text             
                  PdfTextWebLink link = new PdfTextWebLink();
                  link.Font = new PdfTrueTypeFont(new Font("Arial", 10f));
                  link.Brush = PdfBrushes.DarkSeaGreen;
                  link.Text = find.MatchText;
                  link.Url = "www.e-iceblue.com";
                  link.DrawTextWebLink(page.Canvas, location);
                }
            }
            pdf.SaveToFile("9439.pdf");

If there is any question, welcome to get it back to us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Fri Jan 06, 2017 12:47 am

Hello,

Can you show me the VB.NET code, not C# code?
I'm not familiar with C#.

Best Regards,

[email protected]
 
Posts: 2
Joined: Thu Dec 29, 2016 6:13 am

Fri Jan 06, 2017 2:18 am

Hi,

Thanks for the response.
Here is VB code for your kind reference.
Code: Select all
    Public Shared Sub FindTextAndHyperlink9439()
        Dim pdf As New PdfDocument("F:\testing\pdf form testing\original document\9439.pdf")
        Dim result As PdfTextFind() = Nothing
        For Each page As PdfPageBase In pdf.Pages
            result = page.FindText("Hello").Finds
            For Each find As PdfTextFind In result
                Dim location As PointF = find.Position
                'cover the original text
                page.Canvas.DrawRectangle(PdfBrushes.White, location.X, location.Y, find.Size.Width, find.Size.Height)
                'draw the hyperlink to the text             
                Dim link As New PdfTextWebLink()
                link.Font = New PdfTrueTypeFont(New Font("Arial", 10.0F))
                link.Brush = PdfBrushes.DarkSeaGreen
                link.Text = find.MatchText
                link.Url = "www.e-iceblue.com"
                link.DrawTextWebLink(page.Canvas, location)
            Next
        Next
        pdf.SaveToFile("9439.pdf")
    End Sub

If there is any question, welcome to get it back to us.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Tue Jan 10, 2017 8:24 am

Hi,

Did you test the VB code ? Did it meet your requirement ?
Could you please give us some feedback ?

Thanks,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Return to Spire.PDF