C# 以编程方式打印 PDF(代码示例教程)
在 .NET 中打印 PDF 必须快速且可靠。 手动处理打印任务可能会很繁琐,尤其是在文档量大的情况下。 IronPrint 是一个 .NET 打印库,它通过确保发票、报告和图像的顺利打印而无需不必要的复杂性来消除这种麻烦。 它允许开发人员自动化他们的打印工作流程,而无需处理不可预测的打印机行为或耗时的配置。
企业依赖于自动化打印来处理发票、报告和运输标签。 由于商业原因,一些用户也希望打印特定页面。 手动打印引入了不必要的步骤和错误,减缓了工作流程。 通过集成 IronPrint,开发人员消除了这些低效操作,自动化重复任务,并提高了准确性。 这为需要及时、格式良好的文档的最终用户提供了更流畅的体验。
IronPrint 通过提供一种高效管理 PDF 和图像的方式来简化打印。 开发人员可以通过最小的设置将文档发送到打印机,而不是手动处理复杂的打印机设置。这腾出了时间让他们专注于核心应用功能,同时确保每次文档正确打印。下一步是将 IronPrint 设置到您的 .NET 项目中。 过程很简单,一旦集成,它会确保每次打印的一致性和可靠性。
开始使用 IronPrint

通过 NuGet 包管理器进行安装
安装 IronPrint 简单明了。 将其添加到您的 .NET 项目的最简单方法是通过 NuGet 包管理器:
- 在Visual Studio中打开您的项目。
- 导航到 NuGet 包管理器。
- 搜索 "IronPrint"。
- 点击 安装,让 Visual Studio 处理其余的工作。
对于那些喜欢使用 包管理器控制台 的人,可以使用以下命令:
Install-Package IronPrint

安装后,确保所有依赖项均已正确恢复。
在 .NET 项目中的基本设置和配置
安装后,IronPrint 需要最少的配置即可开始工作。 首先,确保您的应用程序可以访问所需的打印机驱动程序。 然后,通过引用库并设置基本参数来初始化 IronPrint。 一个简单的实现可能如下所示:
// Import the IronPrint namespace to access its functionalities.
using IronPrint;
// Use the Printer object to send a PDF file to the default printer.
Printer.Print("sample-document.pdf");// Import the IronPrint namespace to access its functionalities.
using IronPrint;
// Use the Printer object to send a PDF file to the default printer.
Printer.Print("sample-document.pdf");' Import the IronPrint namespace to access its functionalities.
Imports IronPrint
' Use the Printer object to send a PDF file to the default printer.
Printer.Print("sample-document.pdf")此基本设置使应用程序能够立即将 PDF 文件发送到默认打印机。 开发人员可以通过指定不同的打印机、调整打印设置或根据需要进行其他配置集成来自定义行为。 安装和设置完成后,下一步是探索 IronPrint 的核心功能,以实现高效管理不同的打印场景。
核心打印功能
以编程方式打印 PDF
IronPrint 使打印 PDF 和图像变得简单。 Print 方法高效处理 PDF 文件,直接将其发送到打印机,无需不必要的步骤。 这在您需要快速高效打印 PDF 文件时特别有用。 一个简单的实现看起来像这样:
using IronPrint;
// Print a PDF document using the default settings.
Printer.Print("invoice.pdf");using IronPrint;
// Print a PDF document using the default settings.
Printer.Print("invoice.pdf");Imports IronPrint
' Print a PDF document using the default settings.
Printer.Print("invoice.pdf")打印图像
对于图像文件,IronPrint 支持常见格式,如 PNG、JPEG 和 BMP。Print 方法自动检测和处理图像,确保它们打印时保持清晰度和分辨率。
using IronPrint;
// Print an image file, ensuring clarity and resolution are maintained during the process.
Printer.Print("logo.png");using IronPrint;
// Print an image file, ensuring clarity and resolution are maintained during the process.
Printer.Print("logo.png");Imports IronPrint
' Print an image file, ensuring clarity and resolution are maintained during the process.
Printer.Print("logo.png")单个文档中的多个图像按顺序处理。 IronPrint 确保格式保持不变,防止失真或质量损失。 这使得与 PDF 文件一起打印复杂的报告或图形文档成为可能。
无声打印与打印对话框
IronPrint 使开发人员能够控制文档的打印方式。 无声打印直接将 PDF 文档发送到打印机,而无需用户输入。 这对于自动化批量打印减少中断和加速工作流的应用程序特别有用。 您也可以高效打印多个 PDF 文件。
using IronPrint;
// Silent printing of a PDF without requiring user input.
Printer.Print("report.pdf");using IronPrint;
// Silent printing of a PDF without requiring user input.
Printer.Print("report.pdf");Imports IronPrint
' Silent printing of a PDF without requiring user input.
Printer.Print("report.pdf")或者,如果用户需要在打印前调整设置,IronPrint 可以触发打印对话框。 这允许用户选择打印机、设置页面方向和调整打印质量,然后再完成打印作业。
using IronPrint;
// Display a print dialog for user configuration before printing the document.
Printer.ShowPrintDialog("document.pdf");using IronPrint;
// Display a print dialog for user configuration before printing the document.
Printer.ShowPrintDialog("document.pdf");Imports IronPrint
' Display a print dialog for user configuration before printing the document.
Printer.ShowPrintDialog("document.pdf")无声打印最适合无需用户干预的自动化工作流程,而打印对话框则适用于需要自定义的情况。 两种方法都根据应用程序的需求确保灵活性和效率。 通过利用这些功能,开发人员可以在其 .NET 应用程序中集成自动化和用户友好的打印功能,确保顺利处理打印 PDF 文档。
高级打印设置
纸张配置
通过 IronPrint,开发人员可以根据其应用程序的需求自定义纸张尺寸和方向。 当需要打印非标准纸张格式或特定布局时,这尤其有用。 当前的打印文档对象允许开发人员高效管理这些配置。 要设置纸张尺寸和方向,只需初始化 PrintSettings 类并指定所需属性:
using IronPrint;
// Configure paper settings such as size and orientation.
PrintSettings printSettings = new PrintSettings
{
PaperSize = PaperSize.A4,
PaperOrientation = PaperOrientation.Landscape
};
// Print the document using the specified settings.
Printer.Print("document.pdf", printSettings);using IronPrint;
// Configure paper settings such as size and orientation.
PrintSettings printSettings = new PrintSettings
{
PaperSize = PaperSize.A4,
PaperOrientation = PaperOrientation.Landscape
};
// Print the document using the specified settings.
Printer.Print("document.pdf", printSettings);Imports IronPrint
' Configure paper settings such as size and orientation.
Private printSettings As New PrintSettings With {
.PaperSize = PaperSize.A4,
.PaperOrientation = PaperOrientation.Landscape
}
' Print the document using the specified settings.
Printer.Print("document.pdf", printSettings)打印质量和份数
为了更好地控制打印质量,开发人员可以调整 DPI(每英寸点数)设置,以确保文档以正确的细节级别打印。 较高的 DPI 设置提高清晰度,但可能会延长打印时间。此外,您可以指定要打印的份数:
using IronPrint;
// Configure print settings for quality and number of copies.
PrintSettings printSettings = new PrintSettings
{
Dpi = 300,
NumberOfCopies = 2
};
// Print the document with the specified settings.
Printer.Print("report.pdf", printSettings);using IronPrint;
// Configure print settings for quality and number of copies.
PrintSettings printSettings = new PrintSettings
{
Dpi = 300,
NumberOfCopies = 2
};
// Print the document with the specified settings.
Printer.Print("report.pdf", printSettings);Imports IronPrint
' Configure print settings for quality and number of copies.
Private printSettings As New PrintSettings With {
.Dpi = 300,
.NumberOfCopies = 2
}
' Print the document with the specified settings.
Printer.Print("report.pdf", printSettings)打印机选择
开发人员可以指定要使用的打印机,而不是依赖系统的默认打印机。 在提供多个打印机的办公室环境中,这特别有帮助:
using IronPrint;
// Specify the printer to use for printing the document.
PrintSettings printSettings = new PrintSettings
{
PrinterName = "Your Printer Name"
};
// Print the document using the specified printer.
Printer.Print("invoice.pdf", printSettings);using IronPrint;
// Specify the printer to use for printing the document.
PrintSettings printSettings = new PrintSettings
{
PrinterName = "Your Printer Name"
};
// Print the document using the specified printer.
Printer.Print("invoice.pdf", printSettings);Imports IronPrint
' Specify the printer to use for printing the document.
Private printSettings As New PrintSettings With {.PrinterName = "Your Printer Name"}
' Print the document using the specified printer.
Printer.Print("invoice.pdf", printSettings)边距和灰度打印
自定义边距有助于确保正确的文档对齐,而灰度打印可以在不需要颜色时节省墨水。 边距以毫米为单位定义,以便于精确调整:
using IronPrint;
// Configure print settings for margins and grayscale printing.
PrintSettings printSettings = new PrintSettings
{
PaperMargins = new Margins
{
Top = 10,
Right = 10,
Bottom = 10,
Left = 10
},
Grayscale = true
};
// Print the document using the specified settings.
Printer.Print("draft.pdf", printSettings);using IronPrint;
// Configure print settings for margins and grayscale printing.
PrintSettings printSettings = new PrintSettings
{
PaperMargins = new Margins
{
Top = 10,
Right = 10,
Bottom = 10,
Left = 10
},
Grayscale = true
};
// Print the document using the specified settings.
Printer.Print("draft.pdf", printSettings);Imports IronPrint
' Configure print settings for margins and grayscale printing.
Private printSettings As New PrintSettings With {
.PaperMargins = New Margins With {
.Top = 10,
.Right = 10,
.Bottom = 10,
.Left = 10
},
.Grayscale = True
}
' Print the document using the specified settings.
Printer.Print("draft.pdf", printSettings)通过利用这些高级打印设置,开发人员可以微调打印过程,以满足特定要求,确保一致性和专业品质的输出。
检索打印机信息
IronPrint 允许开发人员检索系统上所有可用打印机的列表。 在需要用户选择特定打印机后再启动打印作业的应用程序中,这非常有用。 GetPrinterNames 方法返回一个已安装打印机名称的数组。
using IronPrint;
// Retrieve and display the names of all available printers.
var printers = Printer.GetPrinterNames();
foreach (var printer in printers)
{
Console.WriteLine(printer); // Outputs: OneNote (Desktop), Microsoft Print to PDF
}using IronPrint;
// Retrieve and display the names of all available printers.
var printers = Printer.GetPrinterNames();
foreach (var printer in printers)
{
Console.WriteLine(printer); // Outputs: OneNote (Desktop), Microsoft Print to PDF
}Imports IronPrint
' Retrieve and display the names of all available printers.
Private printers = Printer.GetPrinterNames()
For Each printer In printers
Console.WriteLine(printer) ' Outputs: OneNote (Desktop), Microsoft Print to PDF
Next printer此实现获取可用的打印机名称并将其打印到控制台。 开发人员可以使用这些数据填充下拉菜单或在应用程序中动态分配打印机首选项。
结论

