C# PDF解析器
在C#中使用IronPDF的ExtractAllText方法解析PDF文件,以提取整个文档或特定页面的文本。 这种方法只需几行代码即可为 .NET 应用程序提供简单、高效的 PDF 文本提取功能。
IronPDF 使 PDF 解析在 C# 应用程序中变得简单明了。 本教程演示了如何使用IronPDF(一个全面的C#库,用于PDF生成和操作),仅需几个步骤即可解析PDF。
快速入门:使用IronPDF高效解析 PDF开始使用 IronPDF 以最少的代码在 C# 中解析 PDF。 本示例展示了如何从 PDF 文件中提取所有文本,同时保持其原始格式。 IronPDF的ExtractAllText方法使PDF解析顺利融入.NET应用程序。 请按照以下步骤进行直接设置和执行。
-
1使用 IronPDF 安装 NuGet Package Manager
-
2复制并运行这段代码。
using IronPdf; var text = PdfDocument.FromFile("sample.pdf").ExtractAllText();Imports IronPdf Dim text = PdfDocument.FromFile("sample.pdf").ExtractAllText()Imports IronPdf Dim text = PdfDocument.FromFile("sample.pdf").ExtractAllText() -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronPDF
最小工作流程(5 个步骤)
- 下载C# PDF解析库
- 在你的 Visual Studio 中安装
- 使用
ExtractAllText方法提取每一行文本 - 使用
ExtractTextFromPage方法从单个页面中提取所有文本 - 查看解析后的 PDF 内容
如何在C#中解析PDF文件?
使用 IronPDF 解析 PDF 文件非常简单。 下面的代码使用ExtractAllText方法从整个PDF文档中提取每一行文本。 对比显示了提取的 PDF 内容及其输出结果。 该库还支持从PDF文档的特定部分提取文本和图像。
输入
一个发票PDF样本。
using IronPdf;
// Select the desired PDF File
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");
// Extract all text from an pdf
string allText = pdf.ExtractAllText();
// Extract all text from page 1
string page1Text = pdf.ExtractTextFromPage(0);using IronPdf;
// Select the desired PDF File
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");
// Extract all text from an pdf
string allText = pdf.ExtractAllText();
// Extract all text from page 1
string page1Text = pdf.ExtractTextFromPage(0);Imports IronPdf
' Select the desired PDF File
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")
' Extract all text from an pdf
Private allText As String = pdf.ExtractAllText()
' Extract all text from page 1
Private page1Text As String = pdf.ExtractTextFromPage(0)Imports IronPdf
' Select the desired PDF File
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")
' Extract all text from an pdf
Private allText As String = pdf.ExtractAllText()
' Extract all text from page 1
Private page1Text As String = pdf.ExtractTextFromPage(0)输出
运行ExtractAllText将以阅读顺序返回页面文本:
Invoice INV-2026-0042
Billed to: Northwind Traders
Date: 2026-02-14
Consulting $3,200.00
Support $800.00
Total: $4,000.00
高级文本提取示例
以下是使用 IronPDF 解析 PDF 内容的其他方法:
输入
一个十页的报告文档。
using IronPdf;
using System;
// Parse PDF from URL
var pdfFromUrl = PdfDocument.FromUrl(new Uri("https://example.com/document.pdf"));
string urlPdfText = pdfFromUrl.ExtractAllText();
// Parse password-protected PDFs
var protectedPdf = PdfDocument.FromFile("protected.pdf", "password123");
string protectedText = protectedPdf.ExtractAllText();
// Extract text from specific page range
var largePdf = PdfDocument.FromFile("large-document.pdf");
for (int i = 5; i < 10; i++)
{
string pageText = largePdf.ExtractTextFromPage(i);
Console.WriteLine($"Page {i + 1}: {pageText.Substring(0, 100)}...");
}using IronPdf;
using System;
// Parse PDF from URL
var pdfFromUrl = PdfDocument.FromUrl(new Uri("https://example.com/document.pdf"));
string urlPdfText = pdfFromUrl.ExtractAllText();
// Parse password-protected PDFs
var protectedPdf = PdfDocument.FromFile("protected.pdf", "password123");
string protectedText = protectedPdf.ExtractAllText();
// Extract text from specific page range
var largePdf = PdfDocument.FromFile("large-document.pdf");
for (int i = 5; i < 10; i++)
{
string pageText = largePdf.ExtractTextFromPage(i);
Console.WriteLine($"Page {i + 1}: {pageText.Substring(0, 100)}...");
}Imports IronPdf
Imports System
' Parse PDF from URL
Dim pdfFromUrl = PdfDocument.FromUrl(New Uri("https://example.com/document.pdf"))
Dim urlPdfText As String = pdfFromUrl.ExtractAllText()
' Parse password-protected PDFs
Dim protectedPdf = PdfDocument.FromFile("protected.pdf", "password123")
Dim protectedText As String = protectedPdf.ExtractAllText()
' Extract text from specific page range
Dim largePdf = PdfDocument.FromFile("large-document.pdf")
For i As Integer = 5 To 9
Dim pageText As String = largePdf.ExtractTextFromPage(i)
Console.WriteLine($"Page {i + 1}: {pageText.Substring(0, 100)}...")
NextImports IronPdf
Imports System
' Parse PDF from URL
Dim pdfFromUrl = PdfDocument.FromUrl(New Uri("https://example.com/document.pdf"))
Dim urlPdfText As String = pdfFromUrl.ExtractAllText()
' Parse password-protected PDFs
Dim protectedPdf = PdfDocument.FromFile("protected.pdf", "password123")
Dim protectedText As String = protectedPdf.ExtractAllText()
' Extract text from specific page range
Dim largePdf = PdfDocument.FromFile("large-document.pdf")
For i As Integer = 5 To 9
Dim pageText As String = largePdf.ExtractTextFromPage(i)
Console.WriteLine($"Page {i + 1}: {pageText.Substring(0, 100)}...")
Next输出
URL和密码保护的行需要实时输入,但页面范围循环会将每个提取的页面写入控制台:

