Class Printer
The Printer class allows you to easily print various file types including:
- PNG
- JPG
- BMP
- TIFF
Call `Printer.Print()` for silent printing
Call `Printer.ShowPrintDialog()` for display the print dialog.
We also provide an `Async` functions to prevent the print operation from blocking your thread.Inheritance
Namespace: IronPrint
Assembly: IronPrint.dll
Syntax
public static class Printer : Object
Printer sends a document to a physical printer from C#, and it does so on Windows, macOS, Linux, Android, and iOS through one API. Hand it a file path or a byte[] of file data and it queues the job on the default printer or on one named through PrintSettings. Jobs print silently by default, so a server or background service can print with no user present.
The members fall into three groups. Printing methods (Print and PrintAsync) send a job straight to the device. The interactive methods (ShowPrintDialog and ShowPrintDialogAsync) open the operating system print dialog so the user picks the printer and options. The discovery methods (GetPrinterNames and GetPrinterTrays) list the printers and trays available on the machine. Because the class is static, there is nothing to construct: call the methods directly after rendering or loading the file you want on paper. Every printing and dialog call takes an optional PrintSettings, and each has an asynchronous form that returns a Task to keep the operation off the calling thread.
A typical job builds a PrintSettings, sets the values that matter, and passes it to Print. When the target device is not known ahead of time, call GetPrinterNames first and assign the chosen name to PrintSettings.PrinterName, or call ShowPrintDialog and let the user choose. Where blocking the thread is a concern, prefer PrintAsync or ShowPrintDialogAsync and await the returned Task. The print settings guide covers the options each call accepts.
using IronPrint;
PrintSettings settings = new PrintSettings { NumberOfCopies = 2 };
Printer.Print("invoice.pdf", settings);The silent printing guide walks through unattended jobs, the print with dialog guide covers interactive printing, and the retrieve printer names guide shows how to enumerate devices first.
Methods
GetPrinterNames()
Retrieves a list of printer names available on the system.
Declaration
public static List<string> GetPrinterNames()
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.List<System.String> | A list of strings, each representing a printer name. |
GetPrinterNamesAsync()
Asynchronously retrieves a list of printer names available on the system.
Declaration
public static Task<List<string>> GetPrinterNamesAsync()
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task<System.Collections.Generic.List<System.String>> | The task result contains a list of strings, each representing a printer name. |
GetPrinterTrays(String)
Retrieves a list of printer tray available on the specified printer, or for a default printer if no specific printer is provided.
Declaration
public static List<string> GetPrinterTrays(string printerName = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | printerName | The name of the printer for which to retrieve tray information. Default |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.List<System.String> | A list of strings, each representing a tray name. |
GetPrinterTraysAsync(String)
Asynchronously retrieves a list of printer tray available on the specified printer, or for a default printer if no specific printer is provided.
Declaration
public static Task<List<string>> GetPrinterTraysAsync(string printerName = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | printerName | The name of the printer for which to retrieve tray information. Default |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task<System.Collections.Generic.List<System.String>> | The task result contains a list of strings, each representing a tray name. |
Print(Byte[], PrintSettings)
Silently print (without a dialog) from a PDF or an image file data.
Declaration
public static void Print(byte[] fileData, PrintSettings settings = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Byte[] | fileData | A PDF or an image file data in byte[] |
| PrintSettings | settings | (Optional) The PrintSettings to use for printing. |
Remarks
Note: If this operation block your thread, Please use PrintAsync(Byte[], PrintSettings) instead
Note: In mobile, Silently print is not possible. This operation will show print dialog anyways
Print(String, PrintSettings)
Silently print (without a dialog) from a PDF or an image file path.
Declaration
public static void Print(string path, PrintSettings settings = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | path | The path of a PDF or an image. |
| PrintSettings | settings | (Optional) The PrintSettings to use for printing. |
Remarks
Note: If this operation block your thread, Please use PrintAsync(String, PrintSettings) instead
Note: In mobile, Silently print is not possible. This operation will show print dialog anyways
PrintAsync(Byte[], PrintSettings)
Silently print asynchronously (without a dialog) from a PDF or an image file data.
Declaration
public static Task PrintAsync(byte[] fileData, PrintSettings settings = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Byte[] | fileData | A PDF or an image file data in byte[] |
| PrintSettings | settings | (Optional) The PrintSettings to use for printing. |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task | An awaitable task that starts printing. |
Remarks
Note: In mobile, Silently print is not possible. This operation will show print dialog anyways
PrintAsync(String, PrintSettings)
Silently print asynchronously (without a dialog) from a PDF or an image file path.
Declaration
public static Task PrintAsync(string path, PrintSettings settings = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | path | The path of a PDF or an image file. |
| PrintSettings | settings | (Optional) The PrintSettings to use for printing. |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task | An awaitable task that starts printing. |
Remarks
Note: In mobile, Silently print is not possible. This operation will show print dialog anyways
ShowPrintDialog(Byte[], PrintSettings)
Shows a print dialog asynchronously for a PDF or an image file data in byte[].
The visual and functional aspects of the print dialog may vary based on your platform.
Declaration
public static void ShowPrintDialog(byte[] fileData, PrintSettings settings = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Byte[] | fileData | A PDF or an image file data in byte[] |
| PrintSettings | settings | (Optional) The PrintSettings to use for printing. |
Remarks
Note: If this operation block your thread, Please use ShowPrintDialogAsync(Byte[], PrintSettings) instead
ShowPrintDialog(String, PrintSettings)
Shows a print dialog for a PDF or an image path.
The visual and functional aspects of the print dialog may vary based on your platform.
Declaration
public static void ShowPrintDialog(string path, PrintSettings settings = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | path | The path of a document. |
| PrintSettings | settings | (Optional) The PrintSettings to use for printing. |
Remarks
Note: If this operation block your thread, Please use ShowPrintDialogAsync(String, PrintSettings) instead
ShowPrintDialogAsync(Byte[], PrintSettings)
Shows a print dialog asynchronously for a PDF or an image file data in byte[].
The visual and functional aspects of the print dialog may vary based on your platform.
Declaration
public static Task ShowPrintDialogAsync(byte[] fileData, PrintSettings settings = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Byte[] | fileData | A PDF or an image file data in byte[] |
| PrintSettings | settings | (Optional) The PrintSettings to use for printing. |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task | An awaitable task that shows a printing dialog. |
ShowPrintDialogAsync(String, PrintSettings)
Shows a print dialog asynchronously for a PDF or an image path.
The visual and functional aspects of the print dialog may vary based on your platform.
Declaration
public static Task ShowPrintDialogAsync(string path, PrintSettings settings = null)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | path | The path of a document. |
| PrintSettings | settings | (Optional) The PrintSettings to use for printing. |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task | An awaitable task that shows a printing dialog. |