当前位置: 移动技术网 > IT编程>开发语言>c# > C# 添加、修改、删除PPT中的超链接

C# 添加、修改、删除PPT中的超链接

2020年04月04日  | 移动技术网IT编程  | 我要评论
本文介绍通过C# 编程如何在PPT幻灯片中添加超链接的方法,添加链接时,可给文本或者图片添加超链接,链接对象可指向网页地址、邮件地址、指定幻灯片等,此外,也可以参考文中编辑、删除幻灯片中已有超链接的方法。 程序使用类库:Free Spire.Presentation for .NET (免费版) d ...

本文介绍通过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");
        }
    }
}

超链接修改结果:

超链接删除效果:

 

(本文完)

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网