C#教程:使用IronPDF構建PDF文本內容查看器(Windows Forms)
在當今的數位時代,PDF 文件是教育、商業和個人用途中許多工作流程不可或缺的一部分。 它們是共享和呈現各種資料(包括文字、圖像和表格)的標準格式。
雖然在 C# Windows Forms 應用程式中以完整的視覺保真度顯示完整的 PDF 文件可能需要專門的渲染元件,但開發人員通常還有其他需求。 有時,目標是在 C# 中讀取 PDF 文字、提取資料或顯示 PDF 的文字內容以便快速查看、索引或存取。
本文將引導您建立一個專注於此特定任務的應用程式:使用功能強大的 .NET 庫 IronPDF 建立一個簡單的 C# PDF 文字內容檢視器。 您將學習如何使用 IronPDF 載入 PDF 文件,並在 Windows Forms 應用程式中有效地提取和顯示其文字內容。
什麼是 IronPDF?
IronPDF是一個全面的 C# 庫,它使 .NET 開發人員能夠在他們的應用程式中建立、編輯和處理 PDF 文件。 它允許用戶將 HTML、圖像和 SVG 轉換為 PDF 文檔,更重要的是,對於本教程而言,它還可以讀取和提取現有 PDF 中的內容。 IronPDF 的設計宗旨是容易使用,並提供多種功能來處理 PDF 檔案。
建構PDF文字檢視器的要求
要建立這個 C# PDF 文字顯示應用程序,您需要:
- Visual Studio :用於建立 Windows 窗體應用程式的整合開發環境 (IDE)。
- IronPDF :一個 NuGet 包,提供讀取、建立和操作 PDF 文件的功能,包括文字擷取。
圖示展示了 HTML 到 PDF 的轉換概念 IronPDF 也可以從 HTML 建立 PDF,這是與本教學所示的文字擷取功能不同的另一個功能。
使用 IronPDF 在 C# 中建立 PDF 文字內容檢視器的步驟
步驟 1:在 Visual Studio 中建立一個新的 Windows 窗體應用程式
首先,啟動 Visual Studio,然後按一下"建立新專案"。從清單中選擇"Windows 窗體應用程式 (.NET Framework)"或類似的 .NET 範本。
Visual Studio 新專案對話框 Visual Studio 新專案
接下來,為您的專案提供名稱(例如, CSharpPdfTextReader ),然後按一下"建立"按鈕。 這將建立一個新的 Windows 窗體應用程式專案。
步驟 2:安裝 IronPDF 庫
使用 NuGet 套件管理器 GUI
- 在解決方案資源管理器中,右鍵單擊您的項目,然後選擇"管理 NuGet 套件..."
- 前往"瀏覽"標籤並蒐尋"IronPdf"。
- 選擇
IronPdf軟體包,然後按一下"安裝"。
Visual Studio 中的 NuGet 套件管理器正在搜尋 IronPDF 透過 NuGet 套件管理器安裝 IronPDF
使用 NuGet 套件管理器控制台
或者,開啟套件管理員控制台(工具 > NuGet 套件管理員 > 套件管理員控制台),然後執行下列命令:
Install-Package IronPdf
這會將 IronPDF 及其相依性下載並安裝到您的專案中。
步驟 3:向表單新增 RichTextBox 以顯示文字
我們將使用RichTextBox控制項來顯示從 PDF 擷取的文字內容。 RichTextBox適合顯示格式化文本,但在本教程中,它的主要作用是顯示 IronPDF 提取的純文本。 它有效地顯示了文字訊息,而沒有嘗試呈現 PDF 的原始視覺佈局。
新增RichTextBox框:
- 在設計器檢視中開啟表單。
- 前往工具箱(檢視 > 工具箱)。
- 在"常用控制項"下找到
RichTextBox,將其拖曳到窗體上。 - 根據需要調整其大小和位置。 在"屬性"視窗中,您可以設定其
Name(例如,pdfDataRichTextBox),如果您希望它佔據表單的大部分,則可以將其Dock屬性設為Fill。
PDF 檢視器 C# Windows 應用程式(教學),圖 4:造訪 Form1 中的 RickTextBox 在 Form1 中新增一個 RichTextBox 控件,用於顯示擷取的 PDF 文字。
步驟 4:新增一個按鈕以選擇 PDF 文件
在表單中新增一個Button控制項。 使用者點擊此按鈕將開啟文件對話框,並選擇要提取文字的 PDF 檔案。
- 將工具箱中的
Button拖曳到表單上。 - 在屬性視窗中,設定其
Name(例如openBtn)和Text(例如"開啟 PDF 並顯示文字")。
PDF 檢視器 C# Windows 應用程式(教學),圖 5:為 Form1 新增按鈕 在 Form1 中新增一個按鈕以觸發 PDF 選擇
步驟 5:新增 C# 程式碼以載入 PDF 並提取文本
雙擊剛剛新增的按鈕("開啟 PDF 並顯示文字"),在Form1.cs中建立其Click事件處理程序。
首先,請確保已在Form1.cs檔案頂部匯入 IronPDF 命名空間:
using IronPdf;
using System; // For EventArgs, Exception
using System.Windows.Forms; // For OpenFileDialog, MessageBox, DialogResult, etc.using IronPdf;
using System; // For EventArgs, Exception
using System.Windows.Forms; // For OpenFileDialog, MessageBox, DialogResult, etc.Imports IronPdf
Imports System ' For EventArgs, Exception
Imports System.Windows.Forms ' For OpenFileDialog, MessageBox, DialogResult, etc.現在,實作按鈕點擊事件處理程序。 這段程式碼將:
- 提示使用者選擇 PDF 檔案。
- 使用 IronPDF 載入選定的 PDF 檔案。
- 使用 IronPDF 的
ExtractAllText()方法從 PDF 中取得所有文字。 - 在
RichTextBox中顯示擷取的文字。
private void openBtn_Click(object sender, EventArgs e)
{
// Create an OpenFileDialog to open PDF files
var openFileDialog = new OpenFileDialog
{
Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*", // Filter to show only PDFs
Title = "Select a PDF file to extract text from" // Dialog title
};
// Show dialog and check if the user selected a file
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
// It's recommended to set your license key once at application startup.
// License.LicenseKey = "YourIronPdfLicenseKey";
// If no key is set, IronPDF runs in trial mode (watermarks on output, time limits).
// For text extraction, the trial is fully functional for development.
// Load the selected PDF using IronPDF
var pdf = PdfDocument.FromFile(openFileDialog.FileName);
// Extract all text content from the PDF using IronPDF
string extractedText = pdf.ExtractAllText();
// Display the extracted text in the RichTextBox
// (Assuming your RichTextBox is named pdfDataRichTextBox, change if different)
pdfDataRichTextBox.Text = extractedText;
}
catch (Exception ex)
{
// Show error message if an exception occurs
MessageBox.Show("An error occurred while processing the PDF file: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}private void openBtn_Click(object sender, EventArgs e)
{
// Create an OpenFileDialog to open PDF files
var openFileDialog = new OpenFileDialog
{
Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*", // Filter to show only PDFs
Title = "Select a PDF file to extract text from" // Dialog title
};
// Show dialog and check if the user selected a file
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
// It's recommended to set your license key once at application startup.
// License.LicenseKey = "YourIronPdfLicenseKey";
// If no key is set, IronPDF runs in trial mode (watermarks on output, time limits).
// For text extraction, the trial is fully functional for development.
// Load the selected PDF using IronPDF
var pdf = PdfDocument.FromFile(openFileDialog.FileName);
// Extract all text content from the PDF using IronPDF
string extractedText = pdf.ExtractAllText();
// Display the extracted text in the RichTextBox
// (Assuming your RichTextBox is named pdfDataRichTextBox, change if different)
pdfDataRichTextBox.Text = extractedText;
}
catch (Exception ex)
{
// Show error message if an exception occurs
MessageBox.Show("An error occurred while processing the PDF file: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}Private Sub openBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
' Create an OpenFileDialog to open PDF files
Dim openFileDialog As New OpenFileDialog With {
.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*",
.Title = "Select a PDF file to extract text from"
}
' Show dialog and check if the user selected a file
If openFileDialog.ShowDialog() = DialogResult.OK Then
Try
' It's recommended to set your license key once at application startup.
' License.LicenseKey = "YourIronPdfLicenseKey";
' If no key is set, IronPDF runs in trial mode (watermarks on output, time limits).
' For text extraction, the trial is fully functional for development.
' Load the selected PDF using IronPDF
Dim pdf = PdfDocument.FromFile(openFileDialog.FileName)
' Extract all text content from the PDF using IronPDF
Dim extractedText As String = pdf.ExtractAllText()
' Display the extracted text in the RichTextBox
' (Assuming your RichTextBox is named pdfDataRichTextBox, change if different)
pdfDataRichTextBox.Text = extractedText
Catch ex As Exception
' Show error message if an exception occurs
MessageBox.Show("An error occurred while processing the PDF file: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub程式碼解析:
openFileDialog:用於檔案選擇的標準對話框,篩選出 PDF 檔案。PdfDocument.FromFile(openFileDialog.FileName): 此 IronPDF 方法將選取的 PDF 載入到PdfDocument物件中。pdf.ExtractAllText():這是本教學中 IronPDF 的關鍵函數。 它會讀取整個 PDF 文件,並將所有可辨識的文字內容提取到一個字串中。 這對於C# 解析 PDF 文字的場景非常有用。pdfDataRichTextBox.Text = extractedText;: 然後,將擷取的文字指派給RichTextBox的Text屬性(確保名稱pdfDataRichTextBox與您為 RichTextBox 控制項指定的名稱相符)。
這展示了 IronPDF 如何簡化C# 中讀取 PDF 文字的操作,使開發人員能夠以最少的努力透過程式設計方式存取 PDF 內容。
步驟 6:建立並執行您的 C# PDF 文字檢視器應用程式
- 在 Visual Studio 中,前往"生成"選單,然後選擇"生成解決方案"。
- 建置成功後,按"F5"鍵或按"開始"按鈕運行應用程式。
您的應用程式視窗將會出現。 點擊"開啟 PDF 並顯示文字"按鈕,從您的電腦中選擇一個 PDF 文件,然後點擊"開啟"。
PDF 檢視器 C# Windows 應用程式(教學),圖 6:執行應用程式 運行 C# PDF 文字檢視器應用程式
然後, RichTextBox將顯示從所選 PDF 文件中提取的文字內容。
PDF 檢視器 C# Windows 應用程式(教學),圖 7:選擇 PDF 檔案後顯示文字內容 從 PDF 中提取文字內容並顯示在RichTextBox中。
有關在 MAUI 應用程式中以視覺方式呈現 PDF 的資訊(這與本教學的文字擷取重點不同),您可以探索" MAUI 中的 PDF 檢視教學"。
結論:使用 C# 和 IronPDF 輕鬆存取 PDF 文字內容
按照這些步驟,您已經建立了一個 C# Windows Forms 應用程序,該應用程式使用 IronPDF 有效地從 PDF 文件中提取和顯示文字內容。 當您需要在 .NET 應用程式中以程式設計方式存取 PDF 中的文字資訊以進行顯示、分析或進一步處理時,這種方法非常有用。
IronPDF 為 C# PDF 文字擷取提供了強大的功能,而這只是其全面功能集的一部分。 您還可以使用 IronPDF 執行更高級的任務,例如在 PDF 中搜尋文字、新增註釋、列印 PDF 文件、 PDF 加密和解密以及編輯 PDF 表單。
請記住,本教學重點在於一個特定的用例:使 PDF 文字可在 C# 應用程式中存取。 您可以在此基礎上進行調整和擴展,以滿足更複雜的需求。
如果您有興趣探索 IronPDF 的全部潛力:
- 深入閱讀IronPDF 文檔,以取得詳細指南和範例。
- 若要在您的生產應用程式中使用 IronPDF 而不受試用限制,則需要許可證金鑰。 您可以從 IronPDF 網站購買許可證。 許可證起價為$799 。
- 您也可以透過免費試用版來評估完整商業版本。
常見問題解答
如何在 C# 應用程式中從 PDF 擷取文字?
您可以使用 IronPDF 的 ExtractAllText() 方法在您的 C# 應用程式中有效地從 PDF 文件中提取所有可辨識的文字內容。
在 C# 中建立 PDF 文字檢視器需要哪些工具?
要在 C# 中建立 PDF 文字檢視器,您需要 Visual Studio 作為您的開發環境,以及 IronPDF 函式庫,您可以透過 NuGet 套件管理員安裝該函式庫。
如何在 Windows 窗體應用程式中顯示擷取的 PDF 文字?
您可以在 Windows Forms 應用程式中使用 RichTextBox 控件來顯示從 PDF 擷取的文字內容,這可以讓格式化的文字顯示。
在 C# 應用程式中選擇 PDF 檔案的流程為何?
若要選擇 PDF 檔案,請在表單中加入一個 Button 控件,以開啟檔案對話框。這可讓使用者瀏覽並選擇要處理的 PDF 檔案。
如何在 C# 處理 PDF 時處理錯誤?
您可以將 PDF 處理程式碼封裝在 try-catch 區塊中來處理錯誤,並在發生異常時使用 MessageBox.Show 來顯示錯誤訊息。
IronPdf 提供哪些附加功能?
IronPDF 提供文字萃取以外的功能,包括 HTML 到 PDF 的轉換、新增註解、搜尋文字、加密和解密 PDF、列印和編輯 PDF 表單。
如何在 Visual Studio 中為 PDF 處理設定新的 Windows Forms 專案?
在 Visual Studio 中,選擇「建立新專案」,並選擇「Windows 窗體應用程式 (.NET Framework)」。為專案命名,然後按一下「建立」以設定專案進行 PDF 處理。
在 C# 中執行 PDF 文字檢視器應用程式需要哪些步驟?
從 Visual Studio 的建立功能表中選擇「建立解決方案」,然後按 F5 或按一下「開始」以執行應用程式。使用 按鈕選擇 PDF 檔案並顯示其文字。
IronPDF 可以用於 HTML 到 PDF 的轉換嗎?
是的,IronPDF 可以使用 RenderHtmlAsPdf 等方法將 HTML 轉換為 PDF,這些方法適用於 HTML 字串或 RenderHtmlFileAsPdf 適用於 HTML 檔案。
PDF 文本提取中有哪些常見的故障排除情況?
常見的問題包括處理非標準字型或加密的 PDF。確保 PDF 檔案未受密碼保護,並在文字擷取失敗時檢查字型相容性。
IronPDF 是否與 .NET 10 相容?
是 - IronPDF 支援 .NET 10 與早期版本(如 .NET 9、8、7、6、.NET Core、.NET Standard 與 .NET Framework),這表示您可以在 .NET 10 專案上使用 IronPDF 建立 Windows Forms 文字檢視器,而不會有相容性問題。






