当前位置:网站首页>C#/VB.NET: Extract text and pictures from PowerPoint document

C#/VB.NET: Extract text and pictures from PowerPoint document

2022-08-09 21:15:00 Gia-

当我们想要将PowerPointBeautiful images in the documentation to use as your own material or want to reference elsewherePowerPointtext in a document,Pictures and text can be extracted from this document.在这篇文章中,我将演示如何使用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)
            {
                //Iterate over the shapes on the slide
                foreach (IShape shape in slide.Shapes)
                {
                    //Determine if the shape is IAutoshape
                    if (shape is IAutoShape)
                    {
                        //Traverse the paragraphs within the shape
                        foreach (TextParagraph tp in (shape as IAutoShape).TextFrame.Paragraphs)
                        {
                            //Add paragraph text to 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
                'Iterate over the shapes on the slide
                Dim shape As IShape
                For Each shape In slide.Shapes
                    'Determine if the shape is IAutoshape
                    If TypeOf shape Is IAutoShape Then
                        'Traverse the paragraphs within the shape
                        Dim tp As TextParagraph
                        For Each tp in(shape as IAutoShape).TextFrame.Paragraphs
                            'Add paragraph text to 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");

            //Iterate over the images in the document
            for (int i = 0; i < presentation.Images.Count; i++)
            {
                //get a specific image
                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")

            'Iterate over the images in the document
            Dim i As Integer
         For  i = 0 To  presentation.Images.Count- 1  Step  i + 1
                'get a specific image
                Dim image As Image = presentation.Images(i).Image

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

原网站

版权声明
本文为[Gia-]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091758560442.html