C# 使用 IronPDF 打印 PDF 文件
PDF 是使用最广泛的文件格式。 它轻量化并受大多数操作系统支持。 PDF 文件可以轻松地从一个设备共享到另一个设备。 它们提供了便捷的阅读体验,因为它们在任何界面和操作系统上看起来完全相同。 PDFs 可以从所有设备访问,包括手机、平板电脑等。
它们的流行使得所有 C# 开发人员必须知道如何使用 C# 打印 PDF 文件。 这是因为您可能遇到必须打印文档的问题,如发票、工资单、订单单或简历等。这些文档通常是 PDF 文件格式,所以我们必须知道如何使用 C# 打印 PDF 文件。
在 C# 中打印 PDF 文档可以使用多种库。 但您可能会遇到的问题是许可证费用。 它们大多数不是免费的,或者使用起来很困难。 但幸运的是,您不必担心。 我找到了在 C# 中打印 PDF 文件的最简单和最简单的方法。
I will use the IronPDF Library to print PDF documents. 相信我,您会喜欢的!
通过本文您将获得什么?
阅读本文后,您将能够执行以下任务:
- 使用 C# 打印单个 PDF 文档
- 使用 C# 一次性打印多个 PDF 文件
- 将网址转换为PDF格式,然后打印出来。
为了更好地理解本教程,您必须具备 C# 编程的基本知识。
在开始教程之前,让我们首先了解 IronPDF 是什么。
IronPDF:
IronPDF 是一个 .NET 库,为我们提供了读取、创建、操作和打印 PDF 文件的最简单方法。 此库可用于 C# 和 VB .NET 应用程序。 它可以轻松与 ASP .NET、ASP .NET core、MVC、Web Forms 和 Web APIs 在 .NET 和 .NET core 上一起工作。 它允许用户下载 PDF 文件,发送电子邮件,并存储在云中。
您可以使用此链接进一步探索 IronPDF。
让我们开始演示。
打开 Visual Studio
打开 Visual Studio。 创建新的项目或者打开一个现有项目,如果您打算将此功能添加到现有软件中。
我在本教程中使用的是控制台应用程序; 您可以根据您的软件需求选择任何一种。

项目已创建。
安装 NuGet 包
Open the console by clicking on Tools > NuGet Package Manager > Package Manager 控制台 from the Menu bar on the top of the window.

NuGet 包管理器控制台将打开如下所示:

NuGet 包管理器控制台
Write the following command in the console to Install IronPdf NuGet Package and press Enter.
Install-Package IronPrint
NuGet 包将安装如下所示:

安装 NuGet 包
在 C#中打印 PDF 文件
现在,举个例子来演示如何在 C# 中使用 IronPDF 库打印 PDF 文件。
打开 Program.cs 文件。在 Program.cs 文件顶部添加以下命名空间以使用 IronPDF:
using IronPdf;using IronPdf;Imports IronPdf现在,在 Main 函数中编写以下代码:
using System; // Required for 控制台 operations
class Program
{
static void Main()
{
// Load the PDF document from file
using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
// Print the loaded PDF document to the default printer
pdfDocument.Print();
// Print operation success message to console
控制台.WriteLine("File Printed Successfully!");
控制台.ReadKey();
}
}using System; // Required for 控制台 operations
class Program
{
static void Main()
{
// Load the PDF document from file
using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
// Print the loaded PDF document to the default printer
pdfDocument.Print();
// Print operation success message to console
控制台.WriteLine("File Printed Successfully!");
控制台.ReadKey();
}
}IRON VB CONVERTER ERROR developers@ironsoftware.com让我们理解这些代码。
- 以下这行代码将 PDF 文件加载到内存中。 "Sample.Pdf" 是文件名,而 D 驱动器是我的文件位置。 您必须写一个完整的文件路径以及文件名称。
using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");using PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");Using pdfDocument As PdfDocument = PdfDocument.FromFile("D:\Sample.pdf")
End UsingPrint()函数将使用您的默认打印机打印加载的文件。
测试程序
按 Ctrl + F5 运行程序。

控制台
在 C#中打印多个 PDF 文档
我将向您展示如何使用 IronPDF 在 C# 中打印多个 PDF 文档。

14 个 PDF 文件
在此示例中,我有 14 个需要打印的 PDF 文档。 我将它们命名为编号 1 - 14。您可以用 ID 保存所有文件,或者可以将文件位置保存在数据库中。 从数据库中提取文件路径和文件名,并将其保存于数组或列表中。迭代该数组或列表并全部打印。
在 Main 函数中编写以下代码:
using System; // Required for 控制台 operations
class Program
{
static void Main()
{
// Loop through each file number and print
for (int i = 1; i <= 14; i++)
{
// Load each PDF document from file
using PdfDocument pdfDocument = PdfDocument.FromFile($@"D:\Print\{i}.pdf");
// Inform which file is being printed
控制台.WriteLine("Printing File: {0}.pdf", i);
// Print the loaded PDF document to the default printer
pdfDocument.Print();
// Print operation success message to console
控制台.WriteLine("{0}.pdf Printed Successfully!", i);
}
控制台.ReadKey();
}
}using System; // Required for 控制台 operations
class Program
{
static void Main()
{
// Loop through each file number and print
for (int i = 1; i <= 14; i++)
{
// Load each PDF document from file
using PdfDocument pdfDocument = PdfDocument.FromFile($@"D:\Print\{i}.pdf");
// Inform which file is being printed
控制台.WriteLine("Printing File: {0}.pdf", i);
// Print the loaded PDF document to the default printer
pdfDocument.Print();
// Print operation success message to console
控制台.WriteLine("{0}.pdf Printed Successfully!", i);
}
控制台.ReadKey();
}
}IRON VB CONVERTER ERROR developers@ironsoftware.com我使用了一个 for 循环来根据我的文件名生成 1 到 14 的数字。
运行解决方案:
按 Ctrl + F5 运行程序。

