轉帖|使用教程|編輯:李顯亮|2019-08-15 10:26:45.543|閱讀 812 次
概述:Spire.PDF允許用戶在PDF文檔中添加超鏈接來鏈接到外部網頁、文檔內部的指定頁面或是某些外部文檔。以下示例將介紹如何使用Spire.PDF組件和C#添加這三種超鏈接到PDF文檔。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
更多資源查看:Spire.XLS工作表教程 | Spire.Doc系列教程 | Spire.PDF系列教程
Spire.PDF是一個專業的PDF組件,能夠獨立地創建、編寫、編輯、操作和閱讀PDF文件,支持 .NET、Java、WPF和Silverlight。Spire.PDF的PDF API擁有豐富的功能,如安全設置(包括數字簽名)、PDF文本/附件/圖片提取、PDF文件合并/拆分、元數據更新、章節和段落優化、圖形/圖像描繪和插入、表格創建和處理、數據導入等等。>>下載Spire.PDF最新試用版
Spire.PDF允許用戶在PDF文檔中添加超鏈接來鏈接到外部網頁、文檔內部的指定頁面或是某些外部文檔。以下示例將介紹如何使用Spire.PDF組件和C#添加這三種超鏈接到PDF文檔。
鏈接到外部網頁
//創建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 = "更多詳情請訪問E-iceblue官方網站: "; 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 = "//www.e-iceblue.cn/"; //設置超鏈接文本 webLink.Text = "www.e-iceblue.cn"; //設置超鏈接字體和字體顏色 webLink.Font = font2; webLink.Brush = PdfBrushes.Blue; //添加超鏈接到頁面 webLink.DrawTextWebLink(page.Canvas, new PointF(x, y)); //保存文檔 pdf.SaveToFile("WebLink.pdf");
鏈接到文檔內部的指定頁面
//創建PDF文檔并添加兩頁 PdfDocument pdf = new PdfDocument(); PdfPageBase page1 = pdf.Pages.Add(); PdfPageBase page2 = 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)); string text = "跳轉到第二頁"; //創建RectangleF對象并添加文本 RectangleF rectangle = new RectangleF(20, 80, 70, 20); page1.Canvas.DrawString(text, font, PdfBrushes.ForestGreen, rectangle); //創建PdfDocumentLinkAnnotation對象 PdfDocumentLinkAnnotation documentLink = new PdfDocumentLinkAnnotation(rectangle, new PdfDestination(page2)); //設置邊框顏色 documentLink.Color = Color.DarkSeaGreen; //添加超鏈接到第一頁 page1.AnnotationsWidget.Add(documentLink); //保存文檔 pdf.SaveToFile("InternalFileLink.pdf");
鏈接到外部文檔
//創建PDF文檔并添加一頁 PdfDocument document = new PdfDocument(); PdfPageBase page = document.Pages.Add(); //創建字體 PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 12f, FontStyle.Regular), true); string text = "打開1.jpg"; //創建RectangleF對象并添加文本 RectangleF rectangle = new RectangleF(20, 40, 80, 20); page.Canvas.DrawString(text, font, PdfBrushes.ForestGreen, rectangle); //創建PdfFileLinkAnnotation對象 PdfFileLinkAnnotation fileLink = new PdfFileLinkAnnotation(rectangle, @"1.jpg"); //設置超鏈接邊框顏色 fileLink.Color = Color.DarkSeaGreen; //添加超鏈接到頁面 page.AnnotationsWidget.Add(fileLink); //保存文檔 document.SaveToFile("ExternalFileLink.pdf");
前面我們介紹了如何使用Spire.PDF組件添加超鏈接到PDF文檔,接下來將詳細介紹如何使用C#更新和刪除PDF文檔中的超鏈接。
//加載PDF示例文檔 PdfDocument document = new PdfDocument(); document.LoadFromFile("PDFLink.pdf"); //獲取第一頁 PdfPageBase page = document.Pages[0]; //獲取所有的PDF 超鏈接集合 PdfAnnotationCollection widgetCollection = page.AnnotationsWidget; //更新第一個超鏈接Target link PdfUriAnnotationWidget uri = widgetCollection[0] as PdfUriAnnotationWidget; uri.Uri = "//www.e-iceblue.cn/Introduce/Spire-PDF-NET.html"; //刪除第二個超鏈接 widgetCollection.RemoveAt(1); //保存文檔 document.SaveToFile("Result.pdf");
程序運行后,第一個超鏈接的網址被修改,第二個超鏈接被刪除,但是字體樣式依然存在:
如果你有任何問題或意見,可在下方評論區留言,點擊資源列表查看更多教程資源~
*想要購買正版授權的朋友可以哦~
掃描關注“慧聚IT”微信公眾號,及時獲取更多產品最新動態及最新資訊
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn