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?
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"); 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