PDF 資料擷取 .NET:完整開發者指南
使用IronPDF在.NET中從PDF中提取文字、表格、表單和圖像,只需幾行程式碼——通過NuGet安裝,載入您的PDF,然後調用ExtractAllText()即可在5分鐘內開始。
PDF文件在商務中隨處可見:發票、報告、合同和手冊。 但要以程式方式從中提取重要資訊可能很棘手。 PDF專注於外觀,而不是資料如何被存取。 對於使用C#中的OCR的開發者來說,這在處理掃描文件時帶來了獨特的挑戰。
對於.NET開發者,IronPDF是一個強大的.NET PDF程式庫,可以輕鬆從PDF文件中提取資料。 您可以直接從輸入的PDF文件中提取文字、表格、表單字段、圖像和附件。 無論您是在自動化發票處理、構建知識庫還是生成報告,這個程式庫都能幫助您節省大量時間。在處理掃描的PDF時,您可能還需要PDF OCR文字提取功能來處理基於圖像的內容。
本指南通過實用的範例引導您提取文字內容、表格資料和表單欄位值,並在每個程式碼範例後進行解釋,以便您可以將其應用於自己的項目。 如果您還在處理其他文件型別,您可能會發現探索閱讀掃描文件或TIFF轉換為可搜索的PDF很有幫助。
如何開始使用IronPDF?
通過NuGet Package Manager安裝IronPDF只需幾秒鐘。 打開您的Package Manager Console並運行:
Install-Package IronOcr
有關更高級的安裝場景,請參閱NuGet程式包文件。 一旦安裝完畢,您可以立即開始處理輸入的PDF文件。 這裡是一個簡單的.NET範例,展示了IronPDF的API之簡易:
using IronPdf;
// Load any PDF document
var pdf = PdfDocument.FromFile("document.pdf");
// Extract all text with one line
string allText = pdf.ExtractAllText();
Console.WriteLine(allText);
using IronPdf;
// Load any PDF document
var pdf = PdfDocument.FromFile("document.pdf");
// Extract all text with one line
string allText = pdf.ExtractAllText();
Console.WriteLine(allText);
Imports IronPdf
' Load any PDF document
Dim pdf = PdfDocument.FromFile("document.pdf")
' Extract all text with one line
Dim allText As String = pdf.ExtractAllText()
Console.WriteLine(allText)
此程式碼載入一個PDF並提取每一個字元的文字。 IronPDF自動處理複雜的PDF結構、表單資料和通常會造成其他程式庫問題的編碼。 從PDF文件中提取的資料可以保存到文字文件中或進一步處理以進行分析。 對於更複雜的提取需求,您可能會想探索專業文件處理技術。
實用提示:您可以將提取的文字保存到.txt文件以備後續處理,或解析它以填充資料庫、Excel工作表或知識庫。 此方法對於報告、合同或任何需要快速提取原始文字的PDF文件都非常適用。 對於涉及表格的情況,考慮學習有關在文件中讀取表格以進行更結構化資料提取。
提取的文字會是什麼樣子?

如何從特定頁面提取資料?
真實世界的應用程式通常需要精確的資料提取。 IronPDF提供多種方法來從PDF內的特定頁面中定位有價值的資訊。 這種方法類似於OCR特定區域提取,但用於PDF文件。 在此範例中,我們將使用以下PDF:

