Click here to Skip to main content
13,593,237 members
Click here to Skip to main content
Add your own
alternative version


42 bookmarked
Posted 14 Nov 2012
Licenced CPOL

Client Side Printing, Scanning in .NET

, 14 Nov 2012
Rate this:
Please Sign up or sign in to vote.
To use client side hardware from an intranet web application in ASP.NET


Often, there are requirements by clients to make a web application that can handle hardware installed on a client machine. It sounds completely logical to a user to fire a print/scan command from his/her own browser and the scanner/printer attached to the machine should start working. It looks so logical to users but programmers know that it is not logical actually. Browser applications are not designed to access system resources.

Browser is an application to work in a client - server scenario and browsers cannot gain access to system resources and manipulate them. This would be a security breach if allowed and a remote code can manipulate local systems.

There are limitations inside a browser, you can print what you are seeing in a browser but you cannot access any document from the local computer, manipulate it, and print it with a desired printer silently. Some user intervention is always required, which means pop up windows. Worse is the case with scanners and other hardware devices where you can't even talk to them via client side scripts.


Recently, I also got a requirement for client side printing via browser and I spent several days searching all kinds of possible ways. I found that using ActiveX controls, you can control printing to an extent in Internet Explorer, but it is not a solution anybody will like to use. I went further to use a Windows Service for accessing the printer but a Windows Service too has limitations with other hardware like scanners mainly because the drivers of some hardware devices want to interact with the user and the Windows Service creates problems as they are not designed to have user interaction.

In cases where a scanner wanted to display a sample scanned image to user or in case of a bar-code scanner, we can't even use windows services. In a last attempt, I tried to use JavaScript and tried to consume a self hosted WCF service using JavaScript. I used a new feature in WCF which allows cross browser access of service. But I was not able to make it work. If somebody has worked on this idea, please share.

Finally I admitted that, if all of the devices are installed on the client machine, a Windows Forms application is the way to talk to all devices seamlessly. So finally, I decided to make a formless notify icon application and host a WCF service on it and it worked for me.

The hardware access scenario is a bit indirect in this case, means when the user fires a print or scan command, it goes to the server first and then the server will call the client machine's WCF service to print/scan. Please note that the "WCF service" box inside the NotifyIcon application box shows that the WCF service is hosted on the NotifyIcon app.

Using the Code

The WCF Service

public interface IService1
    Boolean TestPrint();

    Boolean TestScan();
public class Service1 : IService1
    public bool TestPrint()
        WindowsFormsApplication1.TestPrint f = new WindowsFormsApplication1.TestPrint();
        return true;

    public bool TestScan()
        WindowsFormsApplication1.TestScan f = new WindowsFormsApplication1.TestScan();
        return true;

The service includes two methods only, one for print and one for scanning. We can have as many devices as we want to control using this service. Also, we can change the print settings before printing. This gives us complete control on the hardware as the service is hosted on a form application. A basic knowledge of WCF is expected here. Refer to the link for WCF application hosting basics.

The Formless NotifyIcon Application

ServiceHost sHost; 
private void Form1_Load(object sender, EventArgs e)
    sHost = new ServiceHost(typeof(Service1)); // to host the WCF service.

