超鏈接可以實現不同元素之間的連接,用戶可以通過點擊被鏈接的元素來激活這些鏈接。具有高效、快捷、准確的特點。本文中,將分享通過C#編程在PDF文檔中插入超鏈接的方法。內容包含以下要點:
- 插入網頁鏈接
- 插入外部文檔鏈接
- 插入文檔頁面跳轉鏈接
工具
下載安裝后,注意將Spire.Pdf.dll引用到程序(dll文件可在安裝路徑下的Bin文件夾中獲取)
示例代碼(供參考)
【示例1】插入網頁鏈接
步驟 1:創建實例,並添加頁
PdfDocument pdf = new PdfDocument(); PdfPageBase page = pdf.Pages.Add();
步驟 2:定義坐標變量
float x = 10; float y = 50;
步驟 3:創建字體1,並添加文本到頁面
//創建字體1 PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Regular), true); //添加文本到頁面 string text = "注:\n本文主要數據來源參考自WTO,查看原文請點擊:"; page.Canvas.DrawString(text, font1, PdfBrushes.Black, new PointF(x, y)); PdfStringFormat format = new PdfStringFormat(); format.MeasureTrailingSpaces = true; x = x + font1.MeasureString(text, format).Width;
步驟 4:創建字體2 ,添加超鏈接文本,並設置格式
//創建字體2 PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial Unicode MS",12f, FontStyle.Underline), true); //創建PdfTextWebLink對象 PdfTextWebLink webLink = new PdfTextWebLink(); //設置超鏈接地址 webLink.Url = "https://www.wto.org/"; //設置超鏈接文本 webLink.Text = "WTO Official Website"; //設置超鏈接字體和字體顏色 webLink.Font = font2; webLink.Brush = PdfBrushes.Blue;
步驟 5 :添加超鏈接到頁面,並保存文檔
//添加超鏈接到頁面 webLink.DrawTextWebLink(page.Canvas, new PointF(x, y+15)); //保存文檔 pdf.SaveToFile("WebLink.pdf");
網頁鏈接效果:
全部代碼:

