Link annotations represent either hypertext links to other locations in the document or actions to be performed. It is represented by the PdfLinkAnnotation class.
With SautinSoft.Pdf, you can add hyperlinks to PDF in your C# or VB.NET application.
The following example shows how to add link annotations with different actions to a PDF page.
Полный код
using System;
using System.IO;
using SautinSoft;
using SautinSoft.Pdf;
using SautinSoft.Pdf.Content;
namespace Sample
{
class Sample
{
/// <summary>
/// Add hyperlinks to PDF files.
/// </summary>
/// <remarks>
/// Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/hyperlinks.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("...");
string pdfFile = Path.GetFullPath(@"..\..\..\simple text.pdf");
using (var document = new PdfDocument())
{
var page = document.Pages.Add();
var secondPage = document.Pages.Add();
var pageWidth = page.Size.Width;
using (var formattedText = new PdfFormattedText())
{
formattedText.FontSize = 24;
formattedText.Append("First page");
double y = 700;
var origin = new PdfPoint((pageWidth - formattedText.Width) / 2, y);
page.Content.DrawText(formattedText, origin);
var image = PdfImage.Load("..\\..\\..\\SautinSoft.png");
y -= image.Size.Height + 100;
origin = new PdfPoint((pageWidth - image.Size.Width) / 2, y);
page.Content.DrawImage(image, origin);
// Add a link annotation over the drawn image that opens a website.
var link = page.Annotations.AddLink(origin.X, origin.Y, image.Size.Width, image.Size.Height);
link.Actions.AddOpenWebLink("https://sautinsoft.com/");
formattedText.Clear();
formattedText.Append("Open file");
y -= formattedText.Height + 100;
origin = new PdfPoint((pageWidth - formattedText.Width) / 2, y);
page.Content.DrawText(formattedText, origin);
// Add a link annotation over the drawn text that opens a file.
link = page.Annotations.AddLink(origin.X, origin.Y, formattedText.Width, formattedText.Height);
link.Actions.AddOpenFile(pdfFile);
formattedText.Clear();
formattedText.Append("Go to second page");
y -= formattedText.Height + 100;
origin = new PdfPoint((pageWidth - formattedText.Width) / 2, y);
page.Content.DrawText(formattedText, origin);
// Add a link annotation over the drawn text that goes to the second page.
link = page.Annotations.AddLink(origin.X, origin.Y, formattedText.Width, formattedText.Height);
link.Actions.AddGoToPageView(secondPage, PdfDestinationViewType.FitPage);
formattedText.Clear();
formattedText.Append("Second page");
origin = new PdfPoint((pageWidth - formattedText.Width) / 2, 700);
secondPage.Content.DrawText(formattedText, origin);
}
document.Save("Hyperlinks.pdf");
}
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("Hyperlinks.pdf") { UseShellExecute = true });
}
}
}
Option Infer On
Imports System
Imports System.IO
Imports SautinSoft
Imports SautinSoft.Pdf
Imports SautinSoft.Pdf.Content
Namespace Sample
Friend Class Sample
''' <summary>
''' Add hyperlinks to PDF files.
''' </summary>
''' <remarks>
''' Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/hyperlinks.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("...");
Dim pdfFile As String = Path.GetFullPath("..\..\..\simple text.pdf")
Using document = New PdfDocument()
Dim page = document.Pages.Add()
Dim secondPage = document.Pages.Add()
Dim pageWidth = page.Size.Width
Using formattedText = New PdfFormattedText()
formattedText.FontSize = 24
formattedText.Append("First page")
Dim y As Double = 700
Dim origin = New PdfPoint((pageWidth - formattedText.Width) \ 2, y)
page.Content.DrawText(formattedText, origin)
Dim image = PdfImage.Load("..\..\..\SautinSoft.png")
y -= image.Size.Height + 100
origin = New PdfPoint((pageWidth - image.Size.Width) \ 2, y)
page.Content.DrawImage(image, origin)
' Add a link annotation over the drawn image that opens a website.
Dim link = page.Annotations.AddLink(origin.X, origin.Y, image.Size.Width, image.Size.Height)
link.Actions.AddOpenWebLink("https://sautinsoft.com/")
formattedText.Clear()
formattedText.Append("Open file")
y -= formattedText.Height + 100
origin = New PdfPoint((pageWidth - formattedText.Width) \ 2, y)
page.Content.DrawText(formattedText, origin)
' Add a link annotation over the drawn text that opens a file.
link = page.Annotations.AddLink(origin.X, origin.Y, formattedText.Width, formattedText.Height)
link.Actions.AddOpenFile(pdfFile)
formattedText.Clear()
formattedText.Append("Go to second page")
y -= formattedText.Height + 100
origin = New PdfPoint((pageWidth - formattedText.Width) \ 2, y)
page.Content.DrawText(formattedText, origin)
' Add a link annotation over the drawn text that goes to the second page.
link = page.Annotations.AddLink(origin.X, origin.Y, formattedText.Width, formattedText.Height)
link.Actions.AddGoToPageView(secondPage, PdfDestinationViewType.FitPage)
formattedText.Clear()
formattedText.Append("Second page")
origin = New PdfPoint((pageWidth - formattedText.Width) \ 2, 700)
secondPage.Content.DrawText(formattedText, origin)
End Using
document.Save("Hyperlinks.pdf")
End Using
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("Hyperlinks.pdf") With {.UseShellExecute = True})
End Sub
End Class
End Namespace
Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу support@sautinsoft.com или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже: