在生产环境中测试,无水印。
随时随地满足您的需求。
获得30天的全功能产品。
几分钟内就能启动并运行。
在您的产品试用期间,全面访问我们的支持工程团队。
本文探讨了直接打印 PDF 的意义,并演示了强大的 C# 库 IronPDF 如何促进这一过程。
IronPDF 是一个强大的 C# 库,旨在促进 PDF 文档的创建、操作和交互。 有了 IronPDF,开发人员可以毫不费力地从 HTML 内容、图像和其他来源生成 PDF,使其成为一个多功能工具,既能完成简单的 PDF 相关任务,也能完成复杂的 PDF 相关任务。 其功能不仅限于生成 PDF 文档; IronPDF 还能让您以各种方式合并、分割和处理 PDF。 此外,其直观的 API 使初学者和有经验的开发人员都能使用。 其中一些重要功能包括
在处理 PDF 文件时,打印是一项基本要求。 从 C# 应用程序直接打印 PDF 而不显示打印对话框有几个优点。 它通过消除不必要的交互来增强用户体验,使打印任务自动化,并能无缝集成到更大的工作流程中。 IronPDF 简化了这一过程,使开发人员能够保持对打印过程的控制,同时确保打印输出的完整性。
在进入 PDF 打印流程之前,请确保具备以下先决条件:
Visual Studio:这是一个集成开发环境(IDE),您可以在其中创建、编辑和编译您的C#项目。 要下载并安装到您的计算机上,请访问官方Visual Studio 网站。
创建 Visual Studio 控制台项目是一个简单明了的过程。 请按照以下步骤使用 Visual Studio 创建一个新的控制台应用程序:
打开 Visual Studio:启动您的 Visual Studio 集成开发环境(IDE)。
创建新项目:打开 Visual Studio 后,点击“创建新项目”。
选择项目模板:在“创建新项目”窗口中,您将看到一列项目模板。 选择 Visual C# 控制台应用程序。

在 Visual Studio 中创建一个新的 C# 控制台应用程序项目
配置项目详情:选择模板后,系统会提示您配置项目详情。

配置您的新项目
配置附加设置:选择具有长期支持的.NET Framework。 IronPDF 支持最新版本的 .NET Framework。
您可以参考以下步骤在项目中安装 IronPDF:
打开 Visual Studio 和您的项目。
进入 "工具 "菜单,选择 "NuGet 包管理器",然后点击 "管理解决方案的 NuGet 包"。

导航到 NuGet 包管理器
在 "浏览 "选项卡的搜索框中搜索 "IronPDF"。

在 NuGet 包管理器 UI 中搜索 IronPDF
IronPdf包并为您的项目选择它,然后点击“安装”按钮。IronPdf命名空间代码首先通过导入IronPdf和System.Drawing.Printing命名空间开始,这允许使用IronPDF库中的类和方法,以及来自System命名空间的绘图和打印选项。
using IronPdf;
using System.Drawing.Printing;using IronPdf;
using System.Drawing.Printing;Imports IronPdf
Imports System.Drawing.PrintingChromePdfRenderer负责渲染网页。 PdfDocument 类代表 PDF 文档,并提供方法执行各种操作,包括打印。 以下代码通过IronPDF网站URL(https://ironpdf.com/)生成PDF文件:
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/");ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/");Dim renderer As New ChromePdfRenderer()
Dim pdfDocument As PdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/")在上述代码示例中,第一行初始化了IronPDF库中的ChromePdfRenderer实例,该实例负责将网页转换为PDF格式。 第二行使用此实例通过渲染来自指定URL的内容来创建一个PdfDocument,从而启用进一步的PDF相关操作。
pdfDocument.Print(300, "Microsoft Print to PDF", "files/printedfile1.pdf");pdfDocument.Print(300, "Microsoft Print to PDF", "files/printedfile1.pdf");pdfDocument.Print(300, "Microsoft Print to PDF", "files/printedfile1.pdf")上述代码行使用指定的打印分辨率(DPI)启动PdfDocument的打印过程,将其发送到“Microsoft Print to PDF”打印机,该打印机通常是默认打印机(如果未安装其他打印机),并将打印输出保存为名为“printedfile1.pdf”的PDF文件在文件目录中。 您可以根据需要更改打印机名称和文件位置。
PDF 文件打印后像素完美:

输出名为 "printedfile1.pdf" 的 PDF 文件的图像
要更好地控制以编程方式打印 PDF 文件,请查看以下带有高级选项的代码片段:
using (var printDocument = pdfDocument.GetPrintDocument())
{
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
printDocument.PrinterSettings.PrintFileName = "files/printedfile2.pdf";
printDocument.PrinterSettings.PrintToFile = true;
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
Kind = PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
printDocument.Print();
}using (var printDocument = pdfDocument.GetPrintDocument())
{
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
printDocument.PrinterSettings.PrintFileName = "files/printedfile2.pdf";
printDocument.PrinterSettings.PrintToFile = true;
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
Kind = PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
printDocument.Print();
}Using printDocument = pdfDocument.GetPrintDocument()
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
printDocument.PrinterSettings.PrintFileName = "files/printedfile2.pdf"
printDocument.PrinterSettings.PrintToFile = True
printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
.Kind = PrinterResolutionKind.Custom,
.X = 1200,
.Y = 1200
}
printDocument.Print()
End Using在上述代码片段中,IronPDF 为高级 PDF 打印定制提供了便利。 它从PdfDocument生成一个PrintDocument,允许自定义打印机设置、打印机名称、输出文件名和分辨率。 设置PrintFileName很重要,因为它允许您绕过打印对话框进行打印。 然后,代码使用打印方法触发打印,将打印的 PDF 文档内容发送到指定的打印机,即在这种情况下为“Microsoft Print to PDF”。 最后,将输出保存为 PDF 文件。这样就可以通过量身定制的设置实现无缝、无对话框的 PDF 打印。
您还可以指定页面范围,在没有打印机对话框和 Adobe Acrobat Reader 的情况下打印 PDF 文档,这一切都可以通过将 IronPDF 纳入项目来实现。 有关打印的更多详细信息,请访问代码示例页面。
执行项目后,无需任何用户交互,即可在指定文件夹中生成两个输出的打印 PDF 文件。 您还可以比较所用高级选项的大小差异。

显示两个打印的 PDF 文件 "printedfile1.pdf" 和 "printedfile2.pdf" 的图像
从 C# 应用程序直接打印 PDF 文件可简化文档处理并增强用户体验。 IronPDF 及其多种PDF操作工具,使开发人员能够无缝打印PDF文档。 通过将 IronPdf 集成到您的 C# 项目中,您可以充分利用其功能。
IronPDF 是一个以其强大的 PDF 处理能力而闻名的商业库。 它提供免费试用期,使用户能够测试和体验其功能。 试用期结束后,可以获得license以继续使用。 要开始使用,您可以从官方IronPDF网站下载该产品。