如何在 C# 中静默打印 PDF 文件
PDF 文档是一种便携式文档格式,可以以请求的格式存储和传输数据。 它保留存储数据的格式,使用户能够专注于处理数字文档的其他方面。 打印数字文档是一项繁琐的任务,因为数据在不同平台上显示不同。 但通过发送数据到 PDF 文档有助于保留打印格式。 然而,程序化打印 PDF 文件对于 C# 开发人员来说可能是一种挑战。 感谢 IronPDF - C# PDF 库,让打印 PDF 文件的过程变得极其简单且无忧。
本文将解释如何使用 IronPDF 库在 C# 中无提示地打印 PDF 文档。
如何在 C# 中静默打印 PDF 文件
- 安装 C# 库以静默打印 PDF 文件
- 使用
Print方法以默认设置打印 PDF 文档 - 设置
PrinterName属性以指定打印机 - 在 C# 中使用
PrinterResolution类设置自定义分辨率 - 将 PDF 静默打印到指定目录而不打开打印机对话框
IronPDF - C# PDF 库
IronPDF 是一个 C# .NET 库,允许开发人员创建、读取和编辑 PDF 文档。 它是一个顶级 C# 库,优先考虑准确性、易用性和速度。 它专为 C#, F#, & VB.NET 设计,并且与 .NET 7, 6, 5, Core, Standard, or Framework 高度兼容。它利用 IronPDF 强大的 Chromium 引擎从 HTML 生成适用于 Web、桌面和控制台的 PDF。
此外,IronPDF 允许用户轻松操作和编辑 PDF,添加标题和页脚,从 PDF 中提取文本和图像。
一些重要功能包括:
- 从不同文件格式加载和生成 PDF 文件
- 使用默认打印机保存和打印 PDF 文件
- 合并和拆分 PDF 文件
- 无需 Adobe Reader 的 PDF 编辑器
使用 IronPDF 库在 C# 中无提示打印 PDF 文档的步骤
要无提示打印 PDF 文档,首先需要在本地计算机上安装以下组件。
- Visual Studio - 这是 C# 开发的官方 IDE,必须安装在计算机上。 您可以从 Visual Studio 网站下载和安装。
创建项目 - 要在 C# 中为 PDF 打印创建控制台应用程序,请执行以下步骤使用 Visual Studio 2022:
打开 Visual Studio 并点击创建新项目

Visual Studio
选择 C# 控制台应用程序并点击下一步

新建项目对话框
现在,输入项目名称,选择位置,然后点击下一步

Web 窗体
为您的应用程序选择最新的 .NET 框架。 我们将使用稳定版本 6.0。

附加信息
- 点击创建,控制台项目创建完成,我们可以开始程序化打印 PDF 文档。
安装 IronPDF - 有三种方法可以下载和安装 IronPDF 库。 具体如下
使用 Visual Studio - Visual Studio 具有 NuGet 包管理器,可以帮助在 C# 项目中安装 NuGet 包。
- 单击菜单栏中的工具,或者
在解决方案资源管理器中右击项目文件

解决方案资源管理器

项目菜单 > 管理 NuGet 程序包
打开后,在 NuGet 包管理器中浏览 IronPDF 并安装,如下所示:

