跳過到頁腳內容
使用IRONPDF

如何在C#中打開PDF文件

作為最受歡迎的數位文件格式之一,PDF 允許用戶產生發票、列印銀行對帳單等等。 PDF文件還允許使用者對文件進行數位簽名,並提供安全的身份驗證。 了解 IronPDF 如何輕鬆建立、閱讀和編輯 PDF 文件。 在本文中,我們將使用 IronPDF 的 C# 整合在 C# 中產生 PDF 文件,並使用 Acrobat Reader/Adobe Reader 讀取 PDF 文件。 我們也將使用 IronPDF 在 C# 中讀取 PDF 文件。

如何在 C# 中開啟 PDF

  1. 開啟 Visual Studio 並安裝IronPdf NuGet 套件
  2. 新增程式碼引用-啟用可用類別和函數的使用
  3. ChromePdfRenderer宣告一個公共對象
  4. 使用RenderHtmlAsPdf函數
  5. 使用System.Diagnostics.Process.Start

1. 開啟 Visual Studio 並安裝 NuGet 套件

開啟 Visual Studio,轉到"檔案"選單。選擇"新專案",然後選擇"控制台應用程式/Windows 窗體/WPF 應用程式"。 IronPDF 可用於所有應用程式。 您也可以在 Webform、MVC/MVC Core 等應用程式中使用它。

如何在 C# 中開啟 PDF 文件,圖 1:在 Visual Studio 中建立一個新項目 在 Visual Studio 中建立一個新項目

在適當的文字方塊中輸入專案名稱並選擇檔案路徑。 然後點擊"創建"按鈕。 接下來,選擇所需的 .NET Framework。 現在,該專案將為選定的應用程式產生結構。 如果您選擇了控制台應用程序,它將打開Program.cs文件,您可以在其中輸入代碼並建立/運行應用程式。

如何在 C# 中開啟 PDF 文件,圖 2:在 Visual Studio 中設定 .Net 項目 在 Visual Studio 中設定 .NET 項目

接下來,安裝 NuGet 套件,從 NuGet 安裝 IronPdf。

左鍵單擊項目,將彈出選單。從選單中選擇"NuGet 套件管理器",然後搜尋 IronPDF。 在 NuGet 套件對話方塊中選擇第一個結果,然後按一下"安裝/下載"選項。

如何在 C# 中開啟 PDF 文件,圖 3:在 NuGet 套件管理器中安裝 IronPdf 套件 在 NuGet 套件管理器中安裝 IronPdf 套件

或者:

在 Visual Studio 中,前往"工具"->"NuGet 套件管理器"->"套件管理器控制台"。

在程式包管理器控制台標籤中輸入以下代碼。

Install-Package IronPdf

現在該軟體包將下載/安裝到當前專案中,並可在程式碼中使用。

2. 新增程式碼引用 - 啟用可用類別和函數的使用

如下所示,將引用IronPdf加入程式碼。 這將使我們能夠在程式碼中使用 IronPdf 提供的類別和函數。

3. 為 ChromePdfRenderer 宣告一個公共對象

ChromePdfRenderer from IronPDF聲明一個通用對象,可以幫助您使用 IronPDF 將任何網頁或 HTML 片段轉換為 PDF 。 透過建立一個公共對象,我們就可以在不創建同一類別的任何其他對象的情況下使用它,從而允許我們多次重複使用程式碼。 IronPDF 提供多種功能,可用於建立 PDF 檔案。 我們可以使用字串,將 URL 轉換為 PDF ,或將 HTML 檔案轉換為 PDF,然後將其儲存到所需位置。

我們也可以使用靜態函數,而無需為ChromePdfRenderer建立任何物件。 靜態函數如下:

我們可以使用這些靜態方法中的任何一種來產生 PDF 檔案。我們還可以設定各種 PDF 文件選項,例如邊距、標題、DPI、頁首、頁尾、文字等。透過使用ChromePdfRenderOptions ,我們可以將參數傳遞給這些靜態方法中的任何一種。

我們可以將ChromePdfRenderOptions宣告為每個 PDF 文件通用或單獨的選項。 它非常簡單易用。 我們將使用任意一個非靜態函數來產生 PDF 檔案並將其保存到預設位置。

4. 使用 RenderHtmlAsPdf

我們可以使用上述 IronPDF 函數中的任何一個來建立 PDF 檔案。 如果您使用的是函數名稱RenderHtmlAsPdf ,則傳遞任何字串作為參數,然後使用SaveAs Pdf file option from IronPDF將 PDF 儲存到所需的檔案路徑。 使用 SaveAs 函數時,我們需要將檔案名稱和位置作為參數傳遞;或者,如果我們使用的是 Windows 應用程序,我們可以使用 SaveAs 對話方塊將 PDF 檔案儲存到所需位置。 借助 HTML 字串,我們可以格式化 PDF 文件。 此外,我們可以使用CSS 透過 HTML 設計 PDF 中的文本,並且可以使用任何 HTML 標籤來設計 PDF 文檔,因為 IronPDF 對使用 HTML 標籤沒有任何限制。

