C#如何提取PPT中 SmartArt文本和批注中的文本
提取文本的情况在工作和学习中常会遇到,在本篇文章中,将介绍如何使用C#代码语言提取PPT文档中SmartArt和批注中的文本。同样的,程序里面需要使用到 Free Spire.PPT for .NET,在编写代码前,需先安装,并添引用dll文件到项目程序中,同时也要添加到命名空间。
1.提取SmartArt中的文本
原始文件:
(在幻灯片2中插入了SmartArt图形,包含文本内容)
usingSpire.Presentation.Diagrams;usingSystem.Drawing;usingSystem.Text;usingSystem.IO;usingSpire.Presentation;namespaceExtractTextFromSmartArt_PPT{classProgram{staticvoidMain(string[]args){//初始化一个Presentation类实例,并加载文档Presentationppt=newPresentation();ppt.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.pptx");//新建一个StringBuilder对象StringBuilderst=newStringBuilder();//遍历文档中的SmartArt图形for(inti=0;i<ppt.Slides.Count;i++){for(intj=0;j<ppt.Slides[i].Shapes.Count;j++){if(ppt.Slides[i].Shapes[j]isISmartArt){ISmartArtsmartArt=ppt.Slides[i].Shapes[j]asISmartArt;for(intk=0;k<smartArt.Nodes.Count;k++){st.Append(smartArt.Nodes[k].TextFrame.Text);}}}}//将文本写入TXT文档File.WriteAllText("Result.txt",st.ToString());}}}
效果示例如下图:
2.提取批注中的文本
原文件:
在幻灯片1中,插入了批注,包含文本内容
usingSystem;usingSystem.Text;usingSpire.Presentation;usingSystem.IO;namespaceExtractTextFromComment_PPT{classProgram{staticvoidMain(string[]args){//实例化一个Presentation类,并加载文档Presentationppt=newPresentation();ppt.LoadFromFile(@"C:\Users\Administrator\Desktop\comment.pptx");//创建一个StringBuilder对象StringBuilderstr=newStringBuilder();//获取第一张幻灯片中的所有批注Comment[]comments=ppt.Slides[0].Comments;//遍历批注内容for(inti=0;i<comments.Length;i++){str.Append(comments[i].Text+"\r\n");}//将文本写入TXT文档File.WriteAllText("TextFromComment.txt",str.ToString());}}}
效果示例:
以上方法是提取PPT SmartArt和批注中文本的实现方法,供参考,希望能对您有所帮助,感谢阅读!
(本文完)
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。