以下程式碼從此PDF內的特定頁面中提取資料,並將結果返回給我們的控制台。 當處理多頁文件時,您可能還會發現多頁TIFF處理技術對於類似的挑戰來說是有用的。
using IronPdf;
using System;
using System.Text.RegularExpressions;
// Load any PDF document
var pdf = PdfDocument.FromFile("AnnualReport2024.pdf");
// Extract from selected pages
int[] pagesToExtract = { 0, 2, 4 }; // Pages 1, 3, and 5
foreach (var pageIndex in pagesToExtract)
{
string pageText = pdf.ExtractTextFromPage(pageIndex);
// Split on 2 or more spaces (tables often flatten into space-separated values)
var tokens = Regex.Split(pageText, @"\s{2,}");
foreach (string token in tokens)
{
// Match totals, invoice headers, and invoice rows
if (token.Contains("Invoice") || token.Contains("Total") || token.StartsWith("INV-"))
{
Console.WriteLine($"Important: {token.Trim()}");
}
}
}
using IronPdf;
using System;
using System.Text.RegularExpressions;
// Load any PDF document
var pdf = PdfDocument.FromFile("AnnualReport2024.pdf");
// Extract from selected pages
int[] pagesToExtract = { 0, 2, 4 }; // Pages 1, 3, and 5
foreach (var pageIndex in pagesToExtract)
{
string pageText = pdf.ExtractTextFromPage(pageIndex);
// Split on 2 or more spaces (tables often flatten into space-separated values)
var tokens = Regex.Split(pageText, @"\s{2,}");
foreach (string token in tokens)
{
// Match totals, invoice headers, and invoice rows
if (token.Contains("Invoice") || token.Contains("Total") || token.StartsWith("INV-"))
{
Console.WriteLine($"Important: {token.Trim()}");
}
}
}
Imports IronPdf
Imports System
Imports System.Text.RegularExpressions
' Load any PDF document
Dim pdf = PdfDocument.FromFile("AnnualReport2024.pdf")
' Extract from selected pages
Dim pagesToExtract As Integer() = {0, 2, 4} ' Pages 1, 3, and 5
For Each pageIndex In pagesToExtract
Dim pageText As String = pdf.ExtractTextFromPage(pageIndex)
' Split on 2 or more spaces (tables often flatten into space-separated values)
Dim tokens = Regex.Split(pageText, "\s{2,}")
For Each token As String In tokens
' Match totals, invoice headers, and invoice rows
If token.Contains("Invoice") OrElse token.Contains("Total") OrElse token.StartsWith("INV-") Then
Console.WriteLine($"Important: {token.Trim()}")
End If
Next
Next
這個例子展示了如何從PDF文件中提取文字,搜索關鍵資訊並準備將其儲存於資料文件或知識庫。 ExtractTextFromPage()方法保持文件的閱讀順序,使其非常適合用於文件分析和內容索引任務。 為提高準確性,您可以考慮在處理較低質量的PDF時使用圖像優化過濾器。

處理財務文件時,您可能會從財務語言包中獲益,以提高專業術語的準確性。 此外,進度跟踪可以幫助監控提取性能,以便處理大型文件批次。
如何從PDF中提取表格?
PDF文件中的表格沒有本地結構——它們只是被定位為看起來像表格的文字內容。 IronPDF提取表格資料同時保留佈局,以便您可以將其處理為Excel或文字文件。 這類似於OCR圖像提取,但特別針對表格內容進行優化。 在此例中,我們將使用此PDF:

