在C# .NET中程式庫中製作PDF
在現今的數位時代,PDF檔案是包含教育、商業和個人使用的許多工作流程中不可或缺的一部分。 它們是用於共享和呈現各種資料的標準格式,包括文字、圖像和表格。
雖然在C# Windows Forms應用程式中顯示完整PDF文件需要專用的渲染元件,但開發人員通常有其他需求。 有時,目標是用C#讀取PDF文字、提取資料,或顯示PDF的文字內容供快速查看、編索引或提高無障礙性。
本文將指導您建立一個專注於此特定任務的應用程式:使用IronPDF這個強大的.NET庫建構一個簡單的C# PDF文字內容查看器。 您將學習如何使用IronPDF載入PDF,並在Windows Forms應用程式中有效地提取和顯示其文字內容。
什麼是IronPDF?
IronPDF是一個全面的C#庫,讓.NET開發人員能在其應用程式中建立、編輯和處理PDF文件。 它允許使用者將HTML、圖像和SVG轉換為PDF文件,並且對於本教程而言,從現有的PDF中讀取和提取內容相當重要。 IronPDF專為易於使用設計,並提供廣泛的功能來操作PDF文件。
構建PDF文字查看器的需求
要建立此C# PDF文字顯示應用程式,您將需要:
- Visual Studio: 用於建立Windows Forms應用程式的整合開發環境(IDE)。
- IronPDF: 提供讀取、建立和操縱PDF文件功能的NuGet套件,包括文字提取。
IronPDF還可以從HTML建立PDF,這是與本教程中所顯示的文字提取不同的一個功能。
使用IronPDF在C#中建立PDF文字內容查看器的步驟
步驟1:在Visual Studio中建立新的Windows Forms應用程式
首先,啟動Visual Studio並點擊"建立新項目"。從列表中選擇"Windows Forms App (.NET Framework)"或類似的.NET模板。
Visual Studio新項目建立
接下來,為您的項目提供一個名稱(例如,CSharpPdfTextReader)並點擊建立按鈕。 這將設置一個新的Windows Forms應用程式項目。
步驟2:安裝IronPDF程式庫
使用NuGet套件管理器GUI
- 在解決方案管理器中,右鍵點擊您的項目並選擇"管理NuGet套件..."
- 轉到"瀏覽"標籤並搜索"IronPDF"。
- 選擇
IronPdf套件並點擊"安裝"。
通過NuGet套件管理器安裝IronPDF
使用NuGet套件管理器控制台
或者,打開套件管理器控制台(工具 > NuGet套件管理器 > 套件管理器控制台),然後運行以下命令:
Install-Package IronPdf
這將下載並安裝IronPDF及其依賴項到您的項目中。
步驟3:新增一個RichTextBox到您的表單以顯示文字
我們將使用RichTextBox控制項來顯示從PDF提取的文字內容。 RichTextBox適合顯示格式化文字,雖然對於本教程來說,它的主要角色是展示IronPDF提取的純文字。 它有效地顯示了文字資訊,而不嘗試呈現PDF的原始視覺佈局。
要新增RichTextBox:
- 在設計視圖中打開您的表單。
- 轉到工具箱(視圖 > 工具箱)。
- 找到"Common Controls"下的
RichTextBox,將其拖動到您的表單上。 - 根據需要調整其大小和位置。 在屬性窗口中,您可以設置
Fill,如果您想讓它佔據表單的大部分空間。
將RichTextBox新增到Form1以顯示提取的PDF文字
步驟4:新增按鈕以選擇PDF文件
在您的表單上新增Button控制項。 使用者將點擊此按鈕以打開文件對話框並選擇要提取文字的PDF文件。
- 將
Button從工具箱拖到您的表單上。 - 在屬性窗口中,設置
Text(例如,"打開PDF & 顯示文字")。
新增按鈕到Form1以觸發PDF選擇
步驟5: 新增C#程式碼以載入PDF並提取文字
雙擊您剛剛新增的按鈕("打開PDF & 顯示文字")以在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物件。pdf.ExtractAllText():這是本教程的關鍵IronPDF功能。 它遍歷整個PDF,將所有可辨識的文字內容提取到一個字串中。 這對於 C# 解析PDF文字 場景非常有用。Text屬性 (確保名稱pdfDataRichTextBox與您給RichTextBox控制項的名稱匹配)。
這表明IronPDF如何簡化 在C#中讀取PDF文字,讓開發者能夠輕鬆程式化地存取PDF內容。
步驟6:構建並運行您的C# PDF文字查看器應用程式
- 在Visual Studio中,轉到"構建"選單並選擇"構建解決方案"。
- 一旦構建成功,按"F5"或點擊"開始"按鈕來運行應用程式。
您的應用程式窗口將會出現。 點擊"打開PDF & 顯示文字"按鈕,從您的電腦中選擇一個PDF文件,然後點擊"打開"。
運行C# PDF文字查看器應用程式
然後RichTextBox將顯示從所選PDF文件中提取的文字內容。
從PDF提取的文字內容並顯示在RichTextBox中
關於在MAUI應用程式中可視化呈現PDF(不同於本教程的文字提取重點),您可以探索"MAUI教程中的PDF查看"。
結論: 使用C#和IronPDF輕鬆存取PDF文字內容
通過遵循這些步驟,您已經建立了一個使用IronPDF有效提取和顯示PDF文件中的文字內容的C# Windows Forms應用程式。 當您需要程式性地存取PDF中的文字資訊以顯示、分析或在您的.NET應用程式中進一步處理時,這種方法非常有價值。
IronPDF提供了強大的C# PDF文字提取功能,這只是其全面功能集的一部分。 您也可以使用IronPDF執行更高級的任務,如在PDF中進行文字搜尋,新增註釋,列印PDF文件,PDF加密和解密,以及編輯PDF表單。
請記住,本教程側重於一個特定的使用案例:使PDF文字在C#應用程式中可存取。 您可以根據這個基礎進行調整和擴展,以滿足更複雜的需求。
如果您有興趣探索IronPDF的全部潛力:
常見問題
如何在 C# 應用程式中從 PDF 提取文字?
您可以使用 IronPDF 的 ExtractAllText() 方法在 C# 應用程式中高效地從 PDF 文件提取所有可辨識的文字內容。
我需要什麼工具來建立 C# 中的 PDF 文字檢視器?
要在 C# 中建立 PDF 文字檢視器,您需要 Visual Studio 作為開發環境,以及可以透過 NuGet 套件管理器安裝的 IronPDF 程式庫。
如何在 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 中設置新的 Windows Forms 專案以進行 PDF 處理?
在 Visual Studio 中,選擇「建立一個新專案」,選擇「Windows Forms App(.NET Framework)」並為您的專案命名,然後點擊「建立」來設置項目以進行 PDF 處理。
運行 C# 中的 PDF 文字檢視器應用程式需要哪些步驟?
從 Visual Studio 的建構選單中選擇「建構解決方案」,然後按 F5 或點擊「開始」以運行應用程式。使用按鈕來選擇 PDF 文件並顯示其文字。
IronPDF 可以用於 HTML 到 PDF 的轉換嗎?
是的,IronPDF 可以使用 RenderHtmlAsPdf 方法將 HTML 字串或使用 RenderHtmlFileAsPdf 方法將 HTML 檔案轉換為 PDF。
在 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 文字檢視器而不會有相容性問題。

