Click here to Skip to main content
Click here to Skip to main content

Client Side Printing, Scanning in .NET

, 14 Nov 2012 CPOL
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.

Comments and Discussions

 
QuestionSource code for WCF Pinmembersafeer78626-Oct-14 23:13 
QuestionHow to access windows application from Service Pinmembersnprani14-Oct-14 2:04 
Questionproblem PinmemberMember 1047110224-Aug-14 22:45 
QuestionPrinting to client side Label Printer Pinmemberraslasagar19-Mar-14 3:58 
AnswerRe: Printing to client side Label Printer Pinmembervarun bansal21-Mar-14 2:25 
Questiondoubts PinmemberErickVG24-Sep-13 9:54 
AnswerRe: doubts Pinmembervarun bansal2-Oct-13 9:51 
Questionmissing notify application Pinmemberabhi.p13-Sep-13 2:23 
AnswerRe: missing notify application PinmemberVasssek25-Nov-13 1:31 
Questionerror on 64 bit win 7 PinmemberMember 784601216-Jun-13 21:31 
AnswerRe: error on 64 bit win 7 Pinmembervarun bansal17-Jun-13 2:00 
GeneralRe: error on 64 bit win 7 PinmemberMember 784601217-Jun-13 2:40 
GeneralRe: error on 64 bit win 7 PinmemberMember 784601217-Jun-13 2:58 
GeneralRe: error on 64 bit win 7 PinmemberMember 784601217-Jun-13 3:26 
GeneralRe: error on 64 bit win 7 Pinmembervarun bansal17-Jun-13 22:16 
GeneralRe: error on 64 bit win 7 PinmemberMember 784601218-Jun-13 18:28 
QuestionNot clear Pinmemberdtmr.tomar222-Mar-13 0:42 
AnswerRe: Not clear [modified] Pinmembervarun bansal20-Mar-13 5:34 
GeneralRe: Not clear PinmemberMember 430752126-Apr-13 10:33 
QuestionQuestion regarding intended purposes PinmemberRetse11-Dec-12 5:49 
AnswerRe: Question regarding intended purposes Pinmembervarun bansal11-Dec-12 22:43 
GeneralRe: Question regarding intended purposes PinmemberRetse12-Dec-12 4: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
Web02 | 2.8.141220.1 | Last Updated 15 Nov 2012
Article Copyright 2012 by varun bansal
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid