Skip to main content
A newer version of this page is available. .

Hyperlinks

  • 4 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.

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

Tip

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=T244516.

This example shows how to create a document with graphics and add a hyperlink to a page and URI using the PDF Document Creation API. To accomplish this task:

using DevExpress.Pdf;
using System;
using System.Drawing;

namespace DocumentCreationAPI {
    class Program {

        static void Main(string[] args) {

            using (PdfDocumentProcessor processor = new PdfDocumentProcessor()) {

                // Create an empty document.
                processor.CreateEmptyDocument("..\\..\\Result.pdf");

                // Create and draw PDF graphics.
                using (PdfGraphics graph = processor.CreateGraphics()) {
                    DrawGraphics(graph);

                    // Create a link to a page specifying link area, the page number and X, Y destinations.
                    graph.AddLinkToPage(new Rectangle(180, 160, 480, 30), 1, 168, 230);

                    // Create a link to URI specifying link area and URI.  
                    graph.AddLinkToUri(new Rectangle(110, 350, 180, 15), new Uri("https://www.devexpress.com"));

                    // Render a page with graphics.
                    processor.RenderNewPage(PdfPaperSize.Letter, graph);
                }
            }
        }

        static void DrawGraphics(PdfGraphics graph) {

            // Draw text lines on the page. 
            SolidBrush black = (SolidBrush)Brushes.Black;
            using (Font font1 = new Font("Times New Roman", 32, FontStyle.Bold)) {
                graph.DrawString("PDF Document Processor", font1, black, 180, 150);
            }
            using (Font font2 = new Font("Arial", 20)) {
                graph.DrawString("Display, Print and Export PDF Documents", font2, black, 168, 230);
            }
            using (Font font3 = new Font("Arial", 10)) {
                graph.DrawString("The PDF Document Processor is a non-visual component " +
                                  "that provides the application programming interface of the PDF Viewer.", font3, black, 16, 300);
                graph.DrawString("Learn more at", font3, black, 20, 350);
                SolidBrush blue = (SolidBrush)Brushes.Blue;
                graph.DrawString("https://www.devexpress.com", font3, blue, 110, 350);
            }
        }
    }
}
See Also