C# 添加、修改、刪除PPT中的超鏈接


本文介紹通過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:123654zz@163.com";//指向郵件地址


            //根據字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");
        }
    }
}

超鏈接修改結果:

超鏈接刪除效果:

 

(本文完)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM