欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#提取PPT文本和圖片的實(shí)現(xiàn)方法

 更新時(shí)間:2017年11月02日 14:45:23   作者:E-iceblue  
這篇文章主要為大家詳細(xì)介紹了C#提取PPT文本和圖片的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在圖文混排的文檔中,我們可以根據(jù)需要將文檔中的文字信息或者圖片提取出來,通過C#代碼可以提取Word和PDF文件中的文本和圖片,那么同樣的,我們也可以提取PPT幻燈片當(dāng)中的文本和圖片。本篇文檔將講述如何使用C#來實(shí)現(xiàn)提取PPT文本和圖片的操作。首先也是需要安裝組件Spire.Presentation,然后添加引用dll文件到項(xiàng)目中。下面是主要的代碼步驟。

原文檔:

1. 提取文本

步驟一:創(chuàng)建一個(gè)Presentation實(shí)例并加載文檔

Presentation presentation = new Presentation(@"C:\Users\Administrator\Desktop\sample.pptx", FileFormat.Pptx2010);

步驟二:創(chuàng)建一個(gè)StringBuilder對(duì)象

StringBuilder sb = new StringBuilder(); 

步驟三:遍歷幻燈片及幻燈片中的圖形,提取文本內(nèi)容

 foreach (ISlide slide in presentation.Slides)
  {
  foreach (IShape shape in slide.Shapes)
  {
   if (shape is IAutoShape)
   {
   foreach (TextParagraph tp in (shape as IAutoShape).TextFrame.Paragraphs)
   {
    sb.Append(tp.Text + Environment.NewLine);
   }
   }
  }
  }

步驟四:寫入Txt文檔

 File.WriteAllText("target.txt", sb.ToString());
 Process.Start("target.txt");

2. 提取圖片

 這里提取圖片有兩種情況,一種是提取整個(gè)文檔中的所有圖片,另外一種是只提取文檔中某一特定幻燈片中的圖片。

2.1提取所有圖片

步驟一:初始化一個(gè)Presentation類實(shí)例,并加載文檔

Presentation ppt = new Presentation();
 ppt.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pptx");

步驟二:遍歷文檔中圖片,提取圖片并保存

 for (int i = 0; i < ppt.Images.Count; i++)
 {
 Image image = ppt.Images[i].Image;
 image.Save(string.Format(@"..\..\Images{0}.png", i));
 }

提取的圖片已保存到項(xiàng)目文件夾下

2.2.提取特定幻燈片中的圖片

步驟一:創(chuàng)建一個(gè)Presentation類實(shí)例,并加載文檔

Presentation PPT = new Presentation();
PPT.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pptx");

步驟二:獲取第三張幻燈片,提取并保存圖片

int i = 0;
foreach (IShape s in PPT.Slides[2].Shapes)
{
 if (s is SlidePicture)
 {
 SlidePicture ps = s as SlidePicture;
 ps.PictureFill.Picture.EmbedImage.Image.Save(string.Format("{0}.png", i));
 i++;
 }
 if (s is PictureShape)
 {
 PictureShape ps = s as PictureShape;
 ps.EmbedImage.Image.Save(string.Format("{0}.png", i));
 i++;
 }
}

提取的第三張幻燈片中的圖片已保存至指定位置

上文演示了如何提取文本和圖片,步驟比較簡(jiǎn)單實(shí)用,希望對(duì)你有所幫助,感謝閱讀!

相關(guān)文章

最新評(píng)論