private void Use_Notify()
{ //set balloon tip properties.
    MyNotify.ContextMenuStrip = contextMenuStrip1;
    MyNotify.BalloonTipText = "Print/Scan App";
    MyNotify.BalloonTipTitle = "Print/Scan App";

private void Form1_Activated(object sender, EventArgs e)
   //to hide form just after opening it.

// This form opens up when Print button is pressed in browser. <br/>
// All Printer handling should be done here.   <br/>
private void TestPrint_Load(object sender, EventArgs e)
   WindowsFormsApplication1.PrintDocumentMethod printDoc = 
       new WindowsFormsApplication1.PrintDocumentMethod();

// This form opens up when scan button is pressed in browser.<br/>
// All scanner handling code should go here.
private void TestScan_Load(object sender, EventArgs e)
    //using a Twain driver for scanning.
    Twain tw = new Twain();

The notify icon application is actually the key to this solution. It looks like a service and it works like a form application as it is a form application actually. Refer to the link for more details. There are two forms, one for printing and one for scanning which are displayed when the user gives the print /scan command, though these forms are not needed as such and can be skipped and you can make this work completely silently.

The ASP.NET Web Application (to Test the Printing/Scanning)

protected void Button1_Click(object sender, EventArgs e)
    ServiceReference1.Service1Client sc = new ServiceReference1.Service1Client();

protected void Button2_Click(object sender, EventArgs e)
    ServiceReference1.Service1Client sc = new ServiceReference1.Service1Client();

The web application page is quite simple. I included only two ASP.NET buttons which call the WCF service methods. A service reference is needed to generate a proxy for the WCF service. Please refer to the attached code.


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


About the Author

varun bansal
Software Developer (Senior)
India India
Hi, I am software developer from Delhi, India. I work in .Net and related technologies.

Personally I like technology and playing with different new features & possibilities.

You may also be interested in...

Comments and Discussions

QuestionNeed WCF code Pin
suneta10-Jan-18 1:56
membersuneta10-Jan-18 1:56 
QuestionClient Side Printing, Scanning in .NET Pin
Ajay_Saini31-Oct-17 22:35
memberAjay_Saini31-Oct-17 22:35 
QuestionThanks for sharing this article Pin
Sanket172215-Jun-17 22:45
memberSanket172215-Jun-17 22:45 
AnswerRe: Thanks for sharing this article Pin
bionGO1-Oct-17 21:36
memberbionGO1-Oct-17 21:36 
GeneralRe: Thanks for sharing this article Pin
Sanket172215-Dec-17 19:59
memberSanket172215-Dec-17 19:59 
QuestionAccess Client Side Printer and Scanner from web Application Pin
rahulbhadouria18-Apr-17 1:49
memberrahulbhadouria18-Apr-17 1:49 
QuestionMulti threading issue while access the scanner service Pin 8:56 8:56 
Questionwhere's the f***ing service Pin
Ahmed Ibrahim27-Mar-16 22:50
memberAhmed Ibrahim27-Mar-16 22:50 
AnswerRe: where's the f***ing service Pin
varun bansal31-Mar-16 1:45
membervarun bansal31-Mar-16 1:45 
QuestionThere was no endpoint listening Pin
Member 1055331322-Feb-16 11:14
memberMember 1055331322-Feb-16 11:14 
AnswerRe: There was no endpoint listening Pin
Ajay_Saini31-Oct-17 22:37
memberAjay_Saini31-Oct-17 22:37 
QuestionPlease share WCF Service Source Code Pin
Member 863550523-Nov-15 0:19
memberMember 863550523-Nov-15 0:19 
QuestionHow does it work ? Pin
tomer@adasystem.com15-Jul-15 19:25
membertomer@adasystem.com15-Jul-15 19:25 
QuestionShare WCF code Pin
Apeksha0920-May-15 2:48
memberApeksha0920-May-15 2:48 
QuestionPlease Add Source Code Of WCF Pin
jasira17-Jan-15 0:56
memberjasira17-Jan-15 0:56 
QuestionPlease Add Source Code Of WCF Pin
jasira17-Jan-15 0:54
memberjasira17-Jan-15 0:54 
QuestionPlease Add Source Code Of WCF Pin
jasira17-Jan-15 0:48
memberjasira17-Jan-15 0:48 
QuestionSource code for WCF Pin
safeer78626-Oct-14 22:13
membersafeer78626-Oct-14 22:13 
QuestionHow to access windows application from Service Pin
snprani14-Oct-14 1:04
membersnprani14-Oct-14 1:04 
Questionproblem Pin
Member 1047110224-Aug-14 21:45
memberMember 1047110224-Aug-14 21:45 
QuestionPrinting to client side Label Printer Pin
raslasagar19-Mar-14 2:58
memberraslasagar19-Mar-14 2:58 
AnswerRe: Printing to client side Label Printer Pin
varun bansal21-Mar-14 1:25
membervarun bansal21-Mar-14 1:25 
Questiondoubts Pin
ErickVG24-Sep-13 8:54
memberErickVG24-Sep-13 8:54 
AnswerRe: doubts Pin
varun bansal2-Oct-13 8:51
membervarun bansal2-Oct-13 8:51 
Questionmissing notify application Pin
abhi.p13-Sep-13 1:23
memberabhi.p13-Sep-13 1:23 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.180618.1 | Last Updated 15 Nov 2012
Article Copyright 2012 by varun bansal
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid