本文介绍通过C# 编程如何在PPT幻灯片中添加超链接的方法,添加链接时,可给文本或者图片添加超链接,链接对象可指向网页地址、邮件地址、指定幻灯片等,此外,也可以参考文中编辑、删除幻灯片中已有超链接的方法。
程序使用类库:Free Spire.Presentation for .NET (免费版)
dll获取及引用:
方法1:可通过官网下载包,解压将Bin文件夹下的程序安装到指定路径;完成安装后,将安装路径下Bin文件夹中的Spire.Presentation.dll文件添加引用到程序,并添加using指令。
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:[email protected]";指向邮件地址 根据字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.pptxstem.Diagnostics.Process.Start(); } } }
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); } } }
(本文完)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。