我們的目標是提取表格內的資料,展示IronPDF解析表格資料的能力。 對於更高級的表格提取場景,請探索在文件中讀取表格,這使用機器學習技術來處理複雜的表格結構。
using IronPdf;
using System;
using System.Text;
using System.Text.RegularExpressions;
var pdf = PdfDocument.FromFile("example.pdf");
string rawText = pdf.ExtractAllText();
// Split into lines for processing
string[] lines = rawText.Split('\n');
var csvBuilder = new StringBuilder();
foreach (string line in lines)
{
if (string.IsNullOrWhiteSpace(line) || line.Contains("Page"))
continue;
string[] rawCells = Regex.Split(line.Trim(), @"\s+");
string[] cells;
// If the line starts with "Product", combine first two tokens as product name
if (rawCells[0].StartsWith("Product") && rawCells.Length >= 5)
{
cells = new string[rawCells.Length - 1];
cells[0] = rawCells[0] + " " + rawCells[1]; // Combine Product + letter
Array.Copy(rawCells, 2, cells, 1, rawCells.Length - 2);
}
else
{
cells = rawCells;
}
// Keep header or table rows
bool isTableOrHeader = cells.Length >= 2
&& (cells[0].StartsWith("Item") || cells[0].StartsWith("Product")
|| Regex.IsMatch(cells[0], @"^INV-\d+"));
if (isTableOrHeader)
{
Console.WriteLine($"Row: {string.Join("|", cells)}");
string csvRow = string.Join(",", cells).Trim();
csvBuilder.AppendLine(csvRow);
}
}
// Save as CSV for Excel import
File.WriteAllText("extracted_table.csv", csvBuilder.ToString());
Console.WriteLine("Table data exported to CSV");
using IronPdf;
using System;
using System.Text;
using System.Text.RegularExpressions;
var pdf = PdfDocument.FromFile("example.pdf");
string rawText = pdf.ExtractAllText();
// Split into lines for processing
string[] lines = rawText.Split('\n');
var csvBuilder = new StringBuilder();
foreach (string line in lines)
{
if (string.IsNullOrWhiteSpace(line) || line.Contains("Page"))
continue;
string[] rawCells = Regex.Split(line.Trim(), @"\s+");
string[] cells;
// If the line starts with "Product", combine first two tokens as product name
if (rawCells[0].StartsWith("Product") && rawCells.Length >= 5)
{
cells = new string[rawCells.Length - 1];
cells[0] = rawCells[0] + " " + rawCells[1]; // Combine Product + letter
Array.Copy(rawCells, 2, cells, 1, rawCells.Length - 2);
}
else
{
cells = rawCells;
}
// Keep header or table rows
bool isTableOrHeader = cells.Length >= 2
&& (cells[0].StartsWith("Item") || cells[0].StartsWith("Product")
|| Regex.IsMatch(cells[0], @"^INV-\d+"));
if (isTableOrHeader)
{
Console.WriteLine($"Row: {string.Join("|", cells)}");
string csvRow = string.Join(",", cells).Trim();
csvBuilder.AppendLine(csvRow);
}
}
// Save as CSV for Excel import
File.WriteAllText("extracted_table.csv", csvBuilder.ToString());
Console.WriteLine("Table data exported to CSV");
Imports IronPdf
Imports System
Imports System.Text
Imports System.Text.RegularExpressions
Dim pdf = PdfDocument.FromFile("example.pdf")
Dim rawText As String = pdf.ExtractAllText()
' Split into lines for processing
Dim lines As String() = rawText.Split(ControlChars.Lf)
Dim csvBuilder As New StringBuilder()
For Each line As String In lines
If String.IsNullOrWhiteSpace(line) OrElse line.Contains("Page") Then
Continue For
End If
Dim rawCells As String() = Regex.Split(line.Trim(), "\s+")
Dim cells As String()
' If the line starts with "Product", combine first two tokens as product name
If rawCells(0).StartsWith("Product") AndAlso rawCells.Length >= 5 Then
cells = New String(rawCells.Length - 2) {}
cells(0) = rawCells(0) & " " & rawCells(1) ' Combine Product + letter
Array.Copy(rawCells, 2, cells, 1, rawCells.Length - 2)
Else
cells = rawCells
End If
' Keep header or table rows
Dim isTableOrHeader As Boolean = cells.Length >= 2 AndAlso (cells(0).StartsWith("Item") OrElse cells(0).StartsWith("Product") OrElse Regex.IsMatch(cells(0), "^INV-\d+"))
If isTableOrHeader Then
Console.WriteLine($"Row: {String.Join("|", cells)}")
Dim csvRow As String = String.Join(",", cells).Trim()
csvBuilder.AppendLine(csvRow)
End If
Next
' Save as CSV for Excel import
File.WriteAllText("extracted_table.csv", csvBuilder.ToString())
Console.WriteLine("Table data exported to CSV")
PDF中的表格通常只是被定位為看起來像網格的文字。 此檢查有助於確定一行是否屬於表格行或表頭。 通過過濾掉表頭、頁腳和不相關的文字,您可以從PDF中提取乾淨的表格資料,準備好用於CSV或Excel。 處理具有複雜佈局的收據和發票時,請查看AdvancedScan擴展。
此工作流程適用於PDF表單、財務文件和報告。 稍後,您可以將資料從PDF中轉換為xlsx文件,或將它們合併成包含所有有用資料的zip文件。 對於具有合併單元格的復雜表格,您可能需要根據列位置調整解析邏輯。 上述資料輸出文件提供了有關結構化結果工作的詳細指南。