对于复杂的解析需求,请探索PDF DOM对象访问以处理结构化内容。
处理不同类型的PDF
IronPDF 擅长解析各种 PDF 类型:
using IronPdf;
using System.Text.RegularExpressions;
// Parse scanned PDFs with OCR (requires IronOcr)
var scannedPdf = PdfDocument.FromFile("scanned-document.pdf");
string ocrText = scannedPdf.ExtractAllText();
// Parse PDFs with forms
var formPdf = PdfDocument.FromFile("form.pdf");
string formText = formPdf.ExtractAllText();
// Extract and filter specific content
string invoiceText = pdf.ExtractAllText();
var invoiceNumber = Regex.Match(invoiceText, @"Invoice #: (\d+)").Groups[1].Value;
var totalAmount = Regex.Match(invoiceText, @"Total: \$([0-9,]+\.\d{2})").Groups[1].Value;using IronPdf;
using System.Text.RegularExpressions;
// Parse scanned PDFs with OCR (requires IronOcr)
var scannedPdf = PdfDocument.FromFile("scanned-document.pdf");
string ocrText = scannedPdf.ExtractAllText();
// Parse PDFs with forms
var formPdf = PdfDocument.FromFile("form.pdf");
string formText = formPdf.ExtractAllText();
// Extract and filter specific content
string invoiceText = pdf.ExtractAllText();
var invoiceNumber = Regex.Match(invoiceText, @"Invoice #: (\d+)").Groups[1].Value;
var totalAmount = Regex.Match(invoiceText, @"Total: \$([0-9,]+\.\d{2})").Groups[1].Value;Imports IronPdf
Imports System.Text.RegularExpressions
' Parse scanned PDFs with OCR (requires IronOcr)
Dim scannedPdf = PdfDocument.FromFile("scanned-document.pdf")
Dim ocrText As String = scannedPdf.ExtractAllText()
' Parse PDFs with forms
Dim formPdf = PdfDocument.FromFile("form.pdf")
Dim formText As String = formPdf.ExtractAllText()
' Extract and filter specific content
Dim invoiceText As String = pdf.ExtractAllText()
Dim invoiceNumber = Regex.Match(invoiceText, "Invoice #: (\d+)").Groups(1).Value
Dim totalAmount = Regex.Match(invoiceText, "Total: \$([0-9,]+\.\d{2})").Groups(1).ValueImports IronPdf
Imports System.Text.RegularExpressions
' Parse scanned PDFs with OCR (requires IronOcr)
Dim scannedPdf = PdfDocument.FromFile("scanned-document.pdf")
Dim ocrText As String = scannedPdf.ExtractAllText()
' Parse PDFs with forms
Dim formPdf = PdfDocument.FromFile("form.pdf")
Dim formText As String = formPdf.ExtractAllText()
' Extract and filter specific content
Dim invoiceText As String = pdf.ExtractAllText()
Dim invoiceNumber = Regex.Match(invoiceText, "Invoice #: (\d+)").Groups(1).Value
Dim totalAmount = Regex.Match(invoiceText, "Total: \$([0-9,]+\.\d{2})").Groups(1).Value分层PDF,例如CAD图纸和地图导出,是一种独特的类型:其内容被分组为命名的可选内容组(OCGs)。 您可以使用ExtractTextFromLayer将提取范围限定为一个层,或使用ExtractTextFromLayers结合多个层,而不是解析整个文档。 查看从特定层提取文本以获取详细信息。
如何查看解析后的PDF内容?
C# 表单显示上述代码执行中解析的 PDF 内容。 该输出可提供 PDF 中的准确文本,以满足文档处理的需要。
提取的文本保持了 PDF 的原始格式和结构,因此非常适合数据处理、内容分析或迁移任务。 通过查找和替换特定内容或将其导出至其他格式来进一步处理这些文本。
将PDF解析集成到您的应用中
IronPDF 的解析功能可集成到各种应用类型中:
// ASP.NET Core example
public IActionResult ParseUploadedPdf(IFormFile pdfFile)
{
using var stream = pdfFile.OpenReadStream();
var pdf = PdfDocument.FromStream(stream);
var extractedText = pdf.ExtractAllText();
// Process or store the extracted text
return Json(new {
success = true,
textLength = extractedText.Length,
preview = extractedText.Substring(0, Math.Min(500, extractedText.Length))
});
}
// Console application example
static void BatchParsePdfs(string folderPath)
{
var pdfFiles = Directory.GetFiles(folderPath, "*.pdf");
foreach (var file in pdfFiles)
{
var pdf = PdfDocument.FromFile(file);
var text = pdf.ExtractAllText();
// Save extracted text
var textFile = Path.ChangeExtension(file, ".txt");
File.WriteAllText(textFile, text);
Console.WriteLine($"Parsed: {Path.GetFileName(file)} - {text.Length} characters");
}
}// ASP.NET Core example
public IActionResult ParseUploadedPdf(IFormFile pdfFile)
{
using var stream = pdfFile.OpenReadStream();
var pdf = PdfDocument.FromStream(stream);
var extractedText = pdf.ExtractAllText();
// Process or store the extracted text
return Json(new {
success = true,
textLength = extractedText.Length,
preview = extractedText.Substring(0, Math.Min(500, extractedText.Length))
});
}
// Console application example
static void BatchParsePdfs(string folderPath)
{
var pdfFiles = Directory.GetFiles(folderPath, "*.pdf");
foreach (var file in pdfFiles)
{
var pdf = PdfDocument.FromFile(file);
var text = pdf.ExtractAllText();
// Save extracted text
var textFile = Path.ChangeExtension(file, ".txt");
File.WriteAllText(textFile, text);
Console.WriteLine($"Parsed: {Path.GetFileName(file)} - {text.Length} characters");
}
}' ASP.NET Core example
Public Function ParseUploadedPdf(pdfFile As IFormFile) As IActionResult
Using stream = pdfFile.OpenReadStream()
Dim pdf = PdfDocument.FromStream(stream)
Dim extractedText = pdf.ExtractAllText()
' Process or store the extracted text
Return Json(New With {
.success = True,
.textLength = extractedText.Length,
.preview = extractedText.Substring(0, Math.Min(500, extractedText.Length))
})
End Using
End Function
' Console application example
Shared Sub BatchParsePdfs(folderPath As String)
Dim pdfFiles = Directory.GetFiles(folderPath, "*.pdf")
For Each file In pdfFiles
Dim pdf = PdfDocument.FromFile(file)
Dim text = pdf.ExtractAllText()
' Save extracted text
Dim textFile = Path.ChangeExtension(file, ".txt")
File.WriteAllText(textFile, text)
Console.WriteLine($"Parsed: {Path.GetFileName(file)} - {text.Length} characters")
Next
End Sub' ASP.NET Core example
Public Function ParseUploadedPdf(pdfFile As IFormFile) As IActionResult
Using stream = pdfFile.OpenReadStream()
Dim pdf = PdfDocument.FromStream(stream)
Dim extractedText = pdf.ExtractAllText()
' Process or store the extracted text
Return Json(New With {
.success = True,
.textLength = extractedText.Length,
.preview = extractedText.Substring(0, Math.Min(500, extractedText.Length))
})
End Using
End Function
' Console application example
Shared Sub BatchParsePdfs(folderPath As String)
Dim pdfFiles = Directory.GetFiles(folderPath, "*.pdf")
For Each file In pdfFiles
Dim pdf = PdfDocument.FromFile(file)
Dim text = pdf.ExtractAllText()
' Save extracted text
Dim textFile = Path.ChangeExtension(file, ".txt")
File.WriteAllText(textFile, text)
Console.WriteLine($"Parsed: {Path.GetFileName(file)} - {text.Length} characters")
Next
End Sub这些示例展示了将 PDF 解析纳入网络应用程序和批处理场景的情况。 对于高级实现,请探索异步和多线程技术,以在处理多个PDF时提高性能。
准备好看看您还能做些什么吗? 查看我们的教程页面:编辑PDF
常见问题
如何使用C#从PDF中提取文本?
您可以使用IronPDF的ExtractAllText方法轻松地从整个PDF文档或单个页面提取文本,只需几行C#代码。
在IronPDF中,我使用什么方法解析PDF?
IronPDF中的ExtractAllText方法允许您在.NET应用程序中高效地从PDF文档中提取文本。
IronPDF可以处理从PDF的特定页面提取文本吗?
可以,IronPDF通过其ExtractTextFromPage方法支持从特定页面提取文本,使其在针对性PDF文本提取方面非常灵活。
IronPDF支持从密码保护的PDF中提取文本吗?
是的,通过在打开文档时提供正确的密码,IronPDF允许您从密码保护的PDF中提取文本。
IronPDF可以解析从URL检索到的PDF吗?
可以。您可以使用IronPDF的PdfDocument.FromUrl方法直接从URL解析PDF,然后使用ExtractAllText提取其文本。
IronPDF 如何处理从扫描文档中提取文本?
如果PDF是扫描的文档,IronPDF可以利用OCR功能(与IronOcr一起)从此类基于图像的PDF中提取文本。
是否可以使用IronPDF从PDF中提取和筛选特定内容?
可以,IronPDF允许您提取所有文本,并利用正则表达式或自定义逻辑来筛选和定位特定内容,如发票号码或总额。
如何使用IronPDF将PDF解析功能集成到Web应用程序中?
您可以将IronPDF的解析功能集成到Web应用程序中,如ASP.NET Core,通过读取PDF输入流并使用ExtractAllText处理或显示提取的内容。
IronPDF提供哪些高级文本提取功能?
IronPDF提供了高级功能,例如从特定页面范围解析文本、处理分层PDF,并通过异步或多线程提高性能。
IronPDF可以在提取文本时保持PDF的原始格式吗?
可以,IronPDF被设计为在提取文本时保留PDF的原始格式和结构,使其适合文档处理和分析任务。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

