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.