IronPrint 通过提供一个强大而灵活的解决方案来处理 PDF 和图像,革新了 .NET 应用程序中的程序化打印。 IronPrint 的高级打印设置为用户提供了对纸张尺寸、打印质量、边距和灰度选项的精确控制。 此外,它动态检索打印机信息的能力增强了可用性,使应用程序能够适应不同环境,而无需人工干预。
随着 IronPrint 高效且准确地管理与打印相关的任务,开发人员可以专注于完善核心应用功能并提供无缝的功能。 IronPrint 为开发人员提供 免费试用,让他们在承诺之前探索其全部功能。 它的许可以 $liteLicense 起价,为各类规模的企业提供了一种具成本效益和可扩展的解决方案。
常见问题解答
如何在C#中以编程方式打印PDF?
您可以使用IronPrint在C#中以编程方式打印PDF。通过在NuGet包管理器中集成IronPrint到您的.NET项目中,可以以最小的设置自动化打印过程。
使用.NET打印库打印PDF有什么好处?
使用像IronPrint这样的.NET打印库可以自动化打印过程,减少手动错误并提高效率。它支持静默打印、打印对话选项、先进的定制化,以及无缝集成到现有应用中。
如何为PDF设置静默打印?
使用IronPrint,您可以启用静默打印,将文档直接发送到打印机,无需用户交互。这非常适合在C#应用中进行自动化批量打印。
我可以使用.NET库自定义打印设置,如纸张大小和方向吗?
是的,IronPrint允许广泛自定义打印设置,包括纸张大小、方向、打印质量的DPI等,以适应特定应用要求。
如何在C#应用中检索可用打印机的列表?
您可以使用IronPrint的GetPrinterNames方法来获取您环境中的可用打印机名称数组,这有助于动态选择和配置打印机。
使用.NET打印库可以打印哪些图像格式?
IronPrint支持打印多种图像格式,如PNG、JPEG和BMP。您可以使用Printer.Print方法保证高质量的图像打印。
有没有办法在购买前试用.NET打印库?
是的,IronPrint为开发者提供了免费试用,让您在购买之前探索其功能。完整授权从$749起。
哪些平台由.NET打印库支持?
IronPrint支持包括Windows、macOS、Android和iOS在内的多个平台,适用于多样的开发环境。
我该如何将.NET打印库集成到我现有的应用程序中?
IronPrint可以无缝集成到现有.NET应用中,通过自动化PDF和图像的打印来提高工作流程效率。
使用IronPrint进行C#自动化打印有什么优势?
IronPrint简化了C#中的自动化打印,提供了如静默打印、先进的打印设置定制和易于集成等功能,提高了准确性和用户体验。