控制台
将 URL 转换为 PDF 然后打印:
我将向您展示如何通过将 URL 解析到 IronPDF 来打印网页。 该库将通过解析 HTML 创建一个 PDF 文件。 我们可以使用打印功能打印该 PDF 文档。 让我们考虑一个示例。
在主函数中编写以下代码:
using System; // Required for 控制台 operations
class Program
{
static void Main()
{
// HTML to PDF Renderer
IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
// Inform about the printing operation
控制台.WriteLine("Printing File");
// Render URL as PDF
using PdfDocument PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/how-to/print-pdf/#advanced-printing");
// Print the PDF document
PdfDocument.Print();
// Print operation success message to console
控制台.WriteLine("File Printed Successfully!");
}
}using System; // Required for 控制台 operations
class Program
{
static void Main()
{
// HTML to PDF Renderer
IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
// Inform about the printing operation
控制台.WriteLine("Printing File");
// Render URL as PDF
using PdfDocument PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/how-to/print-pdf/#advanced-printing");
// Print the PDF document
PdfDocument.Print();
// Print operation success message to console
控制台.WriteLine("File Printed Successfully!");
}
}IRON VB CONVERTER ERROR developers@ironsoftware.com此程序将 https://ironpdf.com/how-to/print-pdf/#advanced-printing 解析为 PDF,然后 Print() 函数将使用默认打印机打印 PDF。
让我们运行这个程序。
运行程序:
按 Ctrl + F5 运行解决方案。

屏幕上显示的输出
Print() 函数将使用默认打印机打印文档。 可能会出现我们不想使用默认打印机的情况。 在这种情况下,IronPDF 为我们提供了一行代码来更改打印机名称。
更改打印机名称:
让我们用一个示例来帮助我们进一步理解。 使用以下代码来更改默认打印机:
using System; // Required for 控制台 operations
class Program
{
static void Main()
{
// Load the PDF document from file
PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
// Change to a different printer by name
pdfDocument.GetPrintDocument().PrinterSettings.PrinterName = "Microsoft Print to PDF";
// Print the PDF document
pdfDocument.Print();
// Print operation success message to console
控制台.WriteLine("File Printed Successfully!");
}
}using System; // Required for 控制台 operations
class Program
{
static void Main()
{
// Load the PDF document from file
PdfDocument pdfDocument = PdfDocument.FromFile(@"D:\Sample.pdf");
// Change to a different printer by name
pdfDocument.GetPrintDocument().PrinterSettings.PrinterName = "Microsoft Print to PDF";
// Print the PDF document
pdfDocument.Print();
// Print operation success message to console
控制台.WriteLine("File Printed Successfully!");
}
}IRON VB CONVERTER ERROR developers@ironsoftware.com您可以看到我只添加了一行额外代码来指定打印机。 我为演示指定了 Microsoft Print to PDF。
运行程序:
按下 Ctrl + F5 运行程序。
以下对话框将出现,要求我们输入要保存的文件名,因为我们正在使用 Microsoft Print to PDF。

指定文件名并单击保存按钮。 我指定了 "SamplePrint.Pdf"。 将显示以下控制台输出:

Microsoft Print to PDF 已在我的 D 驱动器中打印了一个 PDF 文件。让我们看看它。

我希望这个教程对您有帮助、互动性强且易于理解。 您可以在下面的评论部分提出问题或对此教程给予反馈。
常见问题解答
我如何使用 IronPDF 在 C# 中打印 PDF 文件?
要在 C# 中使用 IronPDF 打印 PDF 文件,使用 PdfDocument.FromFile 方法加载 PDF 文档,然后在加载的文档上调用 Print 方法。
IronPDF 可以顺序打印多个 PDF 文件吗?
是的,IronPDF 可以通过遍历文件路径集合并为每个文档调用 Print 方法来打印多个 PDF 文件。
我如何在 C# 中将 URL 转换为 PDF 然后打印它?
使用 IronPDF,通过 HtmlToPdf.RenderUrlAsPdf 方法将 URL 转换为 PDF。然后,在生成的 PDF 文档上使用 Print 方法进行打印。
使用 IronPDF 打印 PDF 时可以选择特定的打印机吗?
是的,您可以通过在使用 PdfDocument 类的 GetPrintDocument 方法时设置 PrinterSettings 对象内的 PrinterName 属性来选择特定的打印机。
IronPDF 支持哪些平台的 PDF 打印?
IronPDF 支持在 .NET Framework、.NET Core 和 .NET 5 和 6 应用程序上打印 PDF,并且与 Windows、macOS、Android 和 iOS 平台兼容。
我如何开始使用 .NET 库在 C# 中打印 PDF?
要开始使用 IronPDF 打印 PDF,请通过 Visual Studio 中的 NuGet 包管理器安装该库,并遵循 Iron Software 提供的文档和示例。
使用 PDF 进行文档共享有哪些好处?
PDF 在文档共享方面具有优势,因为它们轻量级,在不同设备上保持一致的格式,并且被大多数操作系统支持。
我是否需要高级 C# 技能来使用 IronPDF 进行打印?
基本的 C# 知识就足以使用 IronPDF 进行打印任务,因为该库提供了简单的方法和示例来帮助开发者。









