本文介紹通過C# 編程如何在PPT幻燈片中添加超鏈接的方法,添加鏈接時,可給文本或者圖片添加超鏈接,鏈接物件可指向網頁地址、郵件地址、指定幻燈片等,此外,也可以參考文中編輯、洗掉幻燈片中已有超鏈接的方法,
程式使用類別庫:Free Spire.Presentation for .NET (免費版)
dll獲取及參考:
方法1:可通過官網下載包,解壓將Bin檔案夾下的程式安裝到指定路徑;完成安裝后,將安裝路徑下Bin檔案夾中的Spire.Presentation.dll檔案添加參考到程式,并添加using指令,
方法2:可通過Nuget安裝匯入,
Dll添加參考效果如下圖:

C# 代碼示例
1. 添加超鏈接到PPT幻燈片
using Spire.Presentation; using Spire.Presentation.Drawing; using System.Drawing; namespace AddHyperlink { class Program { static void Main(string[] args) { //初始化Presentation實體 Presentation ppt = new Presentation(); //添加一張幻燈片作為第二張幻燈片(創建檔案時,已默認生成一頁幻燈片) ppt.Slides.Append(); //獲取第1張幻燈片,并添加形狀 ISlide slide1 = ppt.Slides[0]; IAutoShape shape = slide1.Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(100, 100, 450,200)); shape.Fill.FillType = FillFormatType.Solid; shape.Fill.SolidColor.Color = Color.LightYellow; shape.ShapeStyle.LineColor.Color = Color.White; //宣告字串變數 string s1 = "BIDU"; string s2 = "是全球最大的中文搜索引擎,中國最大的以資訊和知識為核心的互聯網綜合服務公司,全球領先的人工智能平臺型公司,"; string s3 = "詳見第二頁內容介紹"; //獲取形狀段落(默認有一個空白段落) TextParagraph paragraph = shape.TextFrame.TextRange.Paragraph; paragraph.Alignment = TextAlignmentType.Left; //根據字串s1創建tr1,并在文字上添加鏈接,指向網頁地址 TextRange tr1 = new TextRange(s1); tr1.ClickAction.Address = "https://www.baidu.com/"; //tr1.ClickAction.Address = "mailto:[email protected]";//指向郵件地址 //根據字s2創建tr2 TextRange tr2 = new TextRange(s2); //根據字串s3創建tr3,并在文字上添加鏈接,指向第二張幻燈片 TextRange tr3 = new TextRange(s3); ClickHyperlink link = new ClickHyperlink(ppt.Slides[1]); tr3.ClickAction = link; //將TextRange添加到段落 paragraph.TextRanges.Append(tr1); paragraph.TextRanges.Append(tr2); paragraph.TextRanges.Append(tr3); //設定段落的字體樣式 foreach (TextRange tr in paragraph.TextRanges) { tr.LatinFont = new TextFont("宋體 (Body)"); tr.FontHeight = 20f; tr.IsBold = TriState.True; tr.Fill.FillType = FillFormatType.Solid; tr.Fill.SolidColor.Color = Color.Black; } //獲取第2張幻燈片,添加形狀,并將圖片添加到形狀,設定鏈接,指向網頁地址 ISlide slide2 = ppt.Slides[1]; RectangleF rect = new RectangleF(250, 175, 195, 130); IEmbedImage image = slide2.Shapes.AppendEmbedImage(ShapeType.Rectangle, @"tp.png", rect); ClickHyperlink hyperlink = new ClickHyperlink("https://www.baidu.com/"); image.Click = hyperlink; //保存檔案 ppt.SaveToFile("AddHyperlink.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("AddHyperlink.pptx"); } } }
可在幻燈片放映中查看超鏈接添加效果,
文本超鏈接添加效果:

圖片超鏈接添加效果:

2. 編輯、洗掉PPT幻燈片中的超鏈接
using Spire.Presentation; namespace ModifyHyperlink { class Program { static void Main(string[] args) { //初始化Presentation實體 Presentation ppt = new Presentation(); //加載現有的檔案 ppt.LoadFromFile("AddHyperlink.pptx"); //獲取第一張幻燈片 ISlide slide = ppt.Slides[0]; //遍歷shape foreach (IShape shape in slide.Shapes) { //判斷是否為autoshape if (shape is IAutoShape) { //將shape轉換為autoshape IAutoShape autoShape = shape as IAutoShape; //遍歷autoshape中的paragraph foreach (TextParagraph tp in autoShape.TextFrame.Paragraphs) { //判斷paragraph下是否含有textrange if (tp.TextRanges != null && tp.TextRanges.Count > 0) { //遍歷textrange for (int tpcount = 0; tpcount < tp.TextRanges.Count; tpcount++) { //判斷是否含有文本且含有ClickAction和鏈接 if (tp.TextRanges[tpcount].ClickAction != null && !string.IsNullOrWhiteSpace(tp.TextRanges[tpcount].ClickAction.Address) && !string.IsNullOrWhiteSpace(tp.TextRanges[tpcount].Text)) { //判斷是否含有http鏈接或https鏈接 if (tp.TextRanges[tpcount].ClickAction.Address.ToLower().Contains("http") || tp.TextRanges[tpcount].ClickAction.Address.ToLower().Contains("https")) { //為鏈接重新賦值 tp.TextRanges[tpcount].ClickAction.Address = "https://baike.baidu.com/"; //重新設定超鏈接文本 tp.TextRanges[tpcount].Text = "百度百科"; //洗掉超鏈接 //tp.TextRanges[tpcount].ClickAction = null; } } } } } } } //保存檔案 ppt.SaveToFile("ModifyHyperlink.pptx", FileFormat.Pptx2010); System.Diagnostics.Process.Start("ModifyHyperlink.pptx"); } } }
超鏈接修改結果:

超鏈接洗掉效果:

(本文完)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/56702.html
標籤:C#
上一篇:【C#】Newtonsoft.Json 中 JArray 添加陣列報錯:Could not determine JSON object type for type 'xxx'
