
如何在 C# 中不使用对话框打印 PDF 文件
IronPrint 是 Iron Software 的全新 .NET 打印库,提供跨多种平台的兼容性,包括 Windows、macOS、Android 和 iOS。现在就开始使用 IronPrint!
这篇文章探讨了直接打印PDF的意义,并演示了强大的C#库IronPDF如何简化这一过程。
IronPDF - .NET Framework PDF库
IronPDF 是一个功能强大的 C# 库,旨在促进 PDF 文档的创建、操作和交互。 使用IronPDF,开发人员可以轻松地从HTML内容、图像和其他来源生成PDF,使其成为处理简单和复杂PDF相关任务的多功能工具。 其功能不仅限于生成PDF文档; IronPDF还支持合并、拆分和各种方式处理PDF。 此外,其直观的API使其对于初学者和有经验的开发人员都很容易使用。 一些重要的功能包括:
- 从HTML4/5、CSS、JavaScript和图像创建PDF文档。
- 从URL生成PDF文档。
- 使用自定义网络登录凭证加载URL。
- 加密和解密PDF。
- 合并现有PDF文件。
- 创建和编辑PDF表单。
直接打印PDF的重要性
在处理PDF时,打印是一个基本需求。 从C#应用程序直接打印PDF而不出现打印对话框提供了多种优势。 它通过消除不必要的交互来增强用户体验,自动化打印任务,并可无缝集成到更大的工作流程中。 IronPDF简化了这一过程,使开发人员能够控制打印过程,同时确保打印输出的完整性。
前提条件
在继续PDF打印过程之前,请确保具备以下先决条件:
- **Visual Studio:**它是一个集成开发环境 (IDE),您可以在其中创建、编辑和编译您的 C# 项目。 要在计算机上下载并安装它,请访问官方Visual Studio 网站。
- IronPDF: IronPDF库,可以使用NuGet包管理器轻松集成到您的项目中。
创建Visual Studio项目
创建Visual Studio控制台项目是一个简单的过程。 按照以下步骤使用Visual Studio创建一个新的控制台应用程序:
-
**打开Visual Studio:**启动你的Visual Studio IDE。
-
**创建新项目:**Visual Studio打开后,点击"Create a new project"。
-
**选择项目模板:**在"Create a new project"窗口中,你将看到一个项目模板列表。 选择Visual C#控制台应用程序。
在Visual Studio中创建一个新的C#控制台应用程序项目 -
**配置项目详细信息:**选择模板后,你将被提示配置项目详细信息。
配置新项目
5.**配置其他设置:**选择具有长期支持的.NET Framework 。 IronPDF支持最新版本的.NET Framework。 6. 创建项目:配置项目详细信息后,点击Create按钮。 Visual Studio将创建项目并在IDE中打开它。
通过NuGet安装IronPDF
您可以参考以下步骤在您的项目中安装IronPDF:
-
打开 Visual Studio 和您的项目。
-
转到"工具"菜单并选择"NuGet包管理器",然后点击"管理解决方案的NuGet包"。
导航到 NuGet 包管理器 -
在"浏览"选项卡中,在搜索框中搜索"IronPDF"。
在NuGet包管理器UI中搜索IronPDF -
点击
IronPdf包并为您的项目选择它,然后点击"安装"按钮。
IronPDF用于无对话框打印 - 步骤详解
导入IronPdf命名空间
代码首先导入System.Drawing.Printing命名空间,从而可以使用IronPDF库中的类和方法,以及System命名空间中的绘图和打印选项。
using IronPdf;
using System.Drawing.Printing;Imports IronPdf
Imports System.Drawing.Printing使用ChromePdfRenderer创建PDF文档
ChromePdfRenderer负责渲染网页。 PdfDocument类表示PDF文档,并提供对其进行各种操作的方法,包括打印。 以下代码从IronPDF网站URL(https://ironpdf.com/)生成PDF文件:
// Initialize the ChromePdfRenderer which is used to convert URLs to PDFs
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Create a PdfDocument by rendering a specified URL as a PDF
PdfDocument pdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/");' Initialize the ChromePdfRenderer which is used to convert URLs to PDFs
Dim renderer As New ChromePdfRenderer()
' Create a PdfDocument by rendering a specified URL as a PDF
Dim pdfDocument As PdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/")在上面的代码示例中,第一行从IronPDF库中初始化一个ChromePdfRenderer实例,负责将网页转换为PDF格式。 第二行使用这个实例通过从指定的URL渲染内容创建一个PdfDocument,从而启用进一步的PDF相关操作。
无对话框打印PDF文件
// Initiate the printing process for the PdfDocument
pdfDocument.Print(300, "Microsoft Print to PDF", "files/printedfile1.pdf");' Initiate the printing process for the PdfDocument
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())
{
// Set the printer name to "Microsoft Print to PDF"
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
// Specify the file name for the printed document
printDocument.PrinterSettings.PrintFileName = "files/printedfile2.pdf";
// Enable printing to file
printDocument.PrinterSettings.PrintToFile = true;
// Set custom printer resolution
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
Kind = PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
// Initiate the print process
printDocument.Print();
}Using printDocument = pdfDocument.GetPrintDocument()
' Set the printer name to "Microsoft Print to PDF"
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
' Specify the file name for the printed document
printDocument.PrinterSettings.PrintFileName = "files/printedfile2.pdf"
' Enable printing to file
printDocument.PrinterSettings.PrintToFile = True
' Set custom printer resolution
printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
.Kind = PrinterResolutionKind.Custom,
.X = 1200,
.Y = 1200
}
' Initiate the print process
printDocument.Print()
End Using在上述代码片段中,IronPDF方便了高级PDF打印自定义。 它从PrintDocument,允许自定义打印机设置、打印机名称、输出文件名和分辨率。 设置PrintFileName很重要,因为它允许您绕过打印对话框进行打印。 然后通过触发打印方法,代码将打印PDF文档内容定向到指定的打印机,即此例中的"Microsoft Print to PDF"。 最后,将输出保存为PDF文件。这使得具有定制设置的无对话框PDF打印无缝进行。
您还可以指定页码范围以在没有打印机对话框和Adobe Acrobat Reader的情况下打印PDF文档,只需将IronPDF整合到您的项目中。 有关打印的更详细信息,请访问代码示例页。
输出
项目执行完成后,两个输出打印的PDF文件将在指定文件夹中生成,不需要任何用户交互。 您还可以比较使用高级选项后的尺寸差异。
显示名为"printedfile1.pdf"和"printedfile2.pdf"的两个打印的PDF文件的图像
结论
直接从C#应用程序打印PDF文件简化了文档处理,并增强了用户体验。 IronPDF,凭借其一系列 PDF 操作工具,使开发人员能够无缝地打印 PDF 文档。 通过将IronPDF集成到您的C#项目中,您可以充分利用其功能。
IronPDF 是一个以其强大 PDF 处理能力而闻名的商业库。 它提供了免费试用期,允许用户测试和体验其功能。 试用期结束后,可以购买许可证继续使用。 要开始,请从官方网站 IronPDF 网站下载产品。

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。
相关文章


