当前位置:网站首页>C#/VB.NET:从PowerPoint文档中提取文本和图片

C#/VB.NET:从PowerPoint文档中提取文本和图片

2022-08-09 17:59:00 Gia-

当我们想要将PowerPoint文档中的精美图片用作自己的素材或是想要在其他地方引用PowerPoint文档中的文本时,可以从该文档中提取图片和文本。在这篇文章中,我将演示如何使用Spire.Presentation for .NET在C#和VB.NET程序中,提取PowerPoint文档中的文本和图片。

从PowerPoint中提取文本

从PowerPoint中提取图片

 

安装 Spire.Presentation for .NET

 

首先,我们需要将 Spire.Presentation for .NET 包中包含的 DLL 文件添加为 .NET 项目中的引用。可以从此链接下载 DLL 文件,也可以通过NuGet 安装 DLL 文件。

 

PM> Install-Package Spire.Presentation

 

PowerPoint中提取文本

C#

using System;
using System.Text;
using Spire.Presentation;
using System.Diagnostics;
using System.IO;

namespace ExtractText
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建Presentation类的对象
            Presentation presentation = new Presentation();

            //加载PowerPoint示例文件
            presentation.LoadFromFile("Sample.pptx");

            //创建StringBuilder类的对象
            StringBuilder sb = new StringBuilder();

            //遍历幻灯片
            foreach (ISlide slide in presentation.Slides)
            {
                //遍历幻灯片上的形状
                foreach (IShape shape in slide.Shapes)
                {
                    //判断形状是否为IAutoshape
                    if (shape is IAutoShape)
                    {
                        //遍历形状内的段落
                        foreach (TextParagraph tp in (shape as IAutoShape).TextFrame.Paragraphs)
                        {
                            //将段落文本添加到StringBuilder
                            sb.Append(tp.Text + Environment.NewLine);
                        }
                    }

                }

            }

            //将文本写入txt文件
            File.WriteAllText("Result.txt", sb.ToString());
        }
    }
}

VB.NET

Imports System
Imports System.Text
Imports Spire.Presentation
Imports System.Diagnostics
Imports System.IO
  
Namespace ExtractText
    Class Program
        Shared  Sub Main(ByVal args() As String)
           '创建Presentation类的对象
            Dim presentation As Presentation =  New Presentation() 
  
            '加载PowerPoint示例文件
            presentation.LoadFromFile("Sample.pptx")
  
            '创建StringBuilder类的对象
            Dim sb As StringBuilder =  New StringBuilder() 
  
            '遍历幻灯片
            Dim slide As ISlide
            For Each slide In presentation.Slides
                '遍历幻灯片上的形状
                Dim shape As IShape
                For Each shape In slide.Shapes
                    '判断形状是否为IAutoshape
                    If TypeOf shape Is IAutoShape Then
                        '遍历形状内的段落
                        Dim tp As TextParagraph
                        For Each tp in(shape as IAutoShape).TextFrame.Paragraphs
                            '将段落文本添加到StringBuilder
                            sb.Append(tp.Text + Environment.NewLine)
                        Next
                    End If
  
                Next
  
            Next
  
            '将文本写入txt文件
            File.WriteAllText("Result.txt", sb.ToString())
        End Sub
    End Class
End Namespace

PowerPoint中提取图片

C#

using System.Drawing;
using Spire.Presentation;

namespace ExtractImage
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建Presentation类的对象
            Presentation presentation = new Presentation();

            //加载PowerPoint示例文件
            presentation.LoadFromFile("Sample.pptx");

            //遍历文档中的图片
            for (int i = 0; i < presentation.Images.Count; i++)
            {
                //获得特定的图片
                Image image = presentation.Images[i].Image;

                //保存图片到本地
                image.Save(string.Format("Result.png", i));
            }
        }
    }
}

VB.NET

Imports System.Drawing
Imports Spire.Presentation
  
Namespace ExtractImage
    Class Program
        Shared  Sub Main(ByVal args() As String)
            '创建Presentation类的对象
            Dim presentation As Presentation = New Presentation()

            '加载PowerPoint示例文件
            presentation.LoadFromFile("Sample.pptx")

            '遍历文档中的图片
            Dim i As Integer
         For  i = 0 To  presentation.Images.Count- 1  Step  i + 1
                '获得特定的图片
                Dim image As Image = presentation.Images(i).Image

                '保存图片到本地
                image.Save(String.Format("Result.png", i))
            Next
        End Sub
    End Class
End Namespace

原网站

版权声明
本文为[Gia-]所创,转载请带上原文链接,感谢
https://www.cnblogs.com/Gia-/p/16567010.html