為提高表格提取的準確性,考慮使用計算機視覺技術自動檢測表格區域後再進行處理。 此方法可大幅提高在複雜佈局上的效果。
如何提取表單字段資料?
IronPDF還處理表單字段資料的提取和修改,類似於護照閱讀功能的結構化文件:
using IronPdf;
using System.Drawing;
using System.Linq;
var pdf = PdfDocument.FromFile("form_document.pdf");
// Extract form field data
var form = pdf.Form;
foreach (var field in form) // Removed '.Fields' as 'FormFieldCollection' is enumerable
{
Console.WriteLine($"{field.Name}: {field.Value}");
// Update form values if needed
if (field.Name == "customer_name")
{
field.Value = "Updated Value";
}
}
// Save modified form
pdf.SaveAs("updated_form.pdf");
using IronPdf;
using System.Drawing;
using System.Linq;
var pdf = PdfDocument.FromFile("form_document.pdf");
// Extract form field data
var form = pdf.Form;
foreach (var field in form) // Removed '.Fields' as 'FormFieldCollection' is enumerable
{
Console.WriteLine($"{field.Name}: {field.Value}");
// Update form values if needed
if (field.Name == "customer_name")
{
field.Value = "Updated Value";
}
}
// Save modified form
pdf.SaveAs("updated_form.pdf");
Imports IronPdf
Imports System.Drawing
Imports System.Linq
Dim pdf = PdfDocument.FromFile("form_document.pdf")
' Extract form field data
Dim form = pdf.Form
For Each field In form ' Removed '.Fields' as 'FormFieldCollection' is enumerable
Console.WriteLine($"{field.Name}: {field.Value}")
' Update form values if needed
If field.Name = "customer_name" Then
field.Value = "Updated Value"
End If
Next
' Save modified form
pdf.SaveAs("updated_form.pdf")
此程式碼從PDF中提取表單字段值並允許您以程式方式更新它們,使您可以輕鬆地處理PDF表格並提取指定範圍的資訊進行分析或報告生成。 這對於自動化如客戶上線、調查處理或資料驗證等工作流程非常有用。 對於身份文件處理,探索身份文件OCR最佳實踐。

當處理包含複選框和單選按鈕的表單時,您可能需要實施自定義邏輯,類似於條碼和QR閱讀,以處理特殊字段型別。OcrResult Class文件提供了有關處理各種結果型別的全面細節。
接下來我應該做什麼?
IronPDF使在.NET中提取PDF資料變得實用且高效。 您可以提取圖像、文字、表格、表單字段,甚至從各種PDF文件中提取附件,包括通常需要額外OCR處理的掃描PDF。 對於掃描文件,將IronPDF與IronOCR功能結合使用,提供全面的文件處理能力。
無論您是在構建知識庫、自動化報告工作流,還是從財務PDF中提取資料,此程式庫都為您提供足夠的工具,以免除手動復製或容易出錯的解析。 它簡單、快速,直接整合到Visual Studio項目中。 對於部署,IronPDF支持多個平台,包括Windows、Linux、Docker,以及像AWS和Azure這樣的雲平台。
試試吧——您可能會節省時間並避免處理PDF的常見頭痛。 對於初創公司和小團隊,授權選項包括能夠隨著您的需求增長的靈活計劃。 您還可以探索授權金鑰實施以進行生產部署。
準備好在您的應用程式中實現PDF資料提取了嗎? IronPDF聽起來像是適合您的.NET程式庫嗎? 開始您的免費試用以存取完整功能,或者探索我們的商業使用授權選項。 存取我們的文件,獲取全面的指南和API參考。 為了快速實施,請查看我們的演示和程式碼範例,以快速開始。
常見問題
從PDF文件中提取資料的主要挑戰為何?
PDF文件主要設計為以特定佈局顯示內容,這使得程式化地提取資料變得具有挑戰性,因為重點放在外觀而非資料可存取性上。
IronOCR如何在.NET中協助PDF資料提取?
IronOCR 提供了從PDF(包括掃描文件)中提取文字和資料的工具,使用光學字元識別(OCR)將文字圖像轉為機器可讀資料。
IronOCR可以處理掃描的PDF文件嗎?
可以,IronOCR 能夠使用先進的OCR技術處理掃描的PDF,識別並從文件中的圖像中提取文字。
IronOCR使用哪種編程語言來進行PDF資料提取?
IronOCR 設計用於C#,這使其成為開發者在.NET框架中用於從PDF中提取資料的絕佳選擇。
是否有使用IronOCR進行PDF資料提取的程式碼範例可用?
有,該指南包括完整的C#程式碼範例,演示如何有效地使用IronOCR從PDF文件中提取資料。
IronOCR能從PDF文件中解析表格嗎?
IronOCR包含從PDF文件中解析表格的功能,使開發者能高效地提取結構化資料。
IronOCR可以提取哪些型別的PDF內容?
IronOCR 可以從PDF中提取各種內容,包括文字、表格和掃描圖像中的資料,是一款多功能資料提取工具。