从 NuGet 包安装 IronPDF
- 直接下载 NuGet 包 - 另一种下载 IronPDF 的方法是导航到 NuGet 网站并直接下载包。 这是链接https://www.nuget.org/packages/IronPdf/。
- 下载 IronPDF .DLL 库 - IronPDF 也可以直接从 IronPDF 网站下载。导航到:IronPDF DLL 下载进行安装。 在项目中引用 .DLL 文件以使用它。
生成 PDF 文件和打印作业
在此我们将从一个 URL 生成 PDF 文件。 创建 PDF 文件很简单,通常是一个两步过程。 以下代码示例生成 PDF:
using IronPdf;
// Initialize a new instance of ChromePdfRenderer.
ChromePdfRenderer Renderer = new ChromePdfRenderer();
// Render the specified URL as a PDF document.
PdfDocument Pdf = Renderer.RenderUrlAsPdf("https://ironpdf.com/");using IronPdf;
// Initialize a new instance of ChromePdfRenderer.
ChromePdfRenderer Renderer = new ChromePdfRenderer();
// Render the specified URL as a PDF document.
PdfDocument Pdf = Renderer.RenderUrlAsPdf("https://ironpdf.com/");Imports IronPdf
' Initialize a new instance of ChromePdfRenderer.
Private Renderer As New ChromePdfRenderer()
' Render the specified URL as a PDF document.
Private Pdf As PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/")通过上述代码创建了一个 PDF 文档对象,准备好打印。 接下来,我们将使用默认打印机纸张打印 PDF文档。 代码是一行,具体如下:
// Print the PDF document using the default printer settings.
Pdf.Print();// Print the PDF document using the default printer settings.
Pdf.Print();' Print the PDF document using the default printer settings.
Pdf.Print()此 Print 方法将 PDF 发送到默认打印机进行打印。
高级打印选项
对于无提示打印,IronPDF 提供了各种高级打印选项。
PdfDocument.GetPrintDocument 方法被使用,结果存储在 System.Drawing.Printing.PrintDocument 对象中。 代码很简单,具体如下:
// Remember to add assembly reference to System.Drawing.dll in project
// Get the print document for the PDF.
System.Drawing.Printing.PrintDocument PrintPDF = Pdf.GetPrintDocument();// Remember to add assembly reference to System.Drawing.dll in project
// Get the print document for the PDF.
System.Drawing.Printing.PrintDocument PrintPDF = Pdf.GetPrintDocument();' Remember to add assembly reference to System.Drawing.dll in project
' Get the print document for the PDF.
Dim PrintPDF As System.Drawing.Printing.PrintDocument = Pdf.GetPrintDocument()指定打印机名称
IronPDF 还提供打印到特定打印机的机会。 要指定名称,请使用PrinterSettings.PrinterName 属性。 首先,我们需要获取当前的 PDF 文档对象。 代码示例如下:
using (var printDocument = pdfDocument.GetPrintDocument())
{
// Specify the printer name.
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
// Send the print job to the specified printer.
printDocument.Print();
}using (var printDocument = pdfDocument.GetPrintDocument())
{
// Specify the printer name.
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
// Send the print job to the specified printer.
printDocument.Print();
}Using printDocument = pdfDocument.GetPrintDocument()
' Specify the printer name.
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
' Send the print job to the specified printer.
printDocument.Print()
End Using设置打印机分辨率
另一个很酷的功能是设置打印机分辨率。 我们可以根据输出控制要打印、显示的像素数量。 PDF 文档的 DefaultPageSettings.PrinterResolution 属性可用于设置分辨率。 这是一个非常快速的代码示例:
// Set the custom resolution for the printer.
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
Kind = PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
// Send the print job with the custom resolution settings.
printDocument.Print();// Set the custom resolution for the printer.
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
Kind = PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
// Send the print job with the custom resolution settings.
printDocument.Print();' Set the custom resolution for the printer.
printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
.Kind = PrinterResolutionKind.Custom,
.X = 1200,
.Y = 1200
}
' Send the print job with the custom resolution settings.
printDocument.Print()PrintToFile 方法
PdfDocument 类提供 PrintToFile 方法,允许我们在 C# 中打印 PDF 到文件。 它将 pathToFile 作为参数来直接将文件打印到该位置,而无需打开打印机对话框。 代码简单如下:
// Print the document to a specified file location.
printDocument.PrintToFile("PathToFile", false);// Print the document to a specified file location.
printDocument.PrintToFile("PathToFile", false);' Print the document to a specified file location.
printDocument.PrintToFile("PathToFile", False)完整代码示例如下:
using IronPdf;
// Initialize the PDF renderer and create the PDF document.
ChromePdfRenderer Renderer = new ChromePdfRenderer();
PdfDocument pdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/");
using (var printDocument = pdfDocument.GetPrintDocument())
{
// Specify the printer name.
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
// Set a custom print resolution.
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
Kind = PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
// Execute the print job.
printDocument.Print();
}using IronPdf;
// Initialize the PDF renderer and create the PDF document.
ChromePdfRenderer Renderer = new ChromePdfRenderer();
PdfDocument pdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/");
using (var printDocument = pdfDocument.GetPrintDocument())
{
// Specify the printer name.
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
// Set a custom print resolution.
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
Kind = PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
// Execute the print job.
printDocument.Print();
}Imports IronPdf
' Initialize the PDF renderer and create the PDF document.
Private Renderer As New ChromePdfRenderer()
Private pdfDocument As PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/")
Using printDocument = pdfDocument.GetPrintDocument()
' Specify the printer name.
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
' Set a custom print resolution.
printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
.Kind = PrinterResolutionKind.Custom,
.X = 1200,
.Y = 1200
}
' Execute the print job.
printDocument.Print()
End Using当代码执行时,它将 URL 转换为 PDF 文档。 然后为了无提示打印 PDF 文档,使用了 GetPrintDocument 方法。 在成功编译和执行程序文件时,会出现打印机对话框,以将其保存为 PDF 文档。 随后使用指定的打印机名称保存 PDF。


