Example_22

Adding "Destinations" and "Go To" actions to text, a box and an image, in a PDF/UA document.

Example_22.pdf — the PDF this example creates.

/*
 * Example_22.cs
 *
 * Copyright (c) 2026 PDFjet Software
 * Licensed under the MIT License. See LICENSE file in the project root.
 */
using System;
using System.IO;
using System.Diagnostics;
using PDFjet.NET;

/**
 * Example_22.cs
 */
public class Example_22 {
    public Example_22() {
        PDF pdf = new PDF(new BufferedStream(
            new FileStream("Example_22.pdf", FileMode.Create)));
        pdf.SetCompliance(Compliance.PDF_UA_1);
        pdf.SetTitle("Internal links and destinations");

        Font f1 = new Font(pdf, IBMPlexSans.Regular);

        Page page = new Page(pdf, Letter.PORTRAIT);
        TextLine text = new TextLine(f1, "Page #1 -> Go to Destination #3.");
        text.SetGoToAction("dest#3");
        text.SetLocation(90f, 50f);
        page.AddDestination("dest#1", 0f, 0f);
        text.DrawOn(page);

        page = new Page(pdf, Letter.PORTRAIT);
        text = new TextLine(f1, "Page #2 -> Go to Destination #3.");
        text.SetGoToAction("dest#3");
        text.SetDestination("dest#2");
        text.SetLocation(90f, 550f);
        text.DrawOn(page);

        page = new Page(pdf, Letter.PORTRAIT);
        text = new TextLine(f1, "Page #3 -> Go to Destination #4.");
        text.SetGoToAction("dest#4");
        text.SetDestination("dest#3");
        text.SetLocation(90f, 700f);
        text.DrawOn(page);

        page = new Page(pdf, Letter.PORTRAIT);
        text = new TextLine(f1, "Page #4 -> Go to Destination #1.");
        text.SetGoToAction("dest#1");
        text.SetDestination("dest#4");
        text.SetLocation(90f, 100f);
        text.DrawOn(page);

        text = new TextLine(f1, "Page #4 -> Go to Destination #2.");
        text.SetGoToAction("dest#2");
        text.SetLocation(90f, 200f);
        text.DrawOn(page);

        // Create a rect with no border that links to destination #1
        Rect rect = new Rect(20f, 20f, 20f, 20f);
        rect.SetGoToAction("dest#1");
        rect.DrawOn(page);

        // Create an up arrow and place it in the rect
        PDFjet.NET.Path path = new PDFjet.NET.Path();
        path.Add(new Point(10f,  1f));
        path.Add(new Point(17f,  9f));
        path.Add(new Point(13f,  9f));
        path.Add(new Point(13f, 19f));
        path.Add(new Point( 7f, 19f));
        path.Add(new Point( 7f,  9f));
        path.Add(new Point( 3f,  9f));
        path.SetClosed(true);
        path.SetStrokeColor(Color.oldgloryblue);
        path.SetStrokeColor(Color.deepskyblue);
        path.SetFillShape(true);
        path.DrawOn(page);

        Image image = new Image(pdf, "images/up-arrow.png");
        image.SetLocation(40f, 40f);
        image.SetGoToAction("dest#1");
        image.DrawOn(page);

        pdf.Complete();
    }

    public static void Main(String[] args) {
        Stopwatch sw = Stopwatch.StartNew();
        long time0 = sw.ElapsedMilliseconds;
        new Example_22();
        long time1 = sw.ElapsedMilliseconds;
        sw.Stop();
        Console.WriteLine($"Example_22 => {time1 - time0,4} ms");
    }
}   // End of Example_22.cs

©2026 PDFjet Software.