半岛权威十大直营(官方)网站

使用Aspose.Slides从演示文稿中提取文本

原创|行业资讯|编辑:郝浩|2013-08-07 12:55:32.000|阅读 2503 次

概述:如何从演示文稿中提取文本?本文以Microsoft PowerPoint PPTX演示文稿为例,为你介绍如何用Aspose.Slides控件从中提取文本。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

开发人员需要从演示文稿中提取文本,这并不罕见。要做到这一点,你需要从演示文稿所有不同图形的幻灯片中提取文本。为此,本文以Microsoft PowerPoint PPTX演示文稿为例, 为你介绍如何用Aspose.Slides控件从中提取文本。无论是从一张幻灯片中提取文本,还是从演示文稿的所有幻灯片中提取文本,Aspose.Slides使用静态方法PresentationScanner都能帮你做到。提取的文本会自动打包在命名空间Aspose.Slides.Util下面。

从单张幻灯片中提取文本


Aspose.Slides for .NET提供一个叫做Aspose.Slides.Util的命名空间,它包括一个PresentationScanner类。这个类显示了多个从一页演示文稿或幻灯片中提取文本的重载静态方法。 从PPTX演示幻灯片中提取文本,可以使用PresentationScanner类下面显示的重载静态方法GetAllTextBoxes。这个方法接收SlideEx对象作为一个参数。
执行时,SlideEx方法扫描经过的幻灯片上的所有文本,作为参数返回一组TextFrameEx对象。这意味着与文本相关的任何文本格式都适用。下面的一段代码显示在第一张幻灯片上提取文本:

C#

//Instatiate PresentationEx class that represents a PPTX file

using(PresentationEx pptxPresentation = new PresentationEx("d:\\pptx\\testx.pptx"))

{

 

 //Get an Array of TextFrameEx objects from the first slide

TextFrameEx[] textFramesSlideOne = SlideUtil.GetAllTextBoxes(pptxPresentation.Slides[0]);

            

 //Loop through the Array of TextFrames

for(int&nbsp;i=0;i<textFramesSlideOne.Length;i++)

 

    //Loop through paragraphs in current TextFrame

    foreach( ParagraphEx para in textFramesSlideOne[i].Paragraphs )

 

        //Loop through portions in the current Paragraph

        foreach (PortionEx port in para.Portions)

       &nbsp;{

       &nbsp;   //Display text in the current portion

    &nbsp;      Console.WriteLine(port.Text);

 

           //Display font height of the text

           Console.WriteLine(port.FontHeight);

 

       &nbsp;    //Display font name of the text

           Console.WriteLine(port.LatinFont.FontName);

 &nbsp;      }

 

 }

 

Visual Basic

'Instatiate PresentationEx class that represents a PPTX file

Using Dim ;pptxPresentation As New PresentationEx("d:\pptx\testx.pptx")

 

'Get an Array of TextFrameEx objects from the first slide

Dim textFramesSlideOne() As TextFrameEx = SlideUtil.GetAllTextBoxes(pptxPresentation.Slides(0))

 

'Loop through the Array of TextFrames

For i As Integer = 0 To textFramesSlideOne.Length&nbsp;- 1

 

    'Loop through paragraphs in current TextFrame

   &nbsp;For Each para As ParagraphEx In textFramesSlideOne(i).Paragraphs

 

     'Loop through portions in the current Paragraph

     For Each port&nbsp;As PortionEx In para.Portions

 

         'Display text in the current portion

   &nbsp;     Console.WriteLine(port.Text)

 

         'Display font height of the text

       ;  Console.WriteLine(port.FontHeight)

 

         'Display font name of the text

         Console.WriteLine(port.LatinFont.FontName)

     Next port

    Next para

Next i

End Using

从整个演示文稿提取文本


