C#提取PPT文本——提取SmartArt中的文本、批注中的文本


提取文本的情況在工作和學習中常會遇到,在前面的文章中,已經講述了如何提取PPT中文本框里的文本,在本篇文章中,將介紹如何使用C#代碼語言提取PPT文檔中SmartArt和批注中的文本。同樣的,程序里面需要使用到Spire.Presentation for .NET,在編寫代碼前,需先安裝,並添引用dll文件到項目程序中。

1.提取SmartArt中的文本

測試文件如下(在第二張幻燈片中插入了SmartArt圖形,包含文本內容

【C#】

 1 using Spire.Presentation.Diagrams;
 2 using System.Drawing;
 3 using System.Text;
 4 using System.IO;
 5 using Spire.Presentation;
 6 
 7 namespace ExtractTextFromSmartArt_PPT
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //初始化一個Presentation類實例,並加載文檔
14             Presentation ppt = new Presentation();
15             ppt.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.pptx");
16             //新建一個StringBuilder對象
17             StringBuilder st = new StringBuilder();
18             //遍歷文檔中的SmartArt圖形
19             for (int i = 0; i < ppt.Slides.Count; i++)
20             {
21                 for (int j = 0; j < ppt.Slides[i].Shapes.Count; j++)
22                 {
23                     if (ppt.Slides[i].Shapes[j] is ISmartArt)
24                     {
25                         ISmartArt smartArt = ppt.Slides[i].Shapes[j] as ISmartArt;
26                         for (int k = 0; k < smartArt.Nodes.Count; k++)
27                         {
28                             st.Append(smartArt.Nodes[k].TextFrame.Text);
29                         }
30                     }
31                 }
32             }
33             //將文本寫入TXT文檔
34             File.WriteAllText("Result.txt", st.ToString());
35         }
36     }
37 }

提取的文本如下圖所示:

 

2.提取批注中文本

測試文件如下(在第一張幻燈片中,插入了批注,包含文本內容

 

【C#】

 1 using System;
 2 using System.Text;
 3 using Spire.Presentation;
 4 using System.IO;
 5 
 6 namespace ExtractTextFromComment_PPT
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             //實例化一個Presentation類,並加載文檔
13             Presentation ppt = new Presentation();
14             ppt.LoadFromFile(@"C:\Users\Administrator\Desktop\comment.pptx");
15             //創建一個StringBuilder對象
16             StringBuilder str = new StringBuilder();
17             //獲取第一張幻燈片中的所有批注
18             Comment[] comments = ppt.Slides[0].Comments;
19             //遍歷批注內容
20             for (int i = 0; i < comments.Length; i++)
21             {
22                 str.Append(comments[i].Text + "\r\n");
23             }
24             //將文本寫入TXT文檔
25             File.WriteAllText("TextFromComment.txt", str.ToString());
26         }
27     }
28 }

調試運行程序后,生成文檔,如下:

 

以上方法是提取PPT SmartArt和批注中文本的實現方法,供參考,希望能對您有所幫助,感謝閱讀!

 

(本文完)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM