跳至页脚内容
使用 IRONPRINT

如何在 C# 中不使用对话框打印 PDF 文件

这篇文章探讨了直接打印PDF的意义,并演示了强大的C#库IronPDF如何简化这一过程。

IronPDF - .NET Framework PDF库

IronPDF是一个强大的C#库,旨在促进PDF文档的创建、操作和交互。 使用IronPDF,开发人员可以轻松地从HTML内容、图像和其他来源生成PDF,使其成为处理简单和复杂PDF相关任务的多功能工具。 其功能不仅限于生成PDF文档; IronPDF还支持合并、拆分和各种方式处理PDF。 此外,其直观的API使其对于初学者和有经验的开发人员都很容易使用。 一些重要的功能包括:

直接打印PDF的重要性

在处理PDF时,打印是一个基本需求。 从C#应用程序直接打印PDF而不出现打印对话框提供了多种优势。 它通过消除不必要的交互来增强用户体验,自动化打印任务,并可无缝集成到更大的工作流程中。 IronPDF简化了这一过程,使开发人员能够控制打印过程,同时确保打印输出的完整性。

前提条件

在继续PDF打印过程之前,请确保具备以下先决条件:

  1. Visual Studio:这是一个集成开发环境(IDE),可以在其中创建、编辑和编译C#项目。 要在计算机上下载并安装它,请访问官方Visual Studio 网站
  2. IronPDFIronPDF库,可以通过NuGet包管理器轻松集成到您的项目中。

创建Visual Studio项目

创建Visual Studio控制台项目是一个简单的过程。 按照以下步骤使用Visual Studio创建一个新的控制台应用程序:

  1. 打开 Visual Studio:启动您的Visual Studio IDE。
  2. 创建新项目:打开Visual Studio后,点击"创建新项目"。
  3. 选择项目模板:在"创建新项目"窗口中,您将看到项目模板列表。 选择Visual C#控制台应用程序。

在C#中如何在没有对话框的情况下打印PDF文件,图1:在Visual Studio中创建一个新的C#控制台应用程序项目 在Visual Studio中创建一个新的C#控制台应用程序项目

  1. 配置项目细节:选择模板后,您将被提示配置项目详情。

在C#中如何在没有对话框的情况下打印PDF文件,图2:配置您的新项目 配置新项目

  1. 配置额外的设置:选择获得长期支持的.NET Framework。 IronPDF支持最新版本的.NET Framework。
  2. 创建项目:配置完项目详情后,点击创建按钮。 Visual Studio将创建项目并在IDE中打开它。

通过NuGet安装IronPDF

您可以参考以下步骤在您的项目中安装IronPDF:

  1. 打开 Visual Studio 和您的项目。
  2. 转到"工具"菜单并选择"NuGet包管理器",然后点击"管理解决方案的NuGet包"。

在C#中如何在没有对话框的情况下打印PDF文件,图3:导航到NuGet包管理器 导航到 NuGet 包管理器

  1. 在"浏览"选项卡中,在搜索框中搜索"IronPDF"。

在C#中如何在没有对话框的情况下打印PDF文件,图4:在NuGet包管理器UI中搜索IronPDF 在NuGet包管理器UI中搜索IronPDF

  1. 点击IronPdf包并为您的项目选择它,然后点击"安装"按钮。

IronPDF用于无对话框打印 - 步骤详解

导入IronPdf命名空间

代码首先通过导入IronPdfSystem.Drawing.Printing命名空间启动,允许使用IronPDF库中的类和方法,以及System命名空间中的绘图和打印选项。

using IronPdf;
using System.Drawing.Printing;
using IronPdf;
using System.Drawing.Printing;
Imports IronPdf
Imports System.Drawing.Printing
$vbLabelText   $csharpLabel

使用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
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/")
$vbLabelText   $csharpLabel

在上面的代码示例中,第一个初始化了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");
' Initiate the printing process for the PdfDocument
pdfDocument.Print(300, "Microsoft Print to PDF", "files/printedfile1.pdf")
$vbLabelText   $csharpLabel

上面的代码启用使用指定的打印分辨率(DPI)为PdfDocument启动打印过程,将其发送到"Microsoft Print to PDF"打印机上,如果没有安装打印机,通常这是默认打印机,并将打印输出另存为文件名为"printedfile1.pdf"的PDF文件在文件目录中。 您可以根据需要更改打印机名称和文件位置。

