IRONSOFTWAREHOME

C# Print Document Tutorial with IronPrint

Curtis Chau
Curtis Chau
Updated: 2026年4月22日

IronPrint 是一个功能强大的打印库,旨在帮助 .NET C# 开发人员将打印功能集成到他们的应用程序中。 IronPrint 具有广泛的兼容性,涵盖 Windows、macOS、iOS 和 Android 平台,可在各种操作系统上稳定可靠地运行。 无论您是为桌面环境、Apple 的 macOS 生态系统还是 iOS 和 Android 等移动平台创建应用程序,IronPrint 都能简化打印功能的实现,为 .NET C# 环境中的所有打印需求提供多功能且用户友好的解决方案。

快速入门:使用 IronPrint 静默打印文档

只需一行代码即可打印——无需对话框,轻松便捷。 使用IronPrint.Printer.Print(...)可以静默地将PDF或图像直接发送到打印机,并使用默认或自定义设置。

  1. 1Install IronPrint with NuGet Package Manager

    PM > Install-Package IronPrint

  2. 2复制并运行这段代码。

    IronPrint.Printer.Print("path/to/your/document.pdf");
    C#
  3. 3部署到您的生产环境中进行测试

    通过免费试用立即在您的项目中开始使用IronPrint
    arrow pointer

目录

打印文档 -静默打印 -带对话框的打印 -应用打印设置 获取打印机信息 获取打印机名称

打印文档

静默打印

无需显示打印对话框即可无缝打印文档。 然后可以直接在代码中设置打印参数。

// Programmatically print a document without showing the print dialog.
// Define your print job and settings here as needed.

using System;
using IronPrint;

public class SilentPrint
{
    public static void Main()
    {
        // Create a print document instance
        var document = new PrintDocument("sample-document.pdf");

        // Initialize a silent print job
        var printJob = new PrintJob(document);

        // Apply specific settings as necessary
        // For example: set printer name, copies, etc.
        
        // Execute the print job
        printJob.PrintSilently();
    }
}

带对话框的打印

在显示的打印设置对话框中启动打印过程。 这样一来,用户可以交互式地自定义打印选项。

// Start a print job with user interaction through the print dialog.

using System;
using IronPrint;

public class DialogPrint
{
    public static void Main()
    {
        // Create a print document instance
        var document = new PrintDocument("sample-document.pdf");

        // Initialize a print job with dialog
        var printJob = new PrintJob(document);

        // Execute the print job with display of print options dialog
        printJob.PrintWithDialog();
    }
}

应用打印设置

通过编程方式调整打印设置以满足特定要求。 本节提供了通过代码微调打印配置的功能。

// Example code to apply custom print settings programmatically.

using System;
using IronPrint;

public class PrintSettingsExample
{
    public static void Main()
    {
        // Create a print document instance
        var document = new PrintDocument("sample-document.pdf");

        // Create a print job
        var printJob = new PrintJob(document);

        // Set custom print settings like duplex, color mode, etc.
        var settings = new PrintSettings
        {
            ColorMode = ColorMode.Color,
            DuplexMode = DuplexMode.OneSided,
            Copies = 2
        };

        // Apply settings to print job
        printJob.ApplySettings(settings);

        // Print the document
        printJob.PrintSilently();
    }
}

获取打印机信息

获取打印机名称

查看所有可用打印机的列表。 检索系统中已安装打印机的名称,以便用于信息参考或在应用程序中进行动态打印机选择。

// Retrieve and display a list of printer names available on the system.

using System;
using IronPrint;

public class PrinterInfo
{
    public static void Main()
    {
        // Get an enumerable list of printer names
        var printerNames = PrinterSettings.GetAvailablePrinters();

        // Print each printer name to the console
        Console.WriteLine("Available Printers:");
        foreach (var name in printerNames)
        {
            Console.WriteLine(name);
        }
    }
}

常见问题解答

我如何在 .NET C# 中无声打印文档?

您可以使用 PrintJob 实例的 PrintSilently() 方法来执行打印作业而无需用户交互。这允许程序化地打印文档而不显示打印对话框。

在 .NET C# 中使用打印对话框打印文档的过程是怎样的?

您可以通过使用 PrintJob 实例上的 PrintWithDialog() 方法来启动带有用户交互的打印作业。这将显示打印设置对话框,允许用户在打印之前自定义选项。

在 .NET C# 中可以以编程方式应用自定义打印设置吗?

是的,您可以通过创建 PrintSettings 对象并配置诸如颜色模式、双工模式和副本数量等属性,以编程方式应用自定义打印设置。这些设置然后可以应用于 PrintJob 实例。

我如何在 .NET C# 应用程序中检索可用的打印机名称?

您可以使用 PrinterSettings.GetAvailablePrinters() 方法检索可用的打印机名称。这为选择或信息目的提供了系统上安装的打印机名称的可枚举列表。

我可以使用 .NET C# 库打印不同的文档格式吗?

是的,该库支持打印各种文档格式,包括 PDF、PNG、HTML、TIFF、GIF、JPEG、IMAGE 和 BITMAP,从而提供多种文档打印选项。

using .NET C# 库打印文档支持哪些平台?

该库支持多个平台,如 Windows、macOS、iOS 和 Android,确保在这些操作系统中打印能力的一致性和可靠性。

在 .NET C# 中,无声打印与基于对话框的打印有何不同?

无声打印允许文档以编程方式打印而无需用户交互,使用 PrintSilently() 方法。基于对话框的打印包括显示打印对话框以便用户自定义,这是通过 PrintWithDialog() 方法实现的。

Can IronPrint perform grayscale printing and adjust the number of copies programmatically?

Yes, you can set custom print settings such as enabling grayscale and specifying the number of copies directly within your C# code using IronPrint's `PrintSettings` class.

Curtis Chau
技术作家

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

...
阅读更多

准备开始了吗?

Nuget Downloads 46,090版本:2026.9刚刚发布

免费获取

30天试用密钥 即刻获取。

bullet_checked无需信用卡或创建账户
bullet_test在生产环境中测试
且无水印
bullet_calendar30天完全
功能性产品
bullet_support试用期间提供
24/5技术支持
立即获取您的免费30 天试用密钥
无需信用卡或创建账户
C# 用于 PDF 的 NuGet 库
通过 NuGet 安装

版本: 2026.9

PM > Install-Package IronPrint
nuget.org/packages/IronPrint/
  1. 在解决方案资源管理器中,右键点击引用,管理 NuGet 包
  2. 选择“浏览”并搜索“IronPrint”
  3. 选择包并安装
C# PDF DLL
下载 DLL

版本: 2026.9

  1. 下载并解压 IronPrint 到您的解决方案目录中的 ~/Libs 之类的位置
  2. 在 Visual Studio 解决方案资源管理器中,右键单击引用。选择“浏览”,“IronPrint.dll”

$999 起售

Key in blue circle

立即获取免费的 30 天试用版密钥

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
预约您的免费现场演示
Booking Badge

深受全球数百万工程师信赖

Iron Software 的客户徽标
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥
无需信用卡或创建账户