如何在C#中從PDF中提取資料
從PDF文件中提取資料對於節省手動輸入的時間至關重要。 本文解釋了開發人員如何使用 IronPDF 庫從 PDF 文件中提取文字和圖像。
如何在C#中從PDF中提取資料
- 從 PDF 提取資料 C# 庫
- 在 Visual Studio 中創建一個新項目
- 將庫安裝到您的項目中
- 從特定頁面提取資料,並從PDF中提取特定內容。
- 查看PDF文件的資料輸出
IronPDF:C# PDF 函式庫
IronPDF是一個 .NET 函式庫,可用於建立、編輯和轉換 PDF 檔案。 它為開發者提供了一個易於使用的 API,供他們在應用程式中使用。 它是全球最受歡迎的用於創建、編輯和轉換 PDF 文件的庫之一。 使用 IronPDF,您可以建立簡單快速的 PDF 解決方案。 您的文字將針對每個文件進行定制,您的佈局將便於閱讀,您的圖形將藉助配套的 .NET 程式進行設計。
IronPDF 庫有一個非常棒的功能,可以從 PDF 文件中提取資料。 本文將探討如何使用 IronPDF 擷取資料。 首先,需要建立或開啟一個 C# 專案。 我們進入下一節。
在 Visual Studio 中建立或開啟 C# 項目
本教學建議使用最新版本的 Visual Studio。
開啟 Visual Studio 後,請依照下列步驟建立一個新的 C# 專案。 如果您想使用現有的項目,請跳過接下來的步驟,直接進入下一節。
- 開啟 Visual Studio 點擊"建立新項目"按鈕。
如何在 C# 中從 PDF 中提取資料,圖 1:Visual Studio 開啟介面 Visual Studio 開啟介面
- 從範本中選擇"C# 控制台應用程式"。
如何在 C# 中從 PDF 中提取資料,圖 2:建立一個新項目 建立一個新項目
- 為項目命名,然後點選"下一步"按鈕。
- 根據專案需求選擇 .NET Framework,然後按一下"建立"按鈕。
如何在 C# 中從 PDF 中提取資料,圖 3:.NET Framework 選擇 .NET Framework 選擇
Visual Studio 現在將產生一個新的 C# .NET 專案。
安裝 IronPDF 函式庫
IronPDF庫可以透過多種方式安裝。
使用軟體套件管理器控制台
- 開啟程式包管理員控制台,方法是前往"工具" > "NuGet 套件管理員" > "套件管理員控制台" 。
- 執行以下指令安裝 IronPDF 庫:
Install-Package IronPdf
如何在 C# 中從 PDF 中提取資料,圖 4:程式包管理器控制台標籤中的安裝進度 軟體包管理器控制台標籤中的安裝進度
安裝完成後,您將在解決方案資源管理器的 dependencies 部分看到 IronPDF 依賴項,如下所示。
如何在 C# 中從 PDF 中提取資料,圖 5:在解決方案資源管理器中引用 IronPDF 包 在解決方案資源管理器中引用 IronPDF 包
使用 NuGet 套件管理器
安裝 IronPDF 庫的另一種方法是使用 Visual Studio 整合的 NuGet 套件管理器 UI。
- 從主選單進入"工具"選單。 將滑鼠停留在下拉式功能表中的"NuGet 套件管理器"上,然後選擇"管理解決方案的 NuGet 套件..."。
如何在 C# 中從 PDF 中提取資料,圖 6:導航到 NuGet 套件管理器 導覽至 NuGet 套件管理器
這將開啟 NuGet 套件管理器視窗。 前往"瀏覽"選項卡,在搜尋框中輸入 IronPdf,然後按 Enter 鍵。
- 從搜尋結果中選擇 IronPDF,然後點擊"安裝"按鈕開始安裝。
如何在 C# 中從 PDF 中提取資料,圖 7:從 NuGet 套件管理器安裝 IronPDF 套件 從 NuGet 套件管理器安裝 IronPDF 套件
從 PDF 文件中提取資料
讓我們來看看如何使用 IronPDF 來擷取資料:
// Import necessary namespaces
using IronPdf;
using System.Collections.Generic;
using System.Drawing;
public class PDFExtractor
{
public void ExtractDataFromPDF()
{
// Open a 128-bit encrypted PDF file by providing the filename and password
using PdfDocument pdf = PdfDocument.FromFile("encrypted.pdf", "password");
// Extract all text from the PDF document
string allText = pdf.ExtractAllText();
// Extract all images from the PDF document
IEnumerable<Image> allImages = pdf.ExtractAllImages();
// Iterate over each page in the PDF document
for (var index = 0; index < pdf.PageCount; index++)
{
int pageNumber = index + 1;
// Extract text from the specific page
string text = pdf.ExtractTextFromPage(index);
// Extract images from the specific page
IEnumerable<Image> images = pdf.ExtractImagesFromPage(index);
// Code to process the extracted text and images
//...
}
}
}
// Import necessary namespaces
using IronPdf;
using System.Collections.Generic;
using System.Drawing;
public class PDFExtractor
{
public void ExtractDataFromPDF()
{
// Open a 128-bit encrypted PDF file by providing the filename and password
using PdfDocument pdf = PdfDocument.FromFile("encrypted.pdf", "password");
// Extract all text from the PDF document
string allText = pdf.ExtractAllText();
// Extract all images from the PDF document
IEnumerable<Image> allImages = pdf.ExtractAllImages();
// Iterate over each page in the PDF document
for (var index = 0; index < pdf.PageCount; index++)
{
int pageNumber = index + 1;
// Extract text from the specific page
string text = pdf.ExtractTextFromPage(index);
// Extract images from the specific page
IEnumerable<Image> images = pdf.ExtractImagesFromPage(index);
// Code to process the extracted text and images
//...
}
}
}
' Import necessary namespaces
Imports IronPdf
Imports System.Collections.Generic
Imports System.Drawing
Public Class PDFExtractor
Public Sub ExtractDataFromPDF()
' Open a 128-bit encrypted PDF file by providing the filename and password
Using pdf As PdfDocument = PdfDocument.FromFile("encrypted.pdf", "password")
' Extract all text from the PDF document
Dim allText As String = pdf.ExtractAllText()
' Extract all images from the PDF document
Dim allImages As IEnumerable(Of Image) = pdf.ExtractAllImages()
' Iterate over each page in the PDF document
For index = 0 To pdf.PageCount - 1
Dim pageNumber As Integer = index + 1
' Extract text from the specific page
Dim text As String = pdf.ExtractTextFromPage(index)
' Extract images from the specific page
Dim images As IEnumerable(Of Image) = pdf.ExtractImagesFromPage(index)
' Code to process the extracted text and images
'...
Next index
End Using
End Sub
End Class
在這個程式碼範例中:
- 使用
FromFile方法載入輸入的 PDF 文檔,該文檔已加密,需要密碼。 ExtractAllText方法從 PDF 擷取所有文字內容。ExtractAllImages方法取得所有嵌入的影像。- 循環遍歷文件的每一頁,使用
ExtractTextFromPage和ExtractImagesFromPage從該特定頁面提取文字和圖像。
結論
IronPDF 讓開發人員能夠輕鬆地從 PDF 文件中提取文字和圖像。 使用 ExtractAllText 和 ExtractAllImages,可以立即擷取 PDF 檔案的全部內容。 或者,這些方法也可以用於從特定頁面提取內容。 前面的程式碼示範如何使用這兩種方法從一系列頁面中讀取文字和圖像。
此外,IronPDF 還提供渲染圖表、添加條碼、使用密碼增強安全性、添加浮水印以及以程式處理 PDF 表單等功能。
IronPDF 在開發階段可免費使用,商業用途需付費。 IronPDF 提供免費試用版,可用於生產環境,無需付費。
只需兩份IronPDF Lite 授權的價格,即可購買Iron Software 的全套文件庫。
立即下載 IronPDF,即可開始從 PDF 檔案中擷取資料!
常見問題解答
如何在 C# 中從 PDF 中提取文本?
您可以使用IronPDF的ExtractAllText方法從PDF文檔中提取所有文本。此方法通過允許輕鬆訪問PDF的文本內容來簡化過程。
使用C#從PDF中提取圖像的過程是什麼?
使用IronPDF,您可以利用ExtractAllImages方法從PDF中提取圖像。此方法能有效檢索PDF文件中嵌入的所有圖像。
如何在C#項目中安裝PDF操作庫?
要在C#項目中安裝IronPDF,您可以使用Package Manager Console執行命令Install-Package IronPDF,或通過Visual Studio中的NuGet Package Manager UI安裝套件。
可以在C#中處理加密的PDF嗎?
是的,IronPDF允許您通過FromFile方法打開和操作加密的PDF文件,您可以提供文件名和密碼以訪問內容。
我可以在C#中從PDF的特定頁面提取數據嗎?
IronPDF使您能夠遍歷PDF文檔的每一頁,並使用諸如ExtractTextFromPage和ExtractImagesFromPage等方法從特定頁面提取數據。
C#的PDF庫提供了哪些附加功能?
除了數據提取,IronPDF還提供渲染圖表、添加條碼、使用密碼增強文檔安全性、水印以及以編程方式處理PDF表單等功能。
怎樣在 C# 中將 HTML 轉換為 PDF?
您可以使用IronPDF的RenderHtmlAsPdf方法將HTML字串轉換為PDF,這對於從網頁內容創建PDF文檔特別有用。
C# PDF庫有試用版嗎?
在開發期間,IronPDF可以免費使用,允許您測試其功能。用於生產的話需要商業許可,但也提供免費試用。
如何開始使用C#庫從PDF中提取數據?
要開始使用IronPDF進行數據提取,請下載該庫,創建或打開Visual Studio中的C#項目,安裝IronPDF,並按照代碼示例高效地從PDF中提取文本和圖像。
.NET 10 的相容性:我可以在 .NET 10 中使用 IronPDF 的數據提取功能嗎?
是的 — IronPDF 在 .NET 10 上完全受支持,包括其數據提取功能,如提取文本和圖像。您可以在 .NET 10 項目中使用 IronPDF 而無需特別配置。它支援 .NET 10、.NET 9、.NET 8 及更早的版本加上 .NET Standard 和 .NET Framework。(ironpdf.com)