PDF文件是像素完美打印的:

在C#中如何在没有对话框的情况下打印PDF文件,图5:名为printedfile1.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 (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
$vbLabelText   $csharpLabel

在上述代码片段中,IronPDF方便了高级PDF打印自定义。 它从PdfDocument生成PrintDocument,允许自定义打印机设置、打印机名称、输出文件名和分辨率。 设置PrintFileName很重要,因为它允许您在打印时跳过打印对话框。 然后通过触发打印方法,代码将打印PDF文档内容定向到指定的打印机,即此例中的"Microsoft Print to PDF"。 最后,将输出保存为PDF文件。这使得具有定制设置的无对话框PDF打印无缝进行。

您还可以指定页码范围以在没有打印机对话框和Adobe Acrobat Reader的情况下打印PDF文档,只需将IronPDF整合到您的项目中。 有关打印的更详细信息,请访问代码示例页

输出

项目执行完成后,两个输出打印的PDF文件将在指定文件夹中生成,不需要任何用户交互。 您还可以比较使用高级选项后的尺寸差异。

在C#中如何在没有对话框的情况下打印PDF文件,图6:显示名为printedfile1.pdf和printedfile2.pdf的两个打印的PDF文件的图像 显示名为"printedfile1.pdf"和"printedfile2.pdf"的两个打印的PDF文件的图像

结论

直接从C#应用程序打印PDF文件简化了文档处理,并增强了用户体验。 IronPDF凭借其多样的PDF操作工具,使开发人员能够无缝打印PDF文档。 通过将IronPDF集成到您的C#项目中,您可以充分利用其功能。

IronPDF是一个以其强大的PDF处理能力而闻名的商业库。 它提供了免费试用期,允许用户测试和体验其功能。 试用期结束后,可以购买许可证继续使用。 要开始,请从官方IronPDF网站下载产品。

常见问题解答

如何直接从 C# 应用程序打印 PDF 文件而不使用对话框?

您可以使用 IronPDF 的 API 直接从 C# 应用程序打印 PDF 文件而不使用对话框。通过程序管理打印机设置,您可以将 PDF 发送到像“Microsoft Print to PDF”这样的打印机,并自动化打印过程。

使用 IronPDF 打印 PDF 的优势是什么?

IronPDF 通过自动化 PDF 打印任务而无需用户交互,无缝集成到工作流程中,并提供高级选项,例如自定义打印机设置和输出文件规范,从而提高用户体验。

如何设置 Visual Studio 项目以使用 IronPDF 打印 PDF?

要设置 Visual Studio 项目,请打开 Visual Studio,创建一个新的控制台应用程序,选择合适的项目模板,并通过 NuGet 包管理器安装 IronPDF 以启用 PDF 打印功能。

IronPDF 能否打印 PDF 的特定页面而无需用户干预?

是的,IronPDF 允许您指定打印的页面范围,而无需用户干预或打印机对话框,非常适合自动化任务。

使用 IronPDF 从网页 URL 生成 PDF 的过程是什么?

IronPDF 提供从网页 URL 生成 PDF 的方法,然后可以直接打印到指定的打印机。这可以自动转换和打印网页内容。

如何使用 IronPDF 自定义打印设置?

IronPDF 提供了打印设置的高级自定义选项,包括定义打印机分辨率和指定输出文件名,使您能够更好地控制打印过程。

IronPrint,Iron Software 的 .NET 打印库,兼容哪些平台?

IronPrint 支持多种平台,包括 Windows、macOS、Android 和 iOS,可在不同环境中灵活使用。

如何在我的 C# 项目中安装 IronPDF?

您可以在 Visual Studio 中使用 NuGet 包管理器在 C# 项目中安装 IronPDF。只需在“浏览”选项卡中搜索 IronPDF 并将其添加到您的项目中。

IronPDF 提供哪些用于在 C# 中处理 PDF 的功能?

IronPDF 提供用于 PDF 创建、操作和交互的强大功能,包括合并、拆分、加密PDF,并直接从 C# 应用程序打印。

IronPDF有哪些许可选项?

IronPDF 为开发人员提供免费试用以评估其功能,并提供各种许可选项以便在生产环境中继续使用。

Curtis Chau
技术作家

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

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。