溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#如何提取PPT中 SmartArt文本和批注中的文本

發(fā)布時(shí)間:2020-08-10 19:06:04 來(lái)源:網(wǎng)絡(luò) 閱讀:827 作者:E_iceblue 欄目:編程語(yǔ)言

提取文本的情況在工作和學(xué)習(xí)中常會(huì)遇到,在本篇文章中,將介紹如何使用C#代碼語(yǔ)言提取PPT文檔中SmartArt和批注中的文本。同樣的,程序里面需要使用到 Free Spire.PPT for .NET,在編寫代碼前,需先安裝,并添引用dll文件到項(xiàng)目程序中,同時(shí)也要添加到命名空間。

1.提取SmartArt中的文本

原始文件:

C#如何提取PPT中 SmartArt文本和批注中的文本

(在幻燈片2中插入了SmartArt圖形,包含文本內(nèi)容)

using Spire.Presentation.Diagrams;
using System.Drawing;
using System.Text;
using System.IO;
using Spire.Presentation;
 
namespaceExtractTextFromSmartArt_PPT
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
            //初始化一個(gè)Presentation類實(shí)例,并加載文檔
            Presentation ppt = newPresentation();
            ppt.LoadFromFile(@"C:\Users\Administrator\Desktop\Sample.pptx");
            //新建一個(gè)StringBuilder對(duì)象
            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());
        }
    }
}


效果示例如下圖:

C#如何提取PPT中 SmartArt文本和批注中的文本


2.提取批注中的文本


原文件:

C#如何提取PPT中 SmartArt文本和批注中的文本

在幻燈片1中,插入了批注,包含文本內(nèi)容

using System;
using System.Text;
using Spire.Presentation;
using System.IO;
 
namespaceExtractTextFromComment_PPT
{
    classProgram
    {
        staticvoid Main(string[] args)
        {
            //實(shí)例化一個(gè)Presentation類,并加載文檔
            Presentation ppt = newPresentation();
           ppt.LoadFromFile(@"C:\Users\Administrator\Desktop\comment.pptx");
            //創(chuàng)建一個(gè)StringBuilder對(duì)象
            StringBuilder str = newStringBuilder();
            //獲取第一張幻燈片中的所有批注
            Comment[] comments =ppt.Slides[0].Comments;
            //遍歷批注內(nèi)容
            for (int i = 0; i <comments.Length; i++)
            {
               str.Append(comments[i].Text + "\r\n");
            }
            //將文本寫入TXT文檔
            File.WriteAllText("TextFromComment.txt", str.ToString());
        }
    }
}

效果示例:

C#如何提取PPT中 SmartArt文本和批注中的文本


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

 

(本文完)


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

ppt
AI