C#/VB.NET:从PowerPoint文档中提取文本和图片
当我们想要将PowerPoint文档中的精美图片用作自己的素材或是想要在其他地方引用PowerPoint文档中的文本时,可以从该文档中提取图片和文本。在这篇文章中,我将演示如何使用Spire.Presentation for .NET在C#和VB.NET程序中,提取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
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)