C# 添加、修改、删除PPT中的超链接
发布时间:2020-12-16 01:07:21 所属栏目:百科 来源:网络整理
导读:本文介绍通过C# 编程如何在PPT幻灯片中添加超链接的方法,添加链接时,可给文本或者图片添加超链接,链接对象可指向网页地址、邮件地址、指定幻灯片等,此外,也可以参考文中编辑、删除幻灯片中已有超链接的方法。 程序使用类库 :Free Spire.Presentation f
本文介绍通过C# 编程如何在PPT幻灯片中添加超链接的方法,添加链接时,可给文本或者图片添加超链接,链接对象可指向网页地址、邮件地址、指定幻灯片等,此外,也可以参考文中编辑、删除幻灯片中已有超链接的方法。 程序使用类库:Free Spire.Presentation for .NET (免费版) dll获取及引用: 方法1:可通过官网下载包,解压将Bin文件夹下的程序安装到指定路径;完成安装后,将安装路径下Bin文件夹中的Spire.Presentation.dll文件添加引用到程序,并添加using指令。 方法2:可通过Nuget安装导入。 Dll添加引用效果如下图: ? ? C# 代码示例1. 添加超链接到PPT幻灯片using Spire.Presentation; Spire.Presentation.Drawing; 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,450,1)">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 = TextRange(s1); tr1.ClickAction.Address = https://www.baidu.com/tr1.ClickAction.Address = "mailto:123654zz@163.com";指向邮件地址 根据字s2创建tr2 TextRange tr2 = TextRange(s2); 根据字符串s3创建tr3,并在文字上添加链接,指向第二张幻灯片 TextRange tr3 = 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[]; RectangleF rect = 250,1)">175,1)">195,1)">130); IEmbedImage image = slide2.Shapes.AppendEmbedImage(ShapeType.Rectangle,1)">@"tp.png,rect); ClickHyperlink hyperlink = new ClickHyperlink(); image.Click = hyperlink; 保存文档 ppt.SaveToFile(AddHyperlink.pptx); } } } 可在幻灯片放映中查看超链接添加效果。 文本超链接添加效果: 图片超链接添加效果: ? 2. 编辑、删除PPT幻灯片中的超链接Spire.Presentation; ModifyHyperlink { 加载现有的文档 ppt.LoadFromFile(); 获取第一张幻灯片 ISlide slide = ppt.Slides[]; 遍历shape foreach (IShape shape slide.Shapes) { 判断是否为autoshape if (shape is IAutoShape) { 将shape转换为autoshape IAutoShape autoShape = shape as IAutoShape; 遍历autoshape中的paragraph foreach (TextParagraph tp autoShape.TextFrame.Paragraphs) { 判断paragraph下是否含有textrange if (tp.TextRanges != null && tp.TextRanges.Count > ) { 遍历textrange for (int tpcount = 0; tpcount < tp.TextRanges.Count; tpcount++) { 判断是否含有文本且含有ClickAction和链接 if (tp.TextRanges[tpcount].ClickAction != null && !string.IsNullOrWhiteSpace(tp.TextRanges[tpcount].ClickAction.Address) && !.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; } } } } } } } ModifyHyperlink.pptx); } } } 超链接修改结果: 超链接删除效果: ? (本文完) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |