Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
Programmatic printing on network printers is a common task across various software applications. Whether you're building a desktop, mobile application, or a web service, the ability to send documents directly to a network printer can greatly enhance the usability and efficiency of your software. In this article, we'll explore how to print on a network printer using C# and IronPrint from Iron Software.
Before delving into the coding part, let's ensure that our environment is set up correctly. To print on a network printer in C#, you need to have the following:
Before diving into the implementation details, let's familiarize ourselves with how to get started with IronPrint:
Compatibility and Support: IronPrint offers extensive compatibility and support across various environments and project types:
Now let's see how to print with IronPrint with the following example.
using IronPrint;
// Configure printer setting
PrintSettings printSettings = new PrintSettings();
printSettings.Dpi = 220;
printSettings.NumberOfCopies = 10;
printSettings.PaperOrientation = PaperOrientation.Portrait;
// Print the document
Printer.Print("awesomeIronPrint.pdf", printSettings);
using IronPrint;
// Configure printer setting
PrintSettings printSettings = new PrintSettings();
printSettings.Dpi = 220;
printSettings.NumberOfCopies = 10;
printSettings.PaperOrientation = PaperOrientation.Portrait;
// Print the document
Printer.Print("awesomeIronPrint.pdf", printSettings);
Imports IronPrint
' Configure printer setting
Private printSettings As New PrintSettings()
printSettings.Dpi = 220
printSettings.NumberOfCopies = 10
printSettings.PaperOrientation = PaperOrientation.Portrait
' Print the document
Printer.Print("awesomeIronPrint.pdf", printSettings)
The System.Printing
namespace in C# contains classes and interfaces that enable developers to interact with printers, print queues, print servers, and print jobs programmatically. Some key classes and interfaces within this namespace include:
PrintQueue
: Represents a printer or print device connected to the system.PrintServer
: Represents a print server on the network.PrintSystemJobInfo
: Provides information about a print job, such as its status and properties.PrintTicket
: Represents the settings for a print job, including paper size, orientation, and print quality.Getting Started with System.Printing
: Before we dive into the code, let's ensure that we have a basic understanding of how printing works with System.Printing
:
PrintQueue
Selection: You need to identify the PrintQueue
object representing the printer you want to use for printing.PrintTicket
Configuration: Optionally, you can configure the PrintTicket
object to specify print settings such as paper size, orientation, and number of copies.PrintQueue
.Implementing Document Printing with System.Printing
: Now, let's walk through the process of printing a document using System.Printing
with the following example:
Here is the source code:
using System;
using System.IO;
using System.Printing;
class Program
{
static void Main(string[] args)
{
// Specify the path to the document to be printed on the local printer
string documentPath = "path/to/your/document.pdf";
// Instantiate a Printer Server object representing the local print server
using (PrintServer printServer = new PrintServer())
{
// Get the default PrintQueue from the PrintServer default
PrintQueue defaultPrintQueue = printServer.DefaultPrintQueue;
// Create a PrintTicket object to specify print settings (optional)
PrintTicket printTicket = new PrintTicket();
// Configure print settings, e.g., number of copies, paper size, orientation
printTicket.CopyCount = 1;
// Print the document to the default PrintQueue with the specified PrintTicket
defaultPrintQueue.AddJob("MyPrintJob", documentPath, false, printTicket);
}
Console.WriteLine("Document sent to print queue.");
}
}
using System;
using System.IO;
using System.Printing;
class Program
{
static void Main(string[] args)
{
// Specify the path to the document to be printed on the local printer
string documentPath = "path/to/your/document.pdf";
// Instantiate a Printer Server object representing the local print server
using (PrintServer printServer = new PrintServer())
{
// Get the default PrintQueue from the PrintServer default
PrintQueue defaultPrintQueue = printServer.DefaultPrintQueue;
// Create a PrintTicket object to specify print settings (optional)
PrintTicket printTicket = new PrintTicket();
// Configure print settings, e.g., number of copies, paper size, orientation
printTicket.CopyCount = 1;
// Print the document to the default PrintQueue with the specified PrintTicket
defaultPrintQueue.AddJob("MyPrintJob", documentPath, false, printTicket);
}
Console.WriteLine("Document sent to print queue.");
}
}
Imports System
Imports System.IO
Imports System.Printing
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Specify the path to the document to be printed on the local printer
Dim documentPath As String = "path/to/your/document.pdf"
' Instantiate a Printer Server object representing the local print server
Using printServer As New PrintServer()
' Get the default PrintQueue from the PrintServer default
Dim defaultPrintQueue As PrintQueue = printServer.DefaultPrintQueue
' Create a PrintTicket object to specify print settings (optional)
Dim printTicket As New PrintTicket()
' Configure print settings, e.g., number of copies, paper size, orientation
printTicket.CopyCount = 1
' Print the document to the default PrintQueue with the specified PrintTicket
defaultPrintQueue.AddJob("MyPrintJob", documentPath, False, printTicket)
End Using
Console.WriteLine("Document sent to print queue.")
End Sub
End Class
System.Printing
This table provides a quick overview of the features and characteristics of IronPrint and System.Printing
, helping developers make informed decisions based on their specific requirements and platform targets.
Create a console application in Visual Studio as shown below.
Provide a project name and location.
Select the .NET version.
Now the project is created and ready for further coding.
Install IronPrint from Visual Studio Package Manager as shown below.
IronPrint can also be installed using the command below:
Install-Package IronPrint
To list the printer names using IronPrint use the code below:
using IronPrint;
namespace IronPrintDemo;
public class Program
{
public static void Main()
{
// Get printer names using printer drivers
List<string> printersName = Printer.GetPrinterNames();
foreach (var printer in printersName)
{
Console.WriteLine(printer);
}
}
}
using IronPrint;
namespace IronPrintDemo;
public class Program
{
public static void Main()
{
// Get printer names using printer drivers
List<string> printersName = Printer.GetPrinterNames();
foreach (var printer in printersName)
{
Console.WriteLine(printer);
}
}
}
Imports IronPrint
Namespace IronPrintDemo
Public Class Program
Public Shared Sub Main()
' Get printer names using printer drivers
Dim printersName As List(Of String) = Printer.GetPrinterNames()
For Each printer In printersName
Console.WriteLine(printer)
Next printer
End Sub
End Class
End Namespace
Printer.GetPrinterNames
to get all the available printer drivers.Console.WriteLine
.Use the code below to print the document silently:
using IronPrint;
namespace IronPrintDemo;
public class Program
{
public static void Main()
{
// Print the document silently
Printer.Print("sample.pdf");
}
}
using IronPrint;
namespace IronPrintDemo;
public class Program
{
public static void Main()
{
// Print the document silently
Printer.Print("sample.pdf");
}
}
Imports IronPrint
Namespace IronPrintDemo
Public Class Program
Public Shared Sub Main()
' Print the document silently
Printer.Print("sample.pdf")
End Sub
End Class
End Namespace
After the execution, the document is added to the print queue as shown in the output.
Use the code below to print the document with the dialog:
using IronPrint;
namespace IronPrintDemo;
public class Program
{
public static void Main()
{
// Print with Dialog
Printer.ShowPrintDialog("sample.pdf");
}
}
using IronPrint;
namespace IronPrintDemo;
public class Program
{
public static void Main()
{
// Print with Dialog
Printer.ShowPrintDialog("sample.pdf");
}
}
Imports IronPrint
Namespace IronPrintDemo
Public Class Program
Public Shared Sub Main()
' Print with Dialog
Printer.ShowPrintDialog("sample.pdf")
End Sub
End Class
End Namespace
ShowPrintDialog
from IronPrint we can print with DialogPrinting documents with advanced settings is supported in IronPrint. It supports the following Properties:
PaperSize
: Specifies the paper dimensions utilized by the printer.PaperOrientation
: Defines the orientation of the paper, such as Automatic, Portrait, or Landscape.DPI
: Sets the desired print resolution in dots per inch. The default value is 300, commonly employed in commercial printing. The actual DPI may be constrained by the printer's capabilities.NumberOfCopies
: Indicates the number of identical copies to print for a document.PrinterName
: Specifies the name of the printer designated for printing tasks.PaperMargins
: Determines the margins for printing, measured in millimeters.Grayscale
: A boolean value determining whether to print in grayscale. The default is false.Now let's look at a code example:
using IronPrint;
namespace IronPrintDemo;
public class Program
{
public static void Main()
{
// Configure custom print setting
PrintSettings printSettings = new PrintSettings();
printSettings.Dpi = 150;
printSettings.NumberOfCopies = 2;
printSettings.PaperOrientation = PaperOrientation.Portrait;
// Print the required document
Printer.Print("sample.pdf", printSettings);
}
}
using IronPrint;
namespace IronPrintDemo;
public class Program
{
public static void Main()
{
// Configure custom print setting
PrintSettings printSettings = new PrintSettings();
printSettings.Dpi = 150;
printSettings.NumberOfCopies = 2;
printSettings.PaperOrientation = PaperOrientation.Portrait;
// Print the required document
Printer.Print("sample.pdf", printSettings);
}
}
Imports IronPrint
Namespace IronPrintDemo
Public Class Program
Public Shared Sub Main()
' Configure custom print setting
Dim printSettings As New PrintSettings()
printSettings.Dpi = 150
printSettings.NumberOfCopies = 2
printSettings.PaperOrientation = PaperOrientation.Portrait
' Print the required document
Printer.Print("sample.pdf", printSettings)
End Sub
End Class
End Namespace
IronPrintDemo
namespace, there's a class named Program declared as public.PrintSettings
object named printSettings
is instantiated to configure custom print settings.printSettings
is set to 150, indicating a print resolution of 150 dots per inch.NumberOfCopies
property of printSettings
is set to 2, specifying that two identical copies of the document will be printed.PaperOrientation
property of printSettings
is set to PaperOrientation.Portrait
, indicating that the document will be printed in portrait orientation.printSettings
(the custom print settings). This method presumably handles the printing process using the specified settings.IronPrint from Iron Software is an enterprise library and requires a license to run. Adding an IronPrint license key makes the project live without restrictions or watermarks. Buy a license here or sign up for a free 30-day trial key here.
Once the License key is obtained the key needs to be placed in the appSettings.json file in the application
{
"IronPrint.License.LicenseKey": "IRONPRINT.KEY"
}
This will print documents without restrictions and watermarks.
In summary, IronPrint and System.Printing
each has its strengths and is suitable for different scenarios. IronPrint provides a streamlined and cross-platform solution with a focus on simplicity and versatility, while System.Printing
offers native integration and fine-grained control, particularly for Windows-based applications. Developers should consider their specific requirements and platform targets when choosing between these printing libraries for their C# projects.
IronPrint simplifies document printing within .NET applications by providing a user-friendly API and extensive compatibility across various platforms and project types. By following the steps outlined in this article, you can seamlessly integrate printing functionality into your .NET projects, enhancing their usability and productivity. Explore the possibilities with IronPrint and unlock a world of efficient document printing within your applications.
9 .NET API products for your office documents