使用 IronPrint 的 C# 打印文档教程
IronPrint 是一个功能强大的打印库,旨在帮助 .NET C# 开发人员将打印功能集成到他们的应用程序中。 IronPrint 具有广泛的兼容性,涵盖 Windows、macOS、iOS 和 Android 平台,可在各种操作系统上稳定可靠地运行。 无论您是为桌面环境、Apple 的 macOS 生态系统还是 iOS 和 Android 等移动平台创建应用程序,IronPrint 都能简化打印功能的实现,为 .NET C# 环境中的所有打印需求提供多功能且用户友好的解决方案。
快速入门:使用 IronPrint 静默打印文档只需一行代码即可打印——无需对话框,无烦恼。 使用IronPrint.Printer.Print(...)可以静默地将PDF或图像直接发送到打印机,使用默认或自定义设置。
-
1使用 IronPrint 安装 NuGet Package Manager
-
2复制并运行这段代码。
IronPrint.Printer.Print("path/to/your/document.pdf");IronPrint.Printer.Print("path/to/your/document.pdf")IronPrint.Printer.Print("path/to/your/document.pdf") -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronPrint
目录
打印文档
静默打印
在不显示打印对话框的情况下打印文档。 然后可以直接在代码中设置打印参数。
// 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()
{
// Apply specific settings as necessary
// For example: set printer name, copies, etc.
var settings = new PrintSettings();
// Print the document without displaying the print dialog
Printer.Print("sample-document.pdf", settings);
}
}// 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()
{
// Apply specific settings as necessary
// For example: set printer name, copies, etc.
var settings = new PrintSettings();
// Print the document without displaying the print dialog
Printer.Print("sample-document.pdf", settings);
}
}Imports System
Imports IronPrint
Public Class SilentPrint
Public Shared Sub Main()
' Apply specific settings as necessary
' For example: set printer name, copies, etc.
Dim settings = New PrintSettings()
' Print the document without displaying the print dialog
Printer.Print("sample-document.pdf", settings)
End Sub
End ClassImports System
Imports IronPrint
Public Class SilentPrint
Public Shared Sub Main()
' Apply specific settings as necessary
' For example: set printer name, copies, etc.
Dim settings = New PrintSettings()
' Print the document without displaying the print dialog
Printer.Print("sample-document.pdf", settings)
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()
{
var settings = new PrintSettings();
// Show the print dialog so the user can customize print options
Printer.ShowPrintDialog("sample-document.pdf", settings);
}
}// Start a print job with user interaction through the print dialog.
using System;
using IronPrint;
public class DialogPrint
{
public static void Main()
{
var settings = new PrintSettings();
// Show the print dialog so the user can customize print options
Printer.ShowPrintDialog("sample-document.pdf", settings);
}
}Imports System
Imports IronPrint
Public Class DialogPrint
Public Shared Sub Main()
Dim settings = New PrintSettings()
' Show the print dialog so the user can customize print options
Printer.ShowPrintDialog("sample-document.pdf", settings)
End Sub
End ClassImports System
Imports IronPrint
Public Class DialogPrint
Public Shared Sub Main()
Dim settings = New PrintSettings()
' Show the print dialog so the user can customize print options
Printer.ShowPrintDialog("sample-document.pdf", settings)
End Sub
End Class应用打印设置
通过编程方式调整打印设置以满足特定要求。 本节提供了通过代码微调打印配置的功能。
// Example code to apply custom print settings programmatically.
using System;
using IronPrint;
public class PrintSettingsExample
{
public static void Main()
{
// Set custom print settings such as grayscale and number of copies
var settings = new PrintSettings
{
Grayscale = false,
NumberOfCopies = 2
};
// Print the document using the configured settings
Printer.Print("sample-document.pdf", settings);
}
}// Example code to apply custom print settings programmatically.
using System;
using IronPrint;
public class PrintSettingsExample
{
public static void Main()
{
// Set custom print settings such as grayscale and number of copies
var settings = new PrintSettings
{
Grayscale = false,
NumberOfCopies = 2
};
// Print the document using the configured settings
Printer.Print("sample-document.pdf", settings);
}
}Imports System
Imports IronPrint
Public Class PrintSettingsExample
Public Shared Sub Main()
' Set custom print settings such as grayscale and number of copies
Dim settings = New PrintSettings With {
.Grayscale = False,
.NumberOfCopies = 2
}
' Print the document using the configured settings
Printer.Print("sample-document.pdf", settings)
End Sub
End ClassImports System
Imports IronPrint
Public Class PrintSettingsExample
Public Shared Sub Main()
' Set custom print settings such as grayscale and number of copies
Dim settings = New PrintSettings With {
.Grayscale = False,
.NumberOfCopies = 2
}
' Print the document using the configured settings
Printer.Print("sample-document.pdf", settings)
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 = Printer.GetPrinterNames();
// 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 = Printer.GetPrinterNames();
// Print each printer name to the console
Console.WriteLine("Available Printers:");
foreach (var name in printerNames)
{
Console.WriteLine(name);
}
}
}Imports System
Imports IronPrint
Public Class PrinterInfo
Public Shared Sub Main()
' Get an enumerable list of printer names
Dim printerNames = Printer.GetPrinterNames()
' Print each printer name to the console
Console.WriteLine("Available Printers:")
For Each name In printerNames
Console.WriteLine(name)
Next
End Sub
End ClassImports System
Imports IronPrint
Public Class PrinterInfo
Public Shared Sub Main()
' Get an enumerable list of printer names
Dim printerNames = Printer.GetPrinterNames()
' Print each printer name to the console
Console.WriteLine("Available Printers:")
For Each name In printerNames
Console.WriteLine(name)
Next
End Sub
End Class常见问题
IronPrint支持哪些平台进行文档打印?
IronPrint支持在包括Windows、macOS、iOS和Android在内的广泛平台上进行打印,确保在不同操作系统中轻松集成。
如何使用IronPrint在不显示打印对话框的情况下打印文档?
使用IronPrint,您可以通过一行代码静默打印文档:IronPrint.Printer.Print("path/to/your/document.pdf");。这允许您将PDF直接发送到打印机,无需对话框中断。
我可以使用IronPrint以编程方式自定义打印设置吗?
是的,您可以通过在C#应用程序代码中定义打印机名称、份数等设置,以编程方式使用IronPrint自定义打印设置。
可以使用IronPrint交互式设置打印选项吗?
IronPrint允许用户利用打印对话框以交互式选项开始打印作业,这有助于根据用户偏好自定义各种打印设置。
如何使用IronPrint检索可用打印机的名称?
您可以通过实现Printer.GetPrinterNames()方法来使用IronPrint检索可用打印机列表,该方法提供系统上安装的打印机名称的可枚举列表。
IronPrint可以处理哪些文档类型?
IronPrint可以处理多种文档类型,包括PDF、PNG、HTML、TIFF、GIF、JPEG等,成为满足各种打印需求的多功能工具。
IronPrint是否兼容桌面和移动应用程序?
是的,IronPrint兼容桌面环境(Windows和macOS)和移动平台(iOS和Android),允许在不同设备上实现灵活的打印解决方案。
IronPrint能否以编程方式执行灰度打印和调整份数?
是的,您可以使用IronPrint的PrintSettings类在您的C#代码中直接设置自定义打印设置,如启用灰度和指定份数。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。