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

Links in PDF Documents

  • 4 minutes to read

PDF Graphics allows you to create and remove link annotations (hypertext links to a URI or document page).

Use the PdfGraphics.AddLinkToUri or PdfGraphics.AddLinkToPage method to create a link to a URI or document page, respectively. These methods use the world coordinate system. Pass a RectangleF object to these methods to specify where a link should appear on the page. Define the page area in points (1/72 of an inch).

Note

You need a reference to the DevExpress.Pdf.Drawing assembly to access the PdfGraphics class.

After you create a link at the specified page area, call one of the following methods to draw graphics content on a page:

PdfGraphics.AddToPageForeground, PdfGraphics.AddToPageBackground
These methods allow you to draw content on an existing page.
PdfDocumentProcessor.RenderNewPage
Draws content on a new page.

The code sample below demonstrates how to use the PdfGraphics.AddLinkToUri method to create a link to a URI.

Add a Link to the Uri

View Example

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

namespace AddLinkToUri
{
    class Program
    {
        static void Main(string[] args)
        {

            using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
            {
                // Create an empty document.
                processor.CreateEmptyDocument("..\\..\\Result.pdf");

                // Create and draw graphics.
                using (PdfGraphics graphics = processor.CreateGraphics())
                {
                    using (Font font = new Font("Arial", 34))
                    {
                        SolidBrush blue = (SolidBrush)Brushes.Blue;

                        // Calculate the link size
                        SizeF stringSize = graphics.MeasureString("https://www.devexpress.com", font);
                        RectangleF stringRect = new RectangleF(100, 150, stringSize.Width, stringSize.Height);

                        // Draw a link text. 
                        graphics.DrawString("https://www.devexpress.com", font, blue, stringRect);

                        // Create a link to a URI at the specified page area.
                        graphics.AddLinkToUri(stringRect, new Uri("https://www.devexpress.com"));
                    }

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

        }
    }
}

The code sample below demonstrates how to use the PdfGraphics.AddLinkToPage method to create a link to a page.

View Example

using DevExpress.Pdf;
using System.Drawing;


namespace AddLinkToPage
{
    class Program
    {
        static void Main(string[] args)
        {            

            using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
            {
                // Create an empty document.
                processor.CreateEmptyDocument("..\\..\\Result.pdf");

                // Create a PdfGraphics class instance.
                using (PdfGraphics graphics = processor.CreateGraphics())
                {
                    // Draw link text.
                    SolidBrush black = (SolidBrush)Brushes.Black;
                    using (Font font = new Font("Times New Roman", 32, FontStyle.Bold))
                    {
                        graphics.DrawString("PDF Document API", font, black, 180, 150);
                    }

                    // Create a link to the second document page.
                    // Specify the page destination to which the link should refer.
                    graphics.AddLinkToPage(new RectangleF(180, 160, 480, 30), 2, 168, 230);

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

                    // Create the second document page to which the link navigates.
                    processor.AddNewPage(PdfPaperSize.A4);
                }
            }
        }
    }
}
See Also