Click here to Skip to main content
15,440,621 members
Articles / Desktop Programming / Win32
Tip/Trick
Posted 27 Aug 2013

Stats

75.9K views
4.8K downloads
21 bookmarked

Print Directly to Selected Local or Network Printer

Rate me:
Please Sign up or sign in to vote.
4.93/5 (9 votes)
29 Aug 2013CPOL3 min read
This little simple and easy to read program will help you understand how to print to a specific printer, local or shared on a network.

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.

C#
using System.Management;

You also want to add the using clause to be able to use the printing commands.

C#
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.

C#
//We use the ObjectQuery to get the list of configured printers
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:

C#
//We are simply going to build a string saying "This is a test".
using (Graphics g = e.Graphics)
{
    // Let's create a StringFormat object with each line of text, 
    // and the block of text centered on the page.
    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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Chronometriq
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
PraiseThank you, very useful!!! Pin
Jcarrazco24-Mar-20 11:06
MemberJcarrazco24-Mar-20 11:06 
PraiseAmazing Pin
Member 1206737419-Sep-17 18:55
MemberMember 1206737419-Sep-17 18:55 
It's simple and amazing... Thanks for sharing the valuable asset
GeneralRe: Amazing Pin
Max Methot19-Jan-18 4:50
MemberMax Methot19-Jan-18 4:50 
Questionfind the computer connected to a local printer Pin
Member 117805428-Jul-15 17:19
MemberMember 117805428-Jul-15 17:19 
AnswerRe: find the computer connected to a local printer Pin
Max Methot21-Jul-15 14:28
MemberMax Methot21-Jul-15 14:28 
GeneralRe: find the computer connected to a local printer Pin
Member 1178054223-Jul-15 1:50
MemberMember 1178054223-Jul-15 1:50 
GeneralRe: find the computer connected to a local printer Pin
Max Methot23-Jul-15 4:10
MemberMax Methot23-Jul-15 4:10 
GeneralGreate Job Pin
Member 108424029-Mar-15 22:27
MemberMember 108424029-Mar-15 22:27 
GeneralRe: Greate Job Pin
Max Methot10-Mar-15 1:59
MemberMax Methot10-Mar-15 1:59 
QuestionWhat if the default printer is a network printer? Pin
bitterbyte30-Aug-13 2:24
Memberbitterbyte30-Aug-13 2:24 
AnswerRe: What if the default printer is a network printer? Pin
Max Methot30-Aug-13 3:53
MemberMax Methot30-Aug-13 3:53 
AnswerRe: What if the default printer is a network printer? Pin
Max Methot23-Jul-15 4:06
MemberMax Methot23-Jul-15 4:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.