继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

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

Eiceblue
关注TA
已关注
手记 107
粉丝 9
获赞 48

本文介绍通过C# 编程如何在PPT幻灯片中添加超链接的方法,添加链接时,可给文本或者图片添加超链接,链接对象可指向网页地址、邮件地址、指定幻灯片等,此外,也可以参考文中编辑、删除幻灯片中已有超链接的方法。

程序使用类库:Free Spire.Presentation for .NET (免费版)

dll获取及引用:

方法1可通过官网下载包,解压将Bin文件夹下的程序安装到指定路径;完成安装后,将安装路径下Bin文件夹中的Spire.Presentation.dll文件添加引用到程序,并添加using指令。

方法2可通过Nuget安装导入

Dll添加引用效果如下图:

http://img4.mukewang.com/5e8c21390001e7b003840600.jpg

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");        
            }    
        }
    }

可在幻灯片放映中查看超链接添加效果。

文本超链接添加效果:

http://img4.mukewang.com/5e8c227e0001e4ad13660768.jpg

图片超链接添加效果:

http://img2.mukewang.com/5e8c22c00001d71913660768.jpg


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");        
     }    
   }
}

超链接修改结果:

http://img.mukewang.com/5e8c24380001e43913660768.jpg

超链接删除效果:

http://img4.mukewang.com/5e8c2448000124da13660768.jpg



打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP