using IronPrint;// Configure print settings with a target printerPrintSettings settings = new PrintSettings();settings.PrinterName = "Microsoft Print to PDF";// Print to the specified printerPrinter.Print("invoice.pdf", settings);
using IronPrint;
// Configure print settings with a target printer
PrintSettings settings = new PrintSettings();
settings.PrinterName = "Microsoft Print to PDF";
// Print to the specified printer
Printer.Print("invoice.pdf", settings);
ImportsIronPrint' Configure print settings with a target printerDim settings As New PrintSettings()settings.PrinterName = "Microsoft Print to PDF"' Print to the specified printerPrinter.Print("invoice.pdf", settings)
Imports IronPrint
' Configure print settings with a target printer
Dim settings As New PrintSettings()
settings.PrinterName = "Microsoft Print to PDF"
' Print to the specified printer
Printer.Print("invoice.pdf", settings)
using IronPrint;// List every installed printerList<string> printers = Printer.GetPrinterNames();// Print each printer nameforeach (string name in printers){Console.WriteLine(name);}// Print to the first available printerif (printers.Count > 0){Printer.Print("report.pdf", new PrintSettings {PrinterName = printers[0] });}
using IronPrint;
// List every installed printer
List<string> printers = Printer.GetPrinterNames();
// Print each printer name
foreach (string name in printers)
{
Console.WriteLine(name);
}
// Print to the first available printer
if (printers.Count > 0)
{
Printer.Print("report.pdf", new PrintSettings
{
PrinterName = printers[0]
});
}
ImportsIronPrint' List every installed printerDim printers AsList(OfString) = Printer.GetPrinterNames()' Print each printer nameFor Each name AsStringIn printersConsole.WriteLine(name)Next' Print to the first available printerIf printers.Count > 0 ThenPrinter.Print("report.pdf", New PrintSettingsWith { .PrinterName = printers(0) })End If
Imports IronPrint
' List every installed printer
Dim printers As List(Of String) = Printer.GetPrinterNames()
' Print each printer name
For Each name As String In printers
Console.WriteLine(name)
Next
' Print to the first available printer
If printers.Count > 0 Then
Printer.Print("report.pdf", New PrintSettings With {
.PrinterName = printers(0)
})
End If
我们调用GetPrinterNames以检索操作系统知道的每台打印机,包括本地的、网络的和虚拟打印机如"Microsoft Print to PDF"。然后我们遍历列表并通过索引、名称匹配或应用程序需要的任何自定义逻辑选择打印机。
using IronPrint;// Configure full print settings with a named printerPrintSettings settings = new PrintSettings{PrinterName = "Office Color Printer",PaperSize = PaperSize.A4,PaperOrientation = PaperOrientation.Portrait,Dpi = 300,NumberOfCopies = 2,PaperMargins = new Margins(15, 15, 15, 15),Grayscale = false};// Print the quarterly reportPrinter.Print("quarterly-report.pdf", settings);
using IronPrint;
// Configure full print settings with a named printer
PrintSettings settings = new PrintSettings
{
PrinterName = "Office Color Printer",
PaperSize = PaperSize.A4,
PaperOrientation = PaperOrientation.Portrait,
Dpi = 300,
NumberOfCopies = 2,
PaperMargins = new Margins(15, 15, 15, 15),
Grayscale = false
};
// Print the quarterly report
Printer.Print("quarterly-report.pdf", settings);
ImportsIronPrint' Configure full print settings with a named printerDim settings As New PrintSettingsWith { .PrinterName = "Office Color Printer", .PaperSize = PaperSize.A4, .PaperOrientation = PaperOrientation.Portrait, .Dpi = 300, .NumberOfCopies = 2, .PaperMargins = New Margins(15, 15, 15, 15), .Grayscale = False}' Print the quarterly reportPrinter.Print("quarterly-report.pdf", settings)
Imports IronPrint
' Configure full print settings with a named printer
Dim settings As New PrintSettings With {
.PrinterName = "Office Color Printer",
.PaperSize = PaperSize.A4,
.PaperOrientation = PaperOrientation.Portrait,
.Dpi = 300,
.NumberOfCopies = 2,
.PaperMargins = New Margins(15, 15, 15, 15),
.Grayscale = False
}
' Print the quarterly report
Printer.Print("quarterly-report.pdf", settings)
我们采用对象初始化语法以提高可读性。 PrinterName将任务路由到"Office Color Printer",而其余属性控制输出格式。 Dpi在300时产生清晰的文本和图形。 Margins构造函数接受毫米值。 有关所有可用属性的完整列表,请参阅打印设置指南。
using IronPrint;using System.Threading.Tasks;public class PrintService{ public async TaskPrintToFirstAvailableAsync(string filePath) { // Discover available printers asynchronously List<string> printers = awaitPrinter.GetPrinterNamesAsync(); // Exit if no printers are installed if (printers.Count == 0) {Console.WriteLine("No printers found."); return; } // Configure settings for the first available printer PrintSettings settings = new PrintSettings {PrinterName = printers[0],Dpi = 300 }; // Print asynchronously awaitPrinter.PrintAsync(filePath, settings); }}
using IronPrint;
using System.Threading.Tasks;
public class PrintService
{
public async Task PrintToFirstAvailableAsync(string filePath)
{
// Discover available printers asynchronously
List<string> printers = await Printer.GetPrinterNamesAsync();
// Exit if no printers are installed
if (printers.Count == 0)
{
Console.WriteLine("No printers found.");
return;
}
// Configure settings for the first available printer
PrintSettings settings = new PrintSettings
{
PrinterName = printers[0],
Dpi = 300
};
// Print asynchronously
await Printer.PrintAsync(filePath, settings);
}
}
ImportsIronPrintImportsSystem.Threading.TasksPublic Class PrintService PublicAsync Function PrintToFirstAvailableAsync(filePath AsString) AsTask ' Discover available printers asynchronously Dim printers AsList(OfString) = AwaitPrinter.GetPrinterNamesAsync() ' Exit if no printers are installed If printers.Count = 0 ThenConsole.WriteLine("No printers found.") Return End If ' Configure settings for the first available printer Dim settings As New PrintSettingsWith { .PrinterName = printers(0), .Dpi = 300 } ' Print asynchronouslyAwaitPrinter.PrintAsync(filePath, settings) End FunctionEnd Class
Imports IronPrint
Imports System.Threading.Tasks
Public Class PrintService
Public Async Function PrintToFirstAvailableAsync(filePath As String) As Task
' Discover available printers asynchronously
Dim printers As List(Of String) = Await Printer.GetPrinterNamesAsync()
' Exit if no printers are installed
If printers.Count = 0 Then
Console.WriteLine("No printers found.")
Return
End If
' Configure settings for the first available printer
Dim settings As New PrintSettings With {
.PrinterName = printers(0),
.Dpi = 300
}
' Print asynchronously
Await Printer.PrintAsync(filePath, settings)
End Function
End Class
Is it possible to print asynchronously with IronPrint?
Yes, you can use `Printer.GetPrinterNamesAsync` and `Printer.PrintAsync` methods in IronPrint to perform asynchronous printing, which is useful for keeping UI applications responsive.
Does IronPrint support user interaction via print dialogs?
Yes, in IronPrint, if a print dialog is opened using `ShowPrintDialog`, it can override the programmatic selection in the `PrinterName` property, giving end-users final control over printer selection.
What should I do if no printers are found using IronPrint?
If no printers are found with IronPrint's `Printer.GetPrinterNames`, ensure that printers are correctly installed and available on the system. You may also execute fallback logic or notify the user.
How do I ensure sharp print output using IronPrint?
To ensure sharp print output with IronPrint, set the `Dpi` property in the `PrintSettings` object to a suitable dpi value like 300, which produces high-quality text and graphics.
Can IronPrint be used for network and virtual printers?
Yes, IronPrint supports printing to local, network, and virtual printers, like 'Microsoft Print to PDF', as long as they are recognized by the operating system.