静默打印可直接从代码将文档发送至打印机——无需对话框、无需用户交互、不打断流程。 对于批量发票处理、自助终端应用和 Windows 服务后台任务等自动化工作流,取消打印对话框是一项硬性要求。 本地System.Drawing.Printing命名空间提供了一条通往静默打印的方法,但它需要事件驱动的样板代码,这样在团队和项目之间的延展性较差。
using IronPrint;// Print a PDF silentlyPrinter.Print("quarterly-report.pdf");// Print from a byte arraybyte[] pdfData = File.ReadAllBytes("shipping-label.pdf");Printer.Print(pdfData);
using IronPrint;
// Print a PDF silently
Printer.Print("quarterly-report.pdf");
// Print from a byte array
byte[] pdfData = File.ReadAllBytes("shipping-label.pdf");
Printer.Print(pdfData);
ImportsIronPrint' Print a PDF silentlyPrinter.Print("quarterly-report.pdf")' Print from a byte arrayDim pdfData AsByte() = File.ReadAllBytes("shipping-label.pdf")Printer.Print(pdfData)
Imports IronPrint
' Print a PDF silently
Printer.Print("quarterly-report.pdf")
' Print from a byte array
Dim pdfData As Byte() = File.ReadAllBytes("shipping-label.pdf")
Printer.Print(pdfData)
using IronPrint;// List all available printersList<string> printers = Printer.GetPrinterNames();foreach (string name in printers){Console.WriteLine(name);}// Target a specific network printervar settings = new PrintSettings{PrinterName = printers.First(p => p.Contains("LaserJet"))};// Print the documentPrinter.Print("document.pdf", settings);
using IronPrint;
// List all available printers
List<string> printers = Printer.GetPrinterNames();
foreach (string name in printers)
{
Console.WriteLine(name);
}
// Target a specific network printer
var settings = new PrintSettings
{
PrinterName = printers.First(p => p.Contains("LaserJet"))
};
// Print the document
Printer.Print("document.pdf", settings);
ImportsIronPrint' List all available printersDim printers AsList(OfString) = Printer.GetPrinterNames()For Each name AsStringIn printersConsole.WriteLine(name)Next' Target a specific network printerDim settings As New PrintSettingsWith { .PrinterName = printers.First(Function(p) p.Contains("LaserJet"))}' Print the documentPrinter.Print("document.pdf", settings)
Imports IronPrint
' List all available printers
Dim printers As List(Of String) = Printer.GetPrinterNames()
For Each name As String In printers
Console.WriteLine(name)
Next
' Target a specific network printer
Dim settings As New PrintSettings With {
.PrinterName = printers.First(Function(p) p.Contains("LaserJet"))
}
' Print the document
Printer.Print("document.pdf", settings)
using IronPrint;// Collect all PDFs in the batch folderstring[] invoices = Directory.GetFiles(@"C:\Invoices\Pending", "*.pdf");// Configure print settings for the batchvar settings = new PrintSettings{PrinterName = "Accounting Printer",NumberOfCopies = 1,Grayscale = true};// Print each invoice and track successesint successCount = 0;foreach (string invoice in invoices){ try {Printer.Print(invoice, settings); successCount++;Console.WriteLine($"Printed: {Path.GetFileName(invoice)}"); } catch (Exception ex) {Console.WriteLine($"Failed: {Path.GetFileName(invoice)}: {ex.Message}"); }}// Report batch resultsConsole.WriteLine($"Batch complete: {successCount}/{invoices.Length} documents printed.");
using IronPrint;
// Collect all PDFs in the batch folder
string[] invoices = Directory.GetFiles(@"C:\Invoices\Pending", "*.pdf");
// Configure print settings for the batch
var settings = new PrintSettings
{
PrinterName = "Accounting Printer",
NumberOfCopies = 1,
Grayscale = true
};
// Print each invoice and track successes
int successCount = 0;
foreach (string invoice in invoices)
{
try
{
Printer.Print(invoice, settings);
successCount++;
Console.WriteLine($"Printed: {Path.GetFileName(invoice)}");
}
catch (Exception ex)
{
Console.WriteLine($"Failed: {Path.GetFileName(invoice)}: {ex.Message}");
}
}
// Report batch results
Console.WriteLine($"Batch complete: {successCount}/{invoices.Length} documents printed.");
ImportsIronPrintImportsSystem.IO' Collect all PDFs in the batch folderDim invoices AsString() = Directory.GetFiles("C:\Invoices\Pending", "*.pdf")' Configure print settings for the batchDim settings As New PrintSettingsWith { .PrinterName = "Accounting Printer", .NumberOfCopies = 1, .Grayscale = True}' Print each invoice and track successesDim successCount AsInteger = 0For Each invoice AsStringIn invoicesTryPrinter.Print(invoice, settings) successCount += 1Console.WriteLine($"Printed: {Path.GetFileName(invoice)}")Catch ex AsExceptionConsole.WriteLine($"Failed: {Path.GetFileName(invoice)}: {ex.Message}")EndTryNext' Report batch resultsConsole.WriteLine($"Batch complete: {successCount}/{invoices.Length} documents printed.")
Imports IronPrint
Imports System.IO
' Collect all PDFs in the batch folder
Dim invoices As String() = Directory.GetFiles("C:\Invoices\Pending", "*.pdf")
' Configure print settings for the batch
Dim settings As New PrintSettings With {
.PrinterName = "Accounting Printer",
.NumberOfCopies = 1,
.Grayscale = True
}
' Print each invoice and track successes
Dim successCount As Integer = 0
For Each invoice As String In invoices
Try
Printer.Print(invoice, settings)
successCount += 1
Console.WriteLine($"Printed: {Path.GetFileName(invoice)}")
Catch ex As Exception
Console.WriteLine($"Failed: {Path.GetFileName(invoice)}: {ex.Message}")
End Try
Next
' Report batch results
Console.WriteLine($"Batch complete: {successCount}/{invoices.Length} documents printed.")
using IronPrint;// Print asynchronously without blocking the threadawaitPrinter.PrintAsync("report.pdf");// Print a batch of reports asynchronouslystring[] files = Directory.GetFiles(@"C:\Reports", "*.pdf");foreach (string file in files){ awaitPrinter.PrintAsync(file);}
using IronPrint;
// Print asynchronously without blocking the thread
await Printer.PrintAsync("report.pdf");
// Print a batch of reports asynchronously
string[] files = Directory.GetFiles(@"C:\Reports", "*.pdf");
foreach (string file in files)
{
await Printer.PrintAsync(file);
}
ImportsIronPrint' Print asynchronously without blocking the threadAwaitPrinter.PrintAsync("report.pdf")' Print a batch of reports asynchronouslyDim files AsString() = Directory.GetFiles("C:\Reports", "*.pdf")For Each file AsStringIn filesAwaitPrinter.PrintAsync(file)Next
Imports IronPrint
' Print asynchronously without blocking the thread
Await Printer.PrintAsync("report.pdf")
' Print a batch of reports asynchronously
Dim files As String() = Directory.GetFiles("C:\Reports", "*.pdf")
For Each file As String In files
Await Printer.PrintAsync(file)
Next