Hyperlinks
- 3 minutes to read
You can add links to a page and the URI using PDF Graphics that are represented by an instance of the PdfGraphics class.
Note
To access the PdfGraphics class, you need to reference the DevExpress.Pdf.Drawing assembly.
The PdfGraphics class methods (including PdfGraphics.AddLinkToPage and PdfGraphics.AddLinkToUri) use the world coordinate system. The origin (0,0) is at the top left of the page. Positive x increases towards the right and positive y - towards the bottom. See Coordinate Systems to learn more about coordinate systems.
To add a link to a page, call one of the overloaded PdfGraphics.AddLinkToPage methods.
The link to the URI is created using the URI and link area as arguments of the PdfGraphics.AddLinkToUri method.
The coordinates of a hyperlink rectangle is specified in the RectangleF object. The hyperlink rectangle is measured in PDF measurement units (points - 1/72 of an inch).
To render a page with created graphics (see Adding Graphics Content to a Page for more details), call one of the PdfDocumentProcessor.RenderNewPage overloaded method. This method allows you to optimize memory usage (the resources that belong to different pages are not doubled).
The DPI can be passed as dpiX and dpiY parameters to one of the PdfDocumentProcessor.RenderNewPage overloaded methods. The default resolution is 96 dpi.
Example
This example shows how to add a hyperlink to a URI using the PdfGraphics.AddLinkToUri method.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/how-to-add-a-link-to-uri-t495074
Imports System
Imports DevExpress.Pdf
Imports System.Drawing
Namespace AddLinkToUri
Friend Class Program
Shared Sub Main(ByVal args() As String)
Using processor As New PdfDocumentProcessor()
' Create an empty document.
processor.CreateEmptyDocument("..\..\Result.pdf")
' Create and draw graphics.
Using graphics As PdfGraphics = processor.CreateGraphics()
DrawGraphics(graphics)
' Create a link to URI specifying link area and URI.
graphics.AddLinkToUri(New RectangleF(310, 150, 180, 15), New Uri("https://www.devexpress.com"))
' Render a page with graphics.
processor.RenderNewPage(PdfPaperSize.Letter, graphics)
End Using
End Using
End Sub
Private Shared Sub DrawGraphics(ByVal graphics As PdfGraphics)
' Draw a text line on the page.
Using font As New Font("Arial", 10)
Dim blue As SolidBrush = CType(Brushes.Blue, SolidBrush)
graphics.DrawString("https://www.devexpress.com", font, blue, 310, 150)
End Using
End Sub
End Class
End Namespace