Click here to Skip to main content
11,711,537 members (84,173 online)
Click here to Skip to main content

Client Side Printing, Scanning in .NET

, 14 Nov 2012 CPOL 29.2K 3.3K 39
Rate this:
Please Sign up or sign in to vote.
To use client side hardware from a intranet web application in ASP.NET.

Introduction

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 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 can not 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 browser but you can not 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.  

Background 

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 IE 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 cant  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 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
{
    [OperationContract]
    Boolean TestPrint();

    [OperationContract]
    Boolean TestScan();
}
  
public class Service1 : IService1
{
    public bool TestPrint()
    {
        WindowsFormsApplication1.TestPrint f = new WindowsFormsApplication1.TestPrint();
        f.Show();
        return true;
    }

    public bool TestScan()
    {
        WindowsFormsApplication1.TestScan f = new WindowsFormsApplication1.TestScan();
        f.Show();
        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 the link for WCF application hosting basics. 

The Formless NotifyIcon Application

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

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

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

// 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();
   printDoc.Printing("Name_of_Printer");
}

// 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();
    tw.Init(this.Handle);
    tw.Acquire();
}

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 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();
    sc.TestPrint();
}

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

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. 

License

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

Share

About the Author

varun bansal
Software Developer (Senior)
India India
Hi, I am software developer from Delhi, India. I am having a total experience of 5 years. 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

 
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 
AnswerRe: missing notify application Pin
Vasssek25-Nov-13 0:31
memberVasssek25-Nov-13 0:31 
Questionerror on 64 bit win 7 Pin
Member 784601216-Jun-13 20:31
memberMember 784601216-Jun-13 20:31 
AnswerRe: error on 64 bit win 7 Pin
varun bansal17-Jun-13 1:00
membervarun bansal17-Jun-13 1:00 
GeneralRe: error on 64 bit win 7 Pin
Member 784601217-Jun-13 1:40
memberMember 784601217-Jun-13 1:40 
GeneralRe: error on 64 bit win 7 Pin
Member 784601217-Jun-13 1:58
memberMember 784601217-Jun-13 1:58 
GeneralRe: error on 64 bit win 7 Pin
Member 784601217-Jun-13 2:26
memberMember 784601217-Jun-13 2:26 
GeneralRe: error on 64 bit win 7 Pin
varun bansal17-Jun-13 21:16
membervarun bansal17-Jun-13 21:16 
GeneralRe: error on 64 bit win 7 Pin
Member 784601218-Jun-13 17:28
memberMember 784601218-Jun-13 17:28 
QuestionNot clear Pin
dtmr.tomar221-Mar-13 23:42
memberdtmr.tomar221-Mar-13 23:42 
please explain how can i run this project.

there are two 2 project in the attachment ,which one run first because when i trying to run this project there is an error of

"There was no endpoint listening at http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/ that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details."

where Is wcf and how can i resolved this error.

Thanks & Regard
Deepak
AnswerRe: Not clear Pin
varun bansal20-Mar-13 4:34
membervarun bansal20-Mar-13 4:34 
GeneralRe: Not clear Pin
Member 430752126-Apr-13 9:33
memberMember 430752126-Apr-13 9:33 
QuestionQuestion regarding intended purposes Pin
Retse11-Dec-12 4:49
memberRetse11-Dec-12 4:49 
AnswerRe: Question regarding intended purposes Pin
varun bansal11-Dec-12 21:43
membervarun bansal11-Dec-12 21:43 
GeneralRe: Question regarding intended purposes Pin
Retse12-Dec-12 3:24
memberRetse12-Dec-12 3:24 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150819.1 | Last Updated 15 Nov 2012
Article Copyright 2012 by varun bansal
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid