Integrating Goto Actions in PDFs with C# and .NET

Integrating Goto Actions in PDFs can significantly enhance the user experience by allowing seamless navigation within the document. This guide will walk you through the process of adding Goto Actions in PDFs using C# and .NET with the help of Sautinsoft SautinSoft.Pdf library.

This integration has several important applications and advantages:

  • Improved Navigation. Goto Actions allow users to easily navigate through a document by navigating to specific pages or sections. This is especially useful for large documents such as books, reports, or technical documentation.
  • Creating interactive documents. With Goto Actions, you can create interactive PDF documents that offer users a more user-friendly and intuitive interface. For example, you can create a table of contents where each entry will be a link to the corresponding page.
  • Improved usability. Users can quickly find the information they need, which increases their satisfaction and reduces the time spent searching for data.
  • Automation of work processes. In business applications and document management systems, Goto Actions can be used to automate transitions between different parts of a document, which simplifies document management and increases productivity.
  • Creating professional documents. Interactive elements such as Goto Actions make documents more professional and modern, which can have a positive impact on the perception of a company or product.
  • Educational materials. In tutorials and textbooks, Goto Actions can be used to create links to additional materials, exercises or answers, which makes the learning process more efficient.

Step-by-Step Guide:

  1. Add SautinSoft.PDF from NuGet.
  2. Create a new PDF document and add a new page.
  3. Add the link annotation.
  4. Add a new page.
  5. Add an action to go to the second page in the link annotation.
  6. Save the PDF document.

Output result:

Полный код

using System;
using System.IO;
using SautinSoft;
using SautinSoft.Pdf;
using SautinSoft.Pdf.Annotations;
using SautinSoft.Pdf.Content;

namespace Sample
{
    class Sample
    {
        /// <summary>
        /// How to add a go-to action in a PDF document.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/add-go-to-action-in-pdf-document.php
        /// </remarks>
        static void Main(string[] args)
        {
            // Before starting this example, please get a free 100-day trial key:
            // https://sautinsoft.com/start-for-free/

            // Apply the key here:
            // PdfDocument.SetLicense("...");

            GoToAction();
        }

        static void GoToAction()
        {
            string outFile = "GoToAction.pdf";
            using (var pdf = new PdfDocument())
            {
                // Add a new page.
                var firstPage = pdf.Pages.Add();
                // Add Link annotation.
                var annotation = firstPage.Annotations.AddLink(10, 700, 300, 100);
                annotation.Appearance.BorderColor = PdfColor.FromRgb(0, 255 / 255, 0);

                annotation.Appearance.BorderDashPattern = PdfLineDashPatterns.Solid;
                annotation.Appearance.BorderWidth = 3;
                annotation.Appearance.BorderStyle = PdfBorderStyle.Solid;

                var text = new PdfFormattedText() { FontSize = 16.0 };
                text.Append("Click to go to the second page!");
                firstPage.Content.DrawText(text, new PdfPoint(50, 750));

                // Add a new page.
                var secondPage = pdf.Pages.Add();
                text = new PdfFormattedText() { FontSize = 72.0, Color = PdfColors.Gray };
                text.Append("Page 2");
                secondPage.Content.DrawText(text, new PdfPoint(150, 400));
                // Add an action to go to the second page in the Link annotation.
                var action = annotation.Actions.AddGoToPageView(secondPage, PdfDestinationViewType.FitPage);
                // Save PDF Document.
                pdf.Save(outFile);
            }
            // Show the result.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
        }
    }
}

Download

Option Infer On

Imports System
Imports System.IO
Imports SautinSoft
Imports SautinSoft.Pdf
Imports SautinSoft.Pdf.Annotations
Imports SautinSoft.Pdf.Content

Namespace Sample
	Friend Class Sample
		''' <summary>
		''' How to add a go-to action in a PDF document.
		''' </summary>
		''' <remarks>
		''' Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/add-go-to-action-in-pdf-document.php
		''' </remarks>
		Shared Sub Main(ByVal args() As String)
			' Before starting this example, please get a free 100-day trial key:
			' https://sautinsoft.com/start-for-free/

			' Apply the key here:
			' PdfDocument.SetLicense("...");

			GoToAction()
		End Sub

		Private Shared Sub GoToAction()
			Dim outFile As String = "GoToAction.pdf"
			Using pdf = New PdfDocument()
				' Add a new page.
				Dim firstPage = pdf.Pages.Add()
				' Add Link annotation.
				Dim annotation = firstPage.Annotations.AddLink(10, 700, 300, 100)
				annotation.Appearance.BorderColor = PdfColor.FromRgb(0, 255 \ 255, 0)

				annotation.Appearance.BorderDashPattern = PdfLineDashPatterns.Solid
				annotation.Appearance.BorderWidth = 3
				annotation.Appearance.BorderStyle = PdfBorderStyle.Solid

				Dim text = New PdfFormattedText() With {.FontSize = 16.0}
				text.Append("Click to go to the second page!")
				firstPage.Content.DrawText(text, New PdfPoint(50, 750))

				' Add a new page.
				Dim secondPage = pdf.Pages.Add()
				text = New PdfFormattedText() With {
					.FontSize = 72.0,
					.Color = PdfColors.Gray
				}
				text.Append("Page 2")
				secondPage.Content.DrawText(text, New PdfPoint(150, 400))
				' Add an action to go to the second page in the Link annotation.
				Dim action = annotation.Actions.AddGoToPageView(secondPage, PdfDestinationViewType.FitPage)
				' Save PDF Document.
				pdf.Save(outFile)
			End Using
			' Show the result.
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})
		End Sub
	End Class
End Namespace

Download


Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу support@sautinsoft.ru или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже:



Вопросы и предложения всегда приветствуются!

Мы разрабатываем компоненты .Net с 2002 года. Мы знаем форматы PDF, DOCX, RTF, HTML, XLSX и Images. Если вам нужна помощь в создании, изменении или преобразовании документов в различных форматах, мы можем вам помочь. Мы напишем для вас любой пример кода абсолютно бесплатно.