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),如果您希望它佔據表單的大部分,則可以將其 @@--CODE-26740--@@ 屬性設為 @@--CODE-26741--@@。
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 並顯示文字"),在 Click 中建立其 Form1.cs 事件處理程序。
首先,請確保已在 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;: 然後,將提取的文字指派給Text屬性,RichTextBox(確保名稱pdfDataRichTextBox與您為 RichTextBox Box 指定的名稱相符)。
這展示了 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 網站購買許可證。 許可證從 $999 開始。
- 您也可以透過免費試用版來評估完整商業版本。
常見問題解答
如何在 C# 應用程式中從 PDF 中提取文本?
您可以使用 IronPDF 的 ExtractAllText() 方法來有效地從 C# 應用程式中的 PDF 文檔中提取所有可辨識的文本內容。
在 C# 中創建 PDF 文本檢視器需要哪些工具?
要在 C# 中創建 PDF 文本檢視器,您需要 Visual Studio 作為開發環境和 IronPDF 庫,您可以通過 NuGet 套件管理器安裝它。
如何在 Windows Forms 應用程序中顯示提取的 PDF 文本?
您可以在 Windows Forms 應用程式中使用 RichTextBox 控件來顯示從 PDF 提取的文本內容,這允許格式化文本顯示。
在 C# 應用程式中選擇 PDF 文件的過程是什麼?
要選擇 PDF 文件,請在您的表單中添加一個按鈕控件,以打開一個文件對話框。這允許用戶浏覽和選擇他們想要處理的 PDF 文件。
如何在 C# 中處理 PDF 處理期間的錯誤?
您可以通過將 PDF 處理代碼封裝在 try-catch 區塊內來處理錯誤,並在發生異常時使用 MessageBox.Show 顯示錯誤消息。
IronPDF 提供哪些其他功能?
IronPDF 提供文本提取之外的功能,包括 HTML 到 PDF 轉換、添加註釋、搜索文本、加密和解密 PDF 打印以及編輯 PDF 表格。
如何在 Visual Studio 中為 PDF 處理設置新的 Windows Forms 專案?
在 Visual Studio 中,選擇“創建新專案”並選擇“Windows Forms App (.NET Framework)”。命名您的專案,然後單擊“創建”以設置 PDF 處理專案。
運行 C# PDF 文本檢視器應用程式需要哪些步驟?
在 Visual Studio 的“生成”菜單中選擇“生成解決方案”,然後按 F5 或單擊“開始”運行該應用程式。使用按鈕選擇一個 PDF 文件並顯示其文本。
IronPDF 是否可以用於 HTML 到 PDF 的轉換?
是的,IronPDF 可以使用 RenderHtmlAsPdf 方法(用於 HTML 字符串)或 RenderHtmlFileAsPdf 方法(用於 HTML 文件)將 HTML 轉換為 PDF。
PDF 文本提取的常見故障排除場景有哪些?
常見問題包括處理非標準字體或加密的 PDF。確保 PDF 文件未被密碼保護,如果文本提取失敗,請檢查字體兼容性。
IronPDF 是否與 .NET 10 相容?
是的—IronPDF支援.NET 10及較早版本(如.NET 9、8、7、6、.NET Core、.NET Standard和.NET Framework),這意味著您可以在.NET 10專案中使用Windows Forms文字查看器而不會有兼容性問題。



