Introduction
I started developing this application because I was facing a major constraint at work. We do business in a medical center, and I had to send a print command to a POS label printer available on the network from the front desk, without changing a thing to the default printer configuration of the computer, since they use it to print patient data on a specific kind of paper on a very specific printer. With this neat tool, I was able to send my print jobs to the printer I wanted, without disrupting the computer configuration.
Background
This is a really simple program that is easy to understand and does not require a high level of coding experience. I used the System.Drawing.Printing
and System.Management
namespaces in order to achieve what I wanted. With the System.Drawing.Printing
namespace, I build the data to send to the printer and send the print job. With the System.Management
namespace, I search for all installed local and network printers. That really is about it.
Using the Code
First, you will want to add the System.Management
library to your project. To do so, right-click the References folder in the Solution Explorer and click on Add Reference... Then, in the .NET tab, select the System.Management
component and press OK.
You will then want to add the using
clause to be able to use the library you just added to your project.
using System.Management;
You also want to add the using
clause to be able to use the printing commands.
using System.Drawing.Printing;
To be able to send the print job to the printer, we have to have a PrintDocument
, with a PrintPage
event attached to it. This is where our data will be built before sending it to the printer.
The tricky part in this program is to find all the printers on the system and the network. This is done with this little bit of code, that I placed in a private
method called LoadPrinters
. I then call this method in the constructor and in the Refresh button click event.
System.Management.ObjectQuery oquery =
new System.Management.ObjectQuery("SELECT * FROM Win32_Printer");
System.Management.ManagementObjectSearcher mosearcher =
new System.Management.ManagementObjectSearcher(oquery);
System.Management.ManagementObjectCollection moc = mosearcher.Get();
What this little snippet does is that it fetches the printer objects with the "SELECT * FROM Win32_Printer
" query. If any found, we then loop through all of them and split them in two categories: local and network and add them to the right list accordingly.
Once we have all the printers, the user will be able to choose the printer he wants to print to and send the print job to the selected printer.
As said earlier, the data sent to the printer is built in the printDoc_PrintPage
event. It looks like this:
using (Graphics g = e.Graphics)
{
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
using (Font font1 = new Font("Arial", 11))
{
RectangleF rectF1 = new RectangleF(15, 85, 160, 15);
e.Graphics.DrawRectangle(Pens.White, Rectangle.Round(rectF1));
g.DrawString("This is a test", font1, Brushes.Black, rectF1, stringFormat);
}
}
We simply use the Graphics
object to create a string
of text and place it in a Rectangle
at a specific location on the document. This will then be sent to the printer as the text to be printed. Not that you can replace this with anything, such as shapes, images and every other type of object the Graphics
object supports.
Points of Interest
This program is a simple way to interact with USB, COM or LPT printer devices without having to deal with Interops or ActiveX or anything like that. I hope you will find this as useful as I do!
History
- Version 1.0
- Version 2.0: Added the printer's information as well as a better sorting process for the local and network printers