使用 IronPrint 进行 C# 文档打印教程
IronPrint 是一个功能强大的打印库,旨在帮助 .NET C# 开发人员将打印功能集成到他们的应用程序中。 IronPrint 具有广泛的兼容性,涵盖 Windows、macOS、iOS 和 Android 平台,可在各种操作系统上稳定可靠地运行。 无论您是为桌面环境、Apple 的 macOS 生态系统还是 iOS 和 Android 等移动平台创建应用程序,IronPrint 都能简化打印功能的实现,为 .NET C# 环境中的所有打印需求提供多功能且用户友好的解决方案。
快速入门:使用 IronPrint 静默打印文档
只需一行代码即可打印——无需对话框,轻松便捷。 使用IronPrint.Printer.Print(...)可以静默地将 PDF 或图像直接发送到打印机,使用默认或自定义设置。
立即开始使用 NuGet 创建 PDF 文件:
使用 NuGet 包管理器安装 IronPrint
复制并运行这段代码。
IronPrint.Printer.Print("path/to/your/document.pdf");部署到您的生产环境中进行测试
目录
打印文档
静默打印
无需显示打印对话框即可无缝打印文档。 然后可以直接在代码中设置打印参数。
// 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();
}
}// 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();
}
}' Programmatically print a document without showing the print dialog.
' Define your print job and settings here as needed.
Imports System
Imports IronPrint
Public Class SilentPrint
Public Shared Sub Main()
' Create a print document instance
Dim document = New PrintDocument("sample-document.pdf")
' Initialize a silent print job
Dim printJob As New PrintJob(document)
' Apply specific settings as necessary
' For example: set printer name, copies, etc.
' Execute the print job
printJob.PrintSilently()
End Sub
End Class带对话框的打印
在显示的打印设置对话框中启动打印过程。 这样一来,用户可以交互式地自定义打印选项。
// 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();
}
}// 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();
}
}' Start a print job with user interaction through the print dialog.
Imports System
Imports IronPrint
Public Class DialogPrint
Public Shared Sub Main()
' Create a print document instance
Dim document = New PrintDocument("sample-document.pdf")
' Initialize a print job with dialog
Dim printJob As New PrintJob(document)
' Execute the print job with display of print options dialog
printJob.PrintWithDialog()
End Sub
End Class应用打印设置
通过编程方式调整打印设置以满足特定要求。 本节提供了通过代码微调打印配置的功能。
// 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();
}
}// 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();
}
}' Example code to apply custom print settings programmatically.
Imports System
Imports IronPrint
Public Class PrintSettingsExample
Public Shared Sub Main()
' Create a print document instance
Dim document = New PrintDocument("sample-document.pdf")
' Create a print job
Dim printJob As New PrintJob(document)
' Set custom print settings like duplex, color mode, etc.
Dim settings = New PrintSettings With {
.ColorMode = ColorMode.Color,
.DuplexMode = DuplexMode.OneSided,
.Copies = 2
}
' Apply settings to print job
printJob.ApplySettings(settings)
' Print the document
printJob.PrintSilently()
End Sub
End Class获取打印机信息
获取打印机名称
查看所有可用打印机的列表。 检索系统中已安装打印机的名称,以便用于信息参考或在应用程序中进行动态打印机选择。
// 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);
}
}
}// 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);
}
}
}' Retrieve and display a list of printer names available on the system.
Imports System
Imports IronPrint
Public Class PrinterInfo
Public Shared Sub Main()
' Get an enumerable list of printer names
Dim printerNames = PrinterSettings.GetAvailablePrinters()
' Print each printer name to the console
Console.WriteLine("Available Printers:")
For Each name In printerNames
Console.WriteLine(name)
Next name
End Sub
End Class常见问题解答
我如何在 .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,从而提供多种文档打印选项。
使用 .NET C# 库打印文档支持哪些平台?
该库支持多个平台,如 Windows、macOS、iOS 和 Android,确保在这些操作系统中打印能力的一致性和可靠性。
在 .NET C# 中,无声打印与基于对话框的打印有何不同?
无声打印允许文档以编程方式打印而无需用户交互,使用 PrintSilently() 方法。基于对话框的打印包括显示打印对话框以便用户自定义,这是通过 PrintWithDialog() 方法实现的。






