如何打开PDF文件在C#中
作为数字文档的最流行格式之一,PDF允许用户生成发票、打印银行对账单等等。 PDF还允许用户以数字方式签署文档,并提供安全认证。 了解IronPDF在创建、读取和编辑PDF方面的功能。 在本文中,我们将使用IronPDF的C#集成在C#中生成一个PDF文件,并使用Acrobat Reader/Adobe Reader读取PDF。 我们还将在C#中使用IronPDF读取PDF文件。
如何在C#中打开PDF
- 打开Visual Studio并安装
IronPdfNuGet包 - 在代码中添加引用 - 启用可用类和函数的使用
- 声明一个
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声明一个通用对象
为IronPDF的ChromePdfRenderer声明一个通用对象,将帮助你使用IronPDF将任何网页或HTML片段转换为PDF。 通过创建一个通用对象,我们可以在不创建相同类的更多对象的情况下使用它,允许我们多次重用代码。 可以使用多个函数创建IronPDF PDF文件。 我们可以使用字符串、将URL转换为PDF,或HTML文件并将它们转换为PDF,然后将它们保存到所需位置。
我们还可以使用静态函数,而不为ChromePdfRenderer创建任何对象。 静态函数如下:
我们可以使用这些静态方法中的任何一个来生成PDF文件。我们还可以包括设置各种PDF文档选项,如边距、标题、DPI、页眉、页脚、文本等。通过使用ChromePdfRenderOptions,我们可以将参数传递给这些静态方法中的任何一个。
我们可以将ChromePdfRenderOptions声明为所有PDF文档的通用或单独。 这非常简单易用。 我们将使用任何一个非静态函数生成PDF文件,并将其保存到默认位置。
4. 使用RenderHtmlAsPdf
我们可以使用上述任何一个IronPDF函数来创建PDF。 如果你使用函数名RenderHtmlAsPdf,则传递任何字符串作为参数,然后使用从IronPDF保存为Pdf文件选项函数将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)本示例展示了如何使用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上面的代码展示了如何使用IronPDF读取PDF文件。 IronPDF首先从输入的字符串文件名中读取PDF文档,并且还允许用户包含密码(如果有)。 它将读取所有行。 这在我们需要从PDF获取数据时非常有用,因为它减少了人工工作量,不需要人类监督。
结论
IronPDF提供了一种简单易用的方式来创建PDF,步骤简单明了。 IronPDF库可以在各种环境中使用,如Windows窗体、移动应用程序和使用.NET Framework或.Net Core最新版本的Web应用程序。 我们不需要为每个平台单独的库。 我们只需要IronPDF来生成PDF。
IronPDF提供免费试用密钥,你目前可以从Iron Software购买五个产品的捆绑价格套餐。
你可以下载一个用于帮助开始使用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。






