C# 插入超連結到PDF文件(3種情況)
阿新 • • 發佈:2018-12-17
超連結可以實現不同元素之間的連線,使用者可以通過點選被連結的元素來啟用這些連結。具有高效、快捷、準確的特點。本文中,將分享通過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"); } } }View Code
【示例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"); } } }View Code
【示例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"); } } }View Code
(本文完)
轉載請註明出處。