當我們使用大量的 HTML 文字時,很難將所有 HTML 文字新增到文字方塊中,因此我們可以使用上面提到的另一種方法RenderHtmlFileAsPdf ,它可以幫助我們將所有 HTML 轉換為 PDF 文件。 透過這種方法,我們可以新增大型HTML檔案。 此外,我們還可以在這些 HTML 檔案中包含外部 CSS 檔案、外部圖片等。

IronPDF 也可以使用RenderUrlAsPdf函數來幫助我們列印任何連結中的資料。 此函數處理連結以產生 PDF,並使用SaveAs函數將 PDF 文件儲存到所需的文件路徑。 IronPDF 的這項功能將包含網站上的所有 CSS 和圖像。

以下程式碼展示了 IronPDF 函數的範例。

using IronPdf; // Ensure you add the IronPdf namespace

// Create an instance of the ChromePdfRenderer class
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Render a PDF from a simple HTML string
PdfDocument pdf = renderer.RenderHtmlAsPdf("Hello IronPdf");

// Specify the path where the resulting PDF will be saved
var outputPath = "DemoIronPdf.pdf";

// Save the PDF document to the specified path
pdf.SaveAs(outputPath);

// Open the resulting PDF document using the default associated application
System.Diagnostics.Process.Start(outputPath);
using IronPdf; // Ensure you add the IronPdf namespace

// Create an instance of the ChromePdfRenderer class
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Render a PDF from a simple HTML string
PdfDocument pdf = renderer.RenderHtmlAsPdf("Hello IronPdf");

// Specify the path where the resulting PDF will be saved
var outputPath = "DemoIronPdf.pdf";

// Save the PDF document to the specified path
pdf.SaveAs(outputPath);

// Open the resulting PDF document using the default associated application
System.Diagnostics.Process.Start(outputPath);
Imports IronPdf ' Ensure you add the IronPdf namespace

' Create an instance of the ChromePdfRenderer class
Private renderer As New ChromePdfRenderer()

' Render a PDF from a simple HTML string
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("Hello IronPdf")

' Specify the path where the resulting PDF will be saved
Private outputPath = "DemoIronPdf.pdf"

' Save the PDF document to the specified path
pdf.SaveAs(outputPath)

' Open the resulting PDF document using the default associated application
System.Diagnostics.Process.Start(outputPath)
$vbLabelText   $csharpLabel

本範例展示如何使用 IronPDF 函數從字串產生 PDF 檔案。 在這段程式碼中,我們建立了一個ChromePdfRenderer的實例對象,然後藉助RenderHtmlAsPdf函數使用該實例物件產生 PDF 檔案。最後,我們使用 SaveAs IronPDF 函數將 PDF 檔案儲存到指定路徑。 如果沒有指定檔案路徑,檔案將保存在程式的執行位置。

5. 使用 System.Diagnostics.Process.Start 預覽 PDF 文件

最後一步,我們使用System.Diagnostics.Process.Start來預覽 PDF 檔案。此函數會呼叫命令列函數,從指定路徑開啟 PDF 檔案。 如果我們安裝了PDF閱讀器,它將在閱讀器中開啟已儲存的PDF檔案。 如果沒有 PDF 閱讀器,它會以對話方塊的形式打開,我們需要從對話方塊中選擇開啟 PDF 的程式。

如何在 C# 中開啟 PDF 文件,圖 4:預設 PDF 閱讀器中顯示的 PDF 文件 預設 PDF 閱讀器中顯示的 PDF 文件

我們可以使用 IronPDF 讀取 PDF 文件,它會逐行讀取 PDF 文件。我們甚至可以使用 IronPDF 開啟受密碼保護的 PDF 檔案。 以下程式碼示範如何讀取 PDF 文件。

using IronPdf; // Ensure you add the IronPdf namespace

// Open a password-protected PDF
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<System.Drawing.Image> allImages = pdf.ExtractAllImages();

// Iterate through each page in the document
for (var index = 0; index < pdf.PageCount; index++)
{
    // Page numbers are typically 1-based, so add 1 to the index
    int pageNumber = index + 1;

    // Extract text from the current page
    string text = pdf.ExtractTextFromPage(index);

    // Extract images from the current page
    IEnumerable<System.Drawing.Image> images = pdf.ExtractImagesFromPage(index);
}
using IronPdf; // Ensure you add the IronPdf namespace

// Open a password-protected PDF
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<System.Drawing.Image> allImages = pdf.ExtractAllImages();

// Iterate through each page in the document
for (var index = 0; index < pdf.PageCount; index++)
{
    // Page numbers are typically 1-based, so add 1 to the index
    int pageNumber = index + 1;

    // Extract text from the current page
    string text = pdf.ExtractTextFromPage(index);

    // Extract images from the current page
    IEnumerable<System.Drawing.Image> images = pdf.ExtractImagesFromPage(index);
}
Imports IronPdf ' Ensure you add the IronPdf namespace