using Spire.Pdf; using Spire.Pdf.Annotations; using Spire.Pdf.Graphics; using System.Drawing; namespace Weblink { class Program { static void Main(string[] args) { //創建PDF文檔並添加一頁 PdfDocument pdf = new PdfDocument(); PdfPageBase page = pdf.Pages.Add(); //定義坐標變量並賦初值 float x = 10; float y = 50; //創建字體 PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Regular), true); //添加文本到頁面 string text = "注:\n本文主要數據來源參考自WTO,查看原文請點擊:"; page.Canvas.DrawString(text, font1, PdfBrushes.Black, new PointF(x, y)); PdfStringFormat format = new PdfStringFormat(); format.MeasureTrailingSpaces = true; x = x + font1.MeasureString(text, format).Width; //創建字體 PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Underline), true); //創建PdfTextWebLink對象 PdfTextWebLink webLink = new PdfTextWebLink(); //設置超鏈接地址 webLink.Url = "https://www.wto.org/"; //設置超鏈接文本 webLink.Text = "WTO Official Website"; //設置超鏈接字體和字體顏色 webLink.Font = font2; webLink.Brush = PdfBrushes.Blue; //添加超鏈接到頁面 webLink.DrawTextWebLink(page.Canvas, new PointF(x, y+15)); //保存文檔 pdf.SaveToFile("WebLink.pdf"); System.Diagnostics.Process.Start("Weblink.pdf"); } } }
【示例2】鏈接到外部文檔
步驟 1:創建實例,並添加頁
PdfDocument document = new PdfDocument(); PdfPageBase page = document.Pages.Add();
步驟 2:創建字體,並繪制超鏈接文本
//創建字體 PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 15f, FontStyle.Regular), true); //添加超鏈接文本 string text = "Clik and View the Original Document"; //創建RectangleF對象並添加文本 RectangleF rectangle = new RectangleF(20, 40, 300,40); page.Canvas.DrawString(text, font, PdfBrushes.SteelBlue, rectangle); //創建PdfFileLinkAnnotation對象 PdfFileLinkAnnotation fileLink = new PdfFileLinkAnnotation(rectangle, @"sample.docx"); //設置超鏈接邊框顏色 fileLink.Color = Color.White;
步驟 3 :添加超鏈接到頁面,並保存文檔
//添加超鏈接到頁面 page.AnnotationsWidget.Add(fileLink); //保存並打開文檔 document.SaveToFile("ExternalFileLink.pdf");
外部文檔連接效果:
全部代碼:

using Spire.Pdf; using Spire.Pdf.Annotations; using Spire.Pdf.Graphics; using System.Drawing; namespace Filelink { class Program { static void Main(string[] args) { //創建PDF文檔並添加一頁 PdfDocument document = new PdfDocument(); PdfPageBase page = document.Pages.Add(); //創建字體 PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 15f, FontStyle.Regular), true); //添加超鏈接文本 string text = "Clik and View the Original Document"; //創建RectangleF對象並添加文本 RectangleF rectangle = new RectangleF(20, 40, 300,40); page.Canvas.DrawString(text, font, PdfBrushes.SteelBlue, rectangle); //創建PdfFileLinkAnnotation對象 PdfFileLinkAnnotation fileLink = new PdfFileLinkAnnotation(rectangle, @"sample.docx"); //設置超鏈接邊框顏色 fileLink.Color = Color.White; //添加超鏈接到頁面 page.AnnotationsWidget.Add(fileLink); //保存並打開文檔 document.SaveToFile("ExternalFileLink.pdf"); System.Diagnostics.Process.Start("ExternalFileLink.pdf"); } } }
【示例3】插入文檔頁面跳轉鏈接
步驟 1 :創建文檔,並添加3頁
PdfDocument pdf = new PdfDocument(); PdfPageBase page1 = pdf.Pages.Add(); PdfPageBase page2 = pdf.Pages.Add(); PdfPageBase page3 = pdf.Pages.Add();
步驟 2:創建字體,添加文本到頁面
//創建字體 PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Regular), true); //添加文本到頁面 page1.Canvas.DrawString("(首頁)", font, PdfBrushes.Black, new PointF(20, 20)); page2.Canvas.DrawString("(第二頁)", font, PdfBrushes.Black, new PointF(20, 20)); page3.Canvas.DrawString("(第三頁)", font, PdfBrushes.Black, new PointF(20, 20)); //創建超鏈接文本 string text = "點擊跳轉至最后一頁"; //創建RectangleF對象並添加文本 RectangleF rectangle = new RectangleF(40, 50, 900, 20); page1.Canvas.DrawString(text, font, PdfBrushes.SteelBlue, rectangle); //創建PdfDocumentLinkAnnotation對象 PdfDocumentLinkAnnotation documentLink = new PdfDocumentLinkAnnotation(rectangle, new PdfDestination(page3)); //設置邊框顏色 documentLink.Color = Color.White;
步驟 3: 添加超鏈接到頁面並保存文檔
//添加超鏈接到第一頁 page1.AnnotationsWidget.Add(documentLink); //保存文檔 pdf.SaveToFile("InternalFileLink.pdf");
頁面跳轉鏈接效果:
全部代碼:

using Spire.Pdf; using Spire.Pdf.Annotations; using Spire.Pdf.General; using Spire.Pdf.Graphics; using System.Drawing; namespace Documentlink { class Program { static void Main(string[] args) { //創建PDF文檔並添加3頁 PdfDocument pdf = new PdfDocument(); PdfPageBase page1 = pdf.Pages.Add(); PdfPageBase page2 = pdf.Pages.Add(); PdfPageBase page3 = pdf.Pages.Add(); //創建字體 PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Regular), true); //添加文本到頁面 page1.Canvas.DrawString("(首頁)", font, PdfBrushes.Black, new PointF(20, 20)); page2.Canvas.DrawString("(第二頁)", font, PdfBrushes.Black, new PointF(20, 20)); page3.Canvas.DrawString("(第三頁)", font, PdfBrushes.Black, new PointF(20, 20)); //創建超鏈接文本 string text = "點擊跳轉至最后一頁"; //創建RectangleF對象並添加文本 RectangleF rectangle = new RectangleF(40, 50, 900, 20); page1.Canvas.DrawString(text, font, PdfBrushes.SteelBlue, rectangle); //創建PdfDocumentLinkAnnotation對象 PdfDocumentLinkAnnotation documentLink = new PdfDocumentLinkAnnotation(rectangle, new PdfDestination(page3)); //設置邊框顏色 documentLink.Color = Color.White; //添加超鏈接到第一頁 page1.AnnotationsWidget.Add(documentLink); //保存文檔並打開 pdf.SaveToFile("InternalFileLink.pdf"); System.Diagnostics.Process.Start("InternalFileLink.pdf"); } } }
(本文完)
轉載請注明出處。