使用IRONPDF 如何打开PDF文件在C#中 Curtis Chau 已更新:七月 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。 我们还将在C#中使用IronPDF读取PDF文件。 如何在C#中打开PDF 打开Visual Studio并安装IronPdf NuGet包 在代码中添加引用 - 启用可用类和函数的使用 声明一个ChromePdfRenderer的通用对象 使用RenderHtmlAsPdf函数 使用System.Diagnostics.Process.Start 1. 打开Visual Studio并安装NuGet包 打开Visual Studio并转到"文件"菜单。选择"新建项目",然后选择控制台应用程序/Windows窗体/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窗体、移动应用程序和使用.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. 你可以下载一个用于帮助开始使用IronPdf的C#文件项目。 常见问题解答 如何在 C# 中生成 PDF? 使用 IronPDF,您可以通过利用 RenderHtmlAsPdf 方法在 C# 中生成 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 文档中的文本内容。 在使用 C# 生成的 PDF 中是否可以添加 CSS 样式? 是的,可以。IronPDF 允许您将 CSS 样式添加到 PDF 中,通过将它们集成到您转换为 PDF 的 HTML 内容中,从而实现丰富的格式和设计。 哪些环境支持使用 IronPDF 进行 PDF 操作? IronPDF 支持多种环境,包括使用 .NET Framework 和 .NET Core 开发的 Windows 窗体、移动应用程序和 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 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 相关文章 已发布十一月 13, 2025 如何在 C# 中合并两个 PDF 字节数组 使用 IronPDF 在 C# 中合并两个 PDF 字节数组。学习通过简单的代码示例从字节数组、内存流和数据库合并多个 PDF 文件。 阅读更多 已发布十一月 13, 2025 如何创建 ASP.NET MVC PDF 查看器 为 ASP.NET MVC 应用程序构建一个强大的 PDF 查看器。显示 PDF 文档,将视图转换为 PDF,并使用 IronPDF 添加交互功能。 阅读更多 已发布十一月 13, 2025 如何构建 .NET HTML 到 PDF 转换器 学习如何使用 IronPDF 在 .NET 中将 HTML 转换为 PDF。 阅读更多 C#以编程方式创建PDF文件
已发布十一月 13, 2025 如何在 C# 中合并两个 PDF 字节数组 使用 IronPDF 在 C# 中合并两个 PDF 字节数组。学习通过简单的代码示例从字节数组、内存流和数据库合并多个 PDF 文件。 阅读更多
已发布十一月 13, 2025 如何创建 ASP.NET MVC PDF 查看器 为 ASP.NET MVC 应用程序构建一个强大的 PDF 查看器。显示 PDF 文档,将视图转换为 PDF,并使用 IronPDF 添加交互功能。 阅读更多