C# PDF 解析器:PDF編輯與文字擷取工具
使用 IronPDF 的 ExtractAllText 方法在 C# 中解析 PDF 文件,以從整個文件或特定頁面中提取文字。 這種方法只需幾行程式碼即可為.NET應用程式提供簡單、高效的 PDF 文字擷取。
IronPDF使 C# 應用程式中的 PDF 解析變得簡單。 本教學課程示範如何使用IronPDF (一個用於產生和操作 PDF 的綜合性 C# 函式庫)來解析 PDF,只需幾個步驟即可完成。
快速入門:使用IronPDF高效率解析 PDF
使用IronPDF ,只需編寫最少的程式碼即可開始在 C# 中解析 PDF 檔案。 本範例展示如何從 PDF 文件中提取所有文本,同時保持其原始格式。 IronPDF 的 ExtractAllText 方法可實現將 PDF 解析無縫整合到.NET應用程式中。 請按照以下步驟即可輕鬆完成設定和執行。
最簡工作流程(5個步驟)
- 下載 C# PDF 解析器庫
- 安裝到您的 Visual Studio 中
- 使用`ExtractAllText`方法提取每一行文字。
- 使用`ExtractTextFromPage`方法從單一頁面中提取所有文本
- 查看已解析的 PDF 內容
如何在C#中解析PDF檔案?
使用IronPDF解析 PDF 檔案非常簡單。 下面的程式碼使用 ExtractAllText 方法從整個 PDF 文件中提取每一行文字。 對比圖顯示了擷取的 PDF 內容及其輸出結果。 該庫還支援從 PDF 文件的特定部分提取文字和圖像。
:path=/static-assets/pdf/content-code-examples/how-to/csharp-parse-pdf-parse-pdf.cs
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)
IronPDF簡化了各種場景下的 PDF 解析。 無論是進行HTML 到 PDF 的轉換、從現有文件中提取內容,還是實現高級 PDF 功能,該程式庫都提供全面的支援。
IronPDF可與Windows 應用程式無縫集成,並支援在Linux和macOS平台上部署。 該程式庫還支援Azure 部署,以實現基於雲端的解決方案。
高級文字擷取範例
以下是使用IronPDF解析 PDF 內容的其他方法:
using IronPdf;
// Parse PDF from URL
var pdfFromUrl = PdfDocument.FromUrl("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;
// Parse PDF from URL
var pdfFromUrl = PdfDocument.FromUrl("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
' Parse PDF from URL
Dim pdfFromUrl = PdfDocument.FromUrl("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
這些範例展示了 IronPDF 在處理不同 PDF 來源和場景時的靈活性。 對於複雜的解析需求,可以探索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).Value
如何查看已解析的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");
}
}
Imports Microsoft.AspNetCore.Mvc
Imports System.IO
' 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
Private 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 解析整合到 Web 應用程式和批次處理場景中。 對於進階實現,可以探索非同步和多執行緒技術,以提高處理多個 PDF 時的效能。
準備好要看看你還能做什麼了嗎? 請造訪我們的教學頁面:編輯 PDF
常見問題解答
如何用 C# 從 PDF 檔案中萃取所有文字?
您可以使用 IronPDF 的 ExtractAllText 方法提取 PDF 文件中的所有文本。只需使用 IronPDF.FromFile("sample.pdf") 載入您的 PDF,並呼叫 ExtractAllText() 以擷取所有文字內容,同時保持原始格式。
在 .NET 中解析 PDF 的最簡單方法是什麼?
最簡單的方法是使用 IronPDF,只需一行程式碼:var text = IronPDF.FromFile("sample.pdf").ExtractAllText().此方法可從整個 PDF 文件中抽取每一行文字,所需的設定極少。
我可以從 PDF 的特定頁面中擷取文字嗎?
是的,IronPDF 提供 ExtractTextFromPage 方法來從個別頁面中提取文字。這可讓您針對 PDF 文件的特定部分,而非一次抽取所有內容。
如何在 C# 中解析受密碼保護的 PDF?
IronPDF 支援解析密碼保護的 PDF。使用 PdfDocument.FromFile("protected.pdf", "password123") 載入受保護的文件,然後調用 ExtractAllText() 來提取文本內容。
我可以從 URL 而非本機檔案解析 PDF 嗎?
是的,IronPDF 可以使用 PdfDocument.FromUrl("https://example.com/document.pdf") 直接從 URL 解析 PDF。從 URL 載入 PDF 之後,使用 ExtractAllText() 來提取文字內容。
PDF 解析器支援哪些平台?
IronPDF 支援跨多種平台的 PDF 解析,包括 Windows 應用程式、Linux、macOS 和 Azure 雲端部署,為您的 .NET 應用程式提供全面的跨平台相容性。
PDF 解析器在擷取過程中會保持文字格式嗎?
是的,IronPDF 的 ExtractAllText 方法可在提取過程中保持 PDF 內容的原始格式,確保解析後的文字保留來源文件的結構和排版。
我可以從 PDF 中同時擷取文字與影像嗎?
IronPDF 支持從 PDF 文檔中提取文本和图像。除了用於文字萃取的 ExtractAllText 方法之外,該函式庫還提供了從 PDF 文件的特定部分萃取影像的附加功能。