要扫描整个演示文稿的文本,可以使用 PresentationScanner类显示的静态方法GetAllTextFrames。它包含两个参数:
1. 一个PresentationEx对象:显示当前正从中提取文本的PPTX演示文稿
2. 一个布尔值:决定当文本正从演示文稿中扫描时,主幻灯片是否包含在内。

 这种方法将返回一组TextFrameEx对象,带有完整的文本格式信息。下面的代码表示扫描来自于演示文稿的文本和格式信息,包括主幻灯片。

C#

//Instatiate PresentationEx class that represents a PPTX file

using(PresentationEx pptxPresentation = new PresentationEx(&quot;d:\\pptx\\testx.pptx"))

{

 

//Get an Array of TextFrameEx objects from all slides in the&nbsp;PPTX

TextFrameEx[] textFramesPPTX = SlideUtil.GetAllTextFrames(pptxPresentation, true);

 

 //Loop through&nbsp;the Array of TextFrames

for (int ;i = 0; i < textFramesPPTX.Length; i++)

 

 &nbsp; //Loop&nbsp;through paragraphs in current TextFrame

   foreach (ParagraphEx para in textFramesPPTX[i].Paragraphs)

 

   ;   //Loop through portions in the current Paragraph

      foreach (PortionEx port in para.Portions)

      {

   &nbsp;     //Display text in the current portion

        &nbsp;Console.WriteLine(port.Text);

 

   &nbsp;     //Display font height of the text

  &nbsp;&nbsp;     Console.WriteLine(port.FontHeight);

 

   &nbsp;     //Display font name of the text

       &nbsp; Console.WriteLine(port.LatinFont.FontName);

       }

   

}

 

Visual Basic

'Instatiate PresentationEx class that represents a PPTX file

Using Dim pptxPresentation&nbsp;As New PresentationEx("d:\pptx\testx.pptx")

 

'Get an Array of TextFrameEx objects from all slides in the PPTX

Dim textFramesPPTX() As TextFrameEx = SlideUtil.GetAllTextBoxes(pptxPresentation.Slides(0))

 

'Loop through the Array of TextFrames

For i As Integer =&nbsp;0 To textFramesPPTX.Length - 1

  

  'Loop through paragraphs in current TextFrame

    For Each para As&nbsp;ParagraphEx In textFramesPPTX(i).Paragraphs

 

     'Loop through portions in the current Paragraph

     ;For Each port As PortionEx&nbsp;In para.Portions

 

         'Display text in the current portion

       &nbsp; Console.WriteLine(port.Text)

 

         'Display font height of the text

         Console.WriteLine(port.FontHeight)

 

         'Display font name of the text

         Console.WriteLine(port.LatinFont.FontName)

     Next port

    Next para

Next i

End Using

 

总结


Aspose.Slides.Util.SlideUtil类显示多个可供选择的动态方法来扫描演示文稿或幻灯片中的文本。格式信息也连同扫描的文件被提取出来。 如果你也遇到需要从演示文稿中提取文本或类似的难题,不妨试试Aspose.Slides,相信它会带给你不一样的体验和收获。

>>Aspose.Slides免费试用下载

Aspose文档管理系列控件查看


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@wqylolg.cn

文章转载自:慧都控件网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
相关产品
Aspose.Slides

Aspose.Slides是第一个能在用户的应用程序中对PowerPoint文档进行管理的组件。

Aspose.Slides for Reporting Services

Aspose.Slides for Reporting Services 是惟一的能在Microsoft SQL Server 2005和2008 Reporting Services 中以 Microsoft PowerPoint PPT 和 PPS 格式生成报表的解决方案。

Aspose.Slides for JasperReports

Aspose.Slides for JasperReports 是专门为JasperReports用户开发的一种标准组件,以帮助他们将其Java应用程序中的报表能够简单地导出为Microsoft PowerPoint Presentation (PPT)和Microsoft PowerPoint Show (PPS)格式。

Aspose.Slides for SharePoint

Aspose.Slides for SharePoint使您在一个SharePoint应用程序中读取和转换PowerPoint文件而不需要使用Microsoft PowerPoint。

扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP