使用IRONPDF 如何在C#中打開PDF文件 Curtis Chau 更新日期:7月 28, 2025 Download IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article 作為最受歡迎的數位文件格式之一,PDF允許用戶生成發票、打印銀行對帳單以及更多功能。 PDF還允許用戶進行數位簽署以及提供安全的身份驗證。 了解IronPDF在創建、閱讀和編輯PDF方面的能力。 在本文中,我們將使用IronPDF的C#集成在C#中生成一個PDF文件,並使用Acrobat Reader/Adobe Reader閱讀該PDF。 我們還將使用IronPDF在C#中閱讀PDF文件。 如何在C#中開啟PDF 打開Visual Studio並安裝IronPdf NuGet套件 添加代碼引用 - 啟用可用類和函數的使用 聲明一個ChromePdfRenderer的通用對象 使用RenderHtmlAsPdf函數 使用System.Diagnostics.Process.Start 1. 打開Visual Studio並安裝NuGet套件 打開Visual Studio並進入“檔案”選單。選擇“新專案”,然後選擇控制臺應用程序/Windows Form/WPF應用程序。 IronPDF可以在所有應用程序中使用。 你還可以在像Webform,MVC/MVC Core這樣的應用程序中使用它。 在Visual Studio中創建新專案 在相應的文本框中輸入項目名稱並選擇文件路徑。 然後點擊“創建”按鈕。 接下來,選擇所需的.NET Framework。 現在專案將為所選應用程序生成結構。 如果您選擇了控制臺應用程序,它將會打開Program.cs文件,您可以在其中輸入代碼並構建/運行應用程序。 在Visual Studio中配置.Net專案 接下來安裝NuGet套件從NuGet安裝IronPdf 左鍵單擊專案,將彈出一個選單。從選單中選擇NuGet包管理器,然後搜索IronPDF。 在NuGet包對話框中選擇第一個結果,然後點擊安裝/下載選項。 在NuGet包管理器中安裝IronPdf套件 或其他方式: 在Visual Studio中轉到工具 -> NuGet包管理器 -> 包管理器控制台 在包管理器控制台標籤中輸入以下代碼。 Install-Package IronPdf 現在,套件將在當前專案上下載/安裝並可以在代碼中使用。 2. 添加引用到代碼 – 啟用可用類和函數的使用 如下面所示,將引用IronPdf添加到代碼中。 這將允許我們在代碼中使用IronPdf提供的類和函數。 3. 聲明ChromePdfRenderer的通用對象 Declaring a common object for ChromePdfRenderer from IronPDF will help you convert any web page or HTML snippet into a PDF using IronPDF. 通過創建一個通用對象,我們將能夠不需要創建更多相同類的對象即可以使用,允許我們多次重用代碼。 可以使用多種函數以用IronPDF創建PDF文件。 我們可以使用字串,將URL轉換為PDF或HTML文件並將它們轉換為PDF,然後可以保存到所需位置。 我們還可以使用靜態函數而不創建任何ChromePdfRenderer對象。 靜態函數如下: StaticRenderHtmlAsPdf StaticRenderHtmlFileAsPdf We can use any one of these static methods to generate a PDF file. We can also include setting various PDF document options such as margins, titles, DPI, headers, footers, text, etc. By using ChromePdfRenderOptions, we can pass parameters to any one of these static methods. 我們可以將ChromePdfRenderOptions聲明為通用或為每個PDF文件單獨設置。 這非常簡單和容易使用。 我們將使用非靜態函數之一來生成PDF文件並將其保存到預設位置。 4. 使用RenderHtmlAsPdf 我們可以使用上述任何IronPDF函數來創建PDF。 If you are using the function name RenderHtmlAsPdf, then pass any string as a parameter and then use the SaveAs Pdf file option from IronPDF function to save the PDF at the desired file path. 在使用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。 在默認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 offers a free trial key and you can currently buy five products from Iron Software for a bundled price package. 您可以下載一個C#文件專案來幫助開始使用IronPdf。 常見問題解答 如何使用 C# 產生 PDF 檔案? 使用 IronPDF,您可以透過 C# 中的RenderHtmlAsPdf方法產生 PDF 文件,該方法可以將 HTML 字串或文件轉換為 PDF 格式。然後,您可以使用SaveAs方法儲存 PDF 檔案。 如何在 C# 專案中設定 IronPDF? 若要在 C# 專案中安裝 IronPDF,請透過 Visual Studio 的 NuGet 套件管理器安裝 IronPDF NuGet 套件。接下來,在程式碼中加入必要的引用,即可開始使用 IronPDF 的類別和方法來處理 PDF 文件。 如何在C#中開啟PDF檔案? 您可以使用 IronPDF 在 C# 中開啟 PDF 文件,方法是先使用 IronPDF 的方法載入 PDF 文檔,然後使用System.Diagnostics.Process.Start查看它,這將使用預設的 PDF 閱讀器啟動 PDF。 IronPDF 可以處理受密碼保護的 PDF 檔案嗎? 是的,IronPDF可以處理受密碼保護的PDF檔案。使用IronPDF的功能開啟檔案時,您需要輸入密碼,這樣才能存取和操作受保護的PDF文件。 如何使用 C# 從 PDF 中提取文字? 要使用 C# 從 PDF 中提取文本,可以使用 IronPDF 的ExtractAllText方法,該方法會檢索並返回 PDF 文件中的文本內容。 是否可以將 CSS 樣式新增到使用 C# 產生的 PDF 檔案中? 是的,這完全可行。 IronPDF 讓您可以將 CSS 樣式新增至 PDF 檔案中,方法是將樣式整合到您轉換為 PDF 的 HTML 內容中,從而實現豐富的格式設定和設計。 IronPDF 支援哪些 PDF 操作環境? IronPDF 支援多種環境,包括 Windows Forms、行動應用程式和使用 .NET Framework 和 .NET Core 開發的 Web 應用,為應用程式開發提供了彈性。 購買前如何試用IronPDF? IronPDF 提供免費試用版。您可以使用試用金鑰來體驗 IronPDF 的各項功能,然後再決定是否要購買。 使用 IronPDF 在 C# 中產生 PDF 有哪些好處? IronPDF 透過提供強大的功能(如 HTML 到 PDF 轉換、密碼保護和內容提取),簡化了在 C# 中產生 PDF 的過程,所有這些都簡化了 .NET 應用程式中的 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 機器人,結合科技與創意的樂趣。 相關文章 發表日期 11月 13, 2025 如何在 C# 中合併兩個 PDF 位元組數組 使用 IronPDF 在 C# 中合併兩個 PDF 位元組數組。學習如何透過簡單的程式碼範例,將來自位元組數組、記憶體流和資料庫的多個 PDF 文件合併在一起。 閱讀更多 發表日期 11月 13, 2025 如何在 ASP.NET MVC 中創建 PDF 檢視器 為 ASP.NET MVC 應用程式構建一個強大的 PDF 檢視器。顯示 PDF 文件,將視圖轉換為 PDF,使用 IronPDF 添加互動功能。 閱讀更多 發表日期 11月 13, 2025 如何建立 .NET HTML 轉 PDF 轉換器 學習如何在.NET中使用IronPDF將HTML轉換為PDF。 閱讀更多 C#程式化創建PDF文件
發表日期 11月 13, 2025 如何在 C# 中合併兩個 PDF 位元組數組 使用 IronPDF 在 C# 中合併兩個 PDF 位元組數組。學習如何透過簡單的程式碼範例,將來自位元組數組、記憶體流和資料庫的多個 PDF 文件合併在一起。 閱讀更多
發表日期 11月 13, 2025 如何在 ASP.NET MVC 中創建 PDF 檢視器 為 ASP.NET MVC 應用程式構建一個強大的 PDF 檢視器。顯示 PDF 文件,將視圖轉換為 PDF,使用 IronPDF 添加互動功能。 閱讀更多