IronPDF 操作指南 提取文本和图像 How to Extract Embedded Text and Images from PDFs Chaknith Bin 已更新:八月 20, 2025 Download IronPDF NuGet 下载 DLL 下载 Windows 安装程序 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article This article was translated from English: Does it need improvement? Translated View the article in English role="alert"> 您的企业在PDF安全性和合规性的年度订阅上花费过多。考虑使用IronSecureDoc,它提供数字签名、涂黑、加密和保护等SaaS服务管理的解决方案,仅需一次性付费。探索IronSecureDoc文档 提取嵌入的文本和图像涉及检索文档中的文本内容和图形元素。 此过程允许用户访问和重新利用内容以进行编辑、搜索或将文本转换为其他格式,并保存图像用于再利用或分析。 要从PDF中提取文本和图像,请使用IronPdf。 提取的图像可以保存到磁盘或转换为另一种图像格式并嵌入到新渲染的文档中。 快速入门:使用IronPDF提取文本和图像 只需几行代码即可使用IronPDF轻松从PDF中提取文本和图像。 此快速入门指南为开发人员提供了从PDF文档中检索嵌入内容所需的工具,有助于内容重新利用和分析。 无论您是提取文本以进行编辑还是保存图像以供进一步使用,IronPDF都能确保提供一个流线型和具有成本效益的解决方案。 今天就开始使用IronPdf库,体验无缝的PDF内容管理。 Get started making PDFs with NuGet now: Install IronPDF with NuGet Package Manager PM > Install-Package IronPdf Copy and run this code snippet. var pdf = new IronPdf.PdfDocument("sample.pdf"); string text = pdf.ExtractAllText(); var images = pdf.ExtractAllImages(); Deploy to test on your live environment Start using IronPDF in your project today with a free trial Free 30 day Trial class="hsg-featured-snippet"> 最小工作流(5步) 下载IronPdf C#库 准备PDF文档以进行文本和图像提取 使用ExtractAllText方法提取文本 使用ExtractAllImages方法提取图像 指定要从中提取文本和图像的特定页面 提取文本示例 文本提取可以在新渲染的和现有的PDF文档上执行。 使用ExtractAllText方法从文档中提取嵌入的文本。 该方法将返回一个包含给定PDF中所有文本的字符串。 页面通过四个连续的换行符分隔。 让我们使用我从维基百科网站渲染的示例PDF。 :path=/static-assets/pdf/content-code-examples/how-to/extract-text-and-images-extract-text.cs using IronPdf; using System.IO; PdfDocument pdf = PdfDocument.FromFile("sample.pdf"); // Extract text string text = pdf.ExtractAllText(); // Export the extracted text to a text file File.WriteAllText("extractedText.txt", text); Imports IronPdf Imports System.IO Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf") ' Extract text Private text As String = pdf.ExtractAllText() ' Export the extracted text to a text file File.WriteAllText("extractedText.txt", text) $vbLabelText $csharpLabel class="content-img-align-center"> class="center-image-wrapper"> 按行和字符提取文本 在每个PDF页面中,可能检索到文本行和字符的坐标。 首先,从PDF中选择一个页面并访问行和字符属性。 坐标布局为上、右、下、左值,表示文本的位置。 :path=/static-assets/pdf/content-code-examples/how-to/extract-text-and-images-extract-text-by-line-character.cs using IronPdf; using System.IO; using System.Linq; // Open PDF from file PdfDocument pdf = PdfDocument.FromFile("sample.pdf"); // Extract text by lines var lines = pdf.Pages[0].Lines; // Extract text by characters var characters = pdf.Pages[0].Characters; File.WriteAllLines("lines.txt", lines.Select(l => $"at Y={l.BoundingBox.Bottom:F2}: {l.Contents}")); Imports IronPdf Imports System.IO Imports System.Linq ' Open PDF from file Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf") ' Extract text by lines Private lines = pdf.Pages(0).Lines ' Extract text by characters Private characters = pdf.Pages(0).Characters File.WriteAllLines("lines.txt", lines.Select(Function(l) $"at Y={l.BoundingBox.Bottom:F2}: {l.Contents}")) $vbLabelText $csharpLabel class="content-img-align-center"> class="center-image-wrapper"> <hr 提取图像示例 使用ExtractAllImages方法提取文档中嵌入的所有图像。 该方法将返回一个AnyBitmap对象列表形式的图像。 使用我们之前示例中的同一文档,我们提取了图像并将它们导出到“images”文件夹中。 :path=/static-assets/pdf/content-code-examples/how-to/extract-text-and-images-extract-image.cs using IronPdf; PdfDocument pdf = PdfDocument.FromFile("sample.pdf"); // Extract images var images = pdf.ExtractAllImages(); for(int i = 0; i < images.Count; i++) { // Export the extracted images images[i].SaveAs($"images/image{i}.png"); } Imports IronPdf Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf") ' Extract images Private images = pdf.ExtractAllImages() For i As Integer = 0 To images.Count - 1 ' Export the extracted images images(i).SaveAs($"images/image{i}.png") Next i $vbLabelText $csharpLabel class="content-img-align-center"> class="center-image-wrapper"> 除了上面显示的ExtractAllImages方法,用户可以使用ExtractAllBitmaps和ExtractAllRawImages方法从文档中提取图像信息。 虽然ExtractAllBitmaps方法将返回像代码示例一样的AnyBitmap列表,ExtractAllRawImages方法将从PDF文档中提取所有图像并以Byte Arrays(byte[])原始数据形式返回。 <hr 在特定页面上提取文本和图像 文本和图像提取可以在单个或多个指定页面上执行。 使用ExtractTextFromPage和ExtractTextFromPages方法分别从单个页面或多个页面提取文本。 对于提取图像,使用ExtractImagesFromPage和ExtractImagesFromPages方法。 :path=/static-assets/pdf/content-code-examples/how-to/extract-text-and-images-extract-text-single-multiple.cs using IronPdf; PdfDocument pdf = PdfDocument.FromFile("sample.pdf"); // Extract text from page 1 string textFromPage1 = pdf.ExtractTextFromPage(0); int[] pages = new[] { 0, 2 }; // Extract text from pages 1 & 3 string textFromPage1_3 = pdf.ExtractTextFromPages(pages); Imports IronPdf Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf") ' Extract text from page 1 Private textFromPage1 As String = pdf.ExtractTextFromPage(0) Private pages() As Integer = { 0, 2 } ' Extract text from pages 1 & 3 Private textFromPage1_3 As String = pdf.ExtractTextFromPages(pages) $vbLabelText $csharpLabel 常见问题解答 如何在.NET C#中从PDF中提取嵌入文本? 您可以使用IronPdf库中的ExtractAllText方法从PDF中提取嵌入文本。此方法返回一个字符串,其中每页的文本之间用四个连续的新行字符分隔。 使用C#从PDF中提取图像需要哪些步骤? 要在C#中从PDF中提取图像,首先通过NuGet下载IronPdf库。然后使用ExtractAllImages方法,它将返回表示图像的AnyBitmap对象列表。 我可以从PDF文档的特定页面中提取文本吗? 是的,您可以使用IronPdf中的ExtractTextFromPage和ExtractTextFromPages方法从PDF文档的特定页面或多个页面中提取文本。 按线和字符坐标提取文本的目的是什么? 按线和字符坐标提取文本使您能够检索PDF页面中文本的确切位置。这可以使用IronPdf中的**Lines**和**Characters**属性来完成,它们提供Top, Right, Bottom, 和Left值。 如何从PDF中提取原始格式的图像? 要提取原始格式的图像,请使用IronPdf中的ExtractAllRawImages方法。此方法将图像作为字节数组返回,使您能够访问原始图像数据。 使用IronPdf提取文本和图像有哪些好处? 使用IronPdf从PDF中提取文本和图像具有成本效益,因为它提供了一次性支付解决方案。它有助于重新利用内容进行编辑、搜索、转换为其他格式,以及重新使用图像进行分析。 如何开始使用IronPdf进行PDF内容提取? 要开始使用IronPdf,从NuGet下载IronPdf C#库,并按照指南准备您的PDF文档,使用ExtractAllText和ExtractAllImages等方法进行内容提取。 是否可以从单个PDF页面提取文本和图像? 是的,IronPdf允许您使用ExtractTextFromPage和ExtractImagesFromPage方法从单个PDF页面提取文本和图像。 有哪些方法可用于从多个页面中提取图像? 您可以使用IronPdf中的ExtractImagesFromPages方法从PDF文档的多个页面中提取图像。 IronPdf 是否兼容 .NET 10,能否用于提取文本和图像? 是的——IronPdf 支持 .NET 10,以及更早的现代版本,例如 .NET 9、8、7、6、CORE、Standard 和 Framework。您可以在 .NET 10 项目中使用所有相同的方法,例如ExtractAllText 、 ExtractAllImages 、 ExtractTextFromPage和ExtractImagesFromPages ,无需任何变通方法或兼容性调整。 Chaknith Bin 立即与工程团队聊天 软件工程师 Chaknith 在 IronXL 和 IronBarcode 工作。他在 C# 和 .NET 方面有着深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的见解有助于更好的产品、文档和整体体验。 准备开始了吗? Nuget 下载 16,154,058 | 版本: 2025.11 刚刚发布 免费 NuGet 下载 总下载量:16,154,058 查看许可证