標籤:.net c# ppt 批註 提取文本
提取文本的情況在工作和學習中常會遇到,在前面的文章中,已經講述了如何提取PPT中文字框裡的文本,在本篇文章中,將介紹如何使用C#代碼語言提取PPT文檔中SmartArt和批註中的文本。同樣的,程式裡面需要使用到 Free Spire.PPT for .NET,在編寫代碼前,需先安裝,並添引用dll檔案到項目程式中,同時也要添加到命名空間。
1.提取SmartArt中的文本
原始檔案:
650) this.width=650;" src="https://s1.51cto.com/oss/201711/09/e5592c7e7e9792816ed7d2650a9e288f.png" style="float:none;" title="21.png" alt="e5592c7e7e9792816ed7d2650a9e288f.png" />
(在投影片2中插入了SmartArt圖形,包含常值內容)
using Spire.Presentation.Diagrams;using System.Drawing;using System.Text;using System.IO;using Spire.Presentation; namespaceExtractTextFromSmartArt_PPT{ classProgram { staticvoid Main(string[] args) { //初始化一個Presentation類執行個體,並載入文檔 Presentation ppt = newPresentation(); ppt.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.pptx"); //建立一個StringBuilder對象 StringBuilder st = newStringBuilder(); //遍曆文檔中的SmartArt圖形 for (int i = 0; i <ppt.Slides.Count; i++) { for (int j = 0; j <ppt.Slides[i].Shapes.Count; j++) { if(ppt.Slides[i].Shapes[j] isISmartArt) { ISmartArt smartArt = ppt.Slides[i].Shapes[j] asISmartArt; for (int k = 0; k < smartArt.Nodes.Count; k++) { st.Append(smartArt.Nodes[k].TextFrame.Text); } } } } //將文本寫入TXT文檔 File.WriteAllText("Result.txt", st.ToString()); } }}
效果樣本如:
650) this.width=650;" src="https://s1.51cto.com/oss/201711/09/362631ddecdc0579887bd9b3db14f094.png" style="float:none;" title="22.png" alt="362631ddecdc0579887bd9b3db14f094.png" />
2.提取批註中的文本
原檔案:
650) this.width=650;" src="https://s1.51cto.com/oss/201711/09/195746d7b43e7f699440fa1c62266cbd.png" style="float:none;" title="23.png" alt="195746d7b43e7f699440fa1c62266cbd.png" />
在投影片1中,插入了批註,包含常值內容
using System;using System.Text;using Spire.Presentation;using System.IO; namespaceExtractTextFromComment_PPT{ classProgram { staticvoid Main(string[] args) { //執行個體化一個Presentation類,並載入文檔 Presentation ppt = newPresentation(); ppt.LoadFromFile(@"C:\Users\Administrator\Desktop\comment.pptx"); //建立一個StringBuilder對象 StringBuilder str = newStringBuilder(); //擷取第一張投影片中的所有批註 Comment[] comments =ppt.Slides[0].Comments; //遍曆批註內容 for (int i = 0; i <comments.Length; i++) { str.Append(comments[i].Text + "\r\n"); } //將文本寫入TXT文檔 File.WriteAllText("TextFromComment.txt", str.ToString()); } }}
效果樣本:
650) this.width=650;" src="https://s1.51cto.com/oss/201711/09/38321bc1893888785c2f77bcbcfece8e.png" style="float:none;" title="24.png" alt="38321bc1893888785c2f77bcbcfece8e.png" />
以上方法是提取PPT SmartArt和批註中文本的實現方法,供參考,希望能對您有所協助,感謝閱讀!
(本文完)
本文出自 “E-iceblue” 部落格,請務必保留此出處http://eiceblue.blog.51cto.com/13438008/1980186
C#如何提取PPT中 SmartArt文本和批註中的文本