' Open a password-protected PDF
Private pdf As PdfDocument = PdfDocument.FromFile("encrypted.pdf", "password")

' Extract all text from the PDF document
Private allText As String = pdf.ExtractAllText()

' Extract all images from the PDF document
Private allImages As IEnumerable(Of System.Drawing.Image) = pdf.ExtractAllImages()

' Iterate through each page in the document
For index = 0 To pdf.PageCount - 1
	' Page numbers are typically 1-based, so add 1 to the index
	Dim pageNumber As Integer = index + 1

	' Extract text from the current page
	Dim text As String = pdf.ExtractTextFromPage(index)

	' Extract images from the current page
	Dim images As IEnumerable(Of System.Drawing.Image) = pdf.ExtractImagesFromPage(index)
Next index
$vbLabelText   $csharpLabel

以上程式碼展示如何使用 IronPDF 讀取 PDF 檔案。 IronPDF 首先從輸入的字串文件名中讀取 PDF 文檔,並且還允許用戶在需要時添加密碼。 它會讀取所有行。 當我們需要從 PDF 文件中獲取數據時,這非常有用,因為它減少了人工工作量,而且不需要任何人工監督。

請查看我們關於 PDF 安全性和密碼處理的程式碼範例

結論

IronPDF 提供了一種簡單易用的方式來建立 PDF 文件,步驟清晰明了。 IronPDF 程式庫可以在各種環境中使用,例如 Windows Forms、行動應用程式和使用 .NET Framework 或 .NET Core 最新版本的 Web 應用程式。 我們不需要為每個平台單獨編寫一個函式庫。 我們只需要 IronPDF 來產生 PDF 文件。

IronPDF 提供免費試用金鑰,目前您可以以捆綁價格購買 Iron Software 的五款產品。

您可以下載一個 C# 檔案專案來幫助您開始使用 IronPdf

常見問題解答

如何用 C# 產生 PDF?

使用 IronPDF,您可以利用 RenderHtmlAsPdf 方法在 C# 中生成 PDF,該方法可將 HTML 字串或檔案轉換為 PDF 格式。然後,您可以使用 SaveAs 方法儲存 PDF。

在 C# 專案中設定 IronPDF 應該遵循哪些步驟?

要在 C# 專案中設定 IronPDF,請透過 Visual Studio 的 NuGet Package Manager 安裝 IronPdf NuGet 套件。接下來,在您的程式碼中加入必要的參考,並開始使用 IronPDF 的類別和方法來操作 PDF。

如何在 C# 中開啟 PDF 檔案?

您可以使用 IronPDF 在 C# 中開啟 PDF 檔案,方法是先使用 IronPDF 的方法載入 PDF 文件,然後再使用 System.Diagnostics.Process.Start 檢視 PDF 文件,此時會使用預設的 PDF 閱讀器啟動 PDF。

IronPDF 可以處理有密碼保護的 PDF 檔案嗎?

是的,IronPDF 可以處理有密碼保護的 PDF 檔案。使用 IronPDF 的功能開啟檔案時,您需要提供密碼,使您能夠存取和處理安全的 PDF 文件。

如何使用 C# 從 PDF 擷取文字?

要使用 C# 從 PDF 中提取文字,您可以使用 IronPDF 的 ExtractAllText 方法,該方法可從 PDF 文件中擷取並返回文字內容。

是否可以在使用 C# 生成的 PDF 中添加 CSS 样式?

是的,這是可能的。IronPdf 可讓您將 CSS 樣式整合到轉換成 PDF 的 HTML 內容中,從而為 PDF 添加豐富的格式和設計。

使用 IronPDF 處理 PDF 支援哪些環境?

IronPdf 支援多種環境,包括 Windows Forms、行動應用程式,以及使用 .NET Framework 和 .NET Core 開發的網頁應用程式,提供應用程式開發的彈性。

在購買 IronPDF 之前,我該如何試用?

IronPdf 提供免費試用版本。在決定購買之前,您可以使用試用鑰匙探索 IronPdf 的特性和功能。

使用 IronPDF 在 C# 中生成 PDF 有哪些優點?

IronPDF 透過提供強大的功能,如 HTML 至 PDF 轉換、密碼保護和內容擷取,簡化了在 .NET 應用程式中處理 PDF 的程序,進而簡化了在 C# 中產生 PDF 的程序。

如何在不儲存的情況下預覽 C# 中產生的 PDF?

若要在 C# 中預覽生成的 PDF 而不將其儲存,請使用 IronPDF 生成 PDF,然後使用 System.Diagnostics.Process.Start 直接使用預設的 PDF 閱讀器應用程式開啟 PDF。

.NET 10 兼容性:IronPDF 是否支援 .NET 10 專案?

是 - IronPDF 與 .NET 10 完全相容。它支持在 .NET 10 專案中開箱即用,包括 HTML 到 PDF 的轉換、URL 渲染以及 ChromePdfRenderer 提供的所有功能。在 .NET 10 應用程式中使用 IronPDF 不需要額外的設定。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。