Skip to main content

Links in PDF Documents

  • 5 minutes to read

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

Warning

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

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).

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.

Note

Coordinate system transformations (e.g., system rotation) are not taken into account when the PdfGraphics.AddLinkToUri or PdfGraphics.AddLinkToPage method is called.

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 System;
using DevExpress.Pdf;
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()) {
                    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);
                }
            }
        }

        static void DrawGraphics(PdfGraphics graphics) {

            // Draw a text line on the page. 
            using (Font font = new Font("Arial", 10)) {
                SolidBrush blue = (SolidBrush)Brushes.Blue;
                graphics.DrawString("https://www.devexpress.com", font, blue, 310, 150);
            }
        }
    }
}

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 and draw graphics.
                using (PdfGraphics graphics = processor.CreateGraphics()) {
                    DrawGraphics(graphics);

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

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

        static void DrawGraphics(PdfGraphics graphics) {

            // Draw a text line on the page. 
            SolidBrush black = (SolidBrush)Brushes.Black;
            using (Font font = new Font("Times New Roman", 32, FontStyle.Bold)) {
                graphics.DrawString("PDF Document Processor", font, black, 180, 150);
            }
        }
    }
}

The PdfPageFacade.ClearContent method overloads clear document content at the specified page areas. You can pass PdfClearContentOptions to these methods to specify what content type to keep.

The following code sample demonstrates how to remove only link annotations from the first document page:

using DevExpress.Pdf;
//...

using (PdfDocumentProcessor processor = new PdfDocumentProcessor())
{
    //...

    // Specify the page area to clear.
    PdfRectangle cropBox = processor.Document.Pages[0].CropBox;

    // Specify clear options.
    PdfClearContentOptions options = new PdfClearContentOptions();

    // Remove annotations at the specified page area.
    options.ClearAnnotations = true;

    // Keep text, graphic, and image content of the specified page area.
    options.ClearText = false;
    options.ClearGraphics = false;
    options.ClearImages = false;

    // Clear the page area.
    processor.DocumentFacade.Pages[0].ClearContent(cropBox, options);
}
See Also