摘要
在本文中,我们深入研究了如何使用 IronPDF 无提示打印 PDF 文档。 IronPDF 在 PDF 打印时提供了很多有用的选项。 它还可以跟踪打印的页面,并允许您在页范围之间打印。
无提示打印以及其他打印选项使得 IronPDF 成为在 C# 中处理 PDF 的杰出库。
IronPDF 可帮助将数据从不同格式转换为 PDF,反之亦然。 它使开发人员能够轻松地将 PDF 功能集成到应用程序开发过程中。 此外,它无需 Adobe Acrobat Reader 查看和编辑 PDF 文档。
IronPDF 免费供个人开发,可以获得商业用途许可。 It provides a free trial license to access and test out the full functionality of the library. 您可以在此链接上查看更多详细信息。
常见问题解答
如何在 C# 中静默打印 PDF?
您可以通过使用 IronPDF 的 Print 方法在 C# 中静默打印 PDF。这允许您在不打开打印机对话框的情况下打印,确保文档的格式得到保留。
设置 IronPDF 进行静默 PDF 打印所需的步骤是什么?
要设置 IronPDF 进行静默 PDF 打印,通过 NuGet 包管理器安装库或下载 .DLL 文件。然后,使用 Print 方法配置静默打印设置并指定如 PrinterName 和 PrinterResolution 等打印机属性。
如何确保打印时 PDF 格式保持不变?
IronPDF 通过允许您配置特定的打印机设置来帮助保持 PDF 格式,例如设置 PrinterResolution 属性,确保打印输出与原始文档匹配。
我可以在 C# 中将 PDF 打印到文件位置吗?
是的,IronPDF 提供了一个 PrintToFile 方法,允许您在不打开打印机对话框的情况下,将 PDF 直接打印到指定的文件位置。
是否可以在不使用 Adobe Acrobat Reader 的情况下打印 PDF?
是的,IronPDF 使您能够在不需要 Adobe Acrobat Reader 的情况下打印、查看和编辑 PDF,使其成为处理 C# 应用程序中 PDF 文档的灵活工具。
IronPDF 是否支持跨平台的 PDF 打印?
是的,IronPDF 支持跨平台的 PDF 打印,提供 Windows、macOS、Android 和 iOS 之间的兼容性,使其适用于各种开发环境。
IronPDF 提供哪些试用选项?
IronPDF 对个人开发是免费的,并提供试用许可证,允许您在购买商业许可证之前测试其全部功能。
如何指定特定的打印机以使用 IronPDF 打印 PDF?
您可以通过在 IronPDF 中设置 PrinterSettings.PrinterName 属性来指定特定的打印机,将打印任务指向您想要的打印机。
推荐使用什么 IDE 来开发使用 IronPDF 的 C# 应用程序?
Visual Studio 是推荐用于开发使用 IronPDF 的 C# 应用程序的集成开发环境 (IDE),提供一个用于编码和项目管理的强大平台。
如何排除 C# 中静默 PDF 打印的问题?
如果在 C# 中遇到静默 PDF 打印问题,请确保 IronPDF 正确安装,并准确配置如 PrinterName 和 PrinterResolution 的打印机设置。查阅 IronPDF 文档也可以提供有用的故障排除建议。









