Click here to Skip to main content
15,881,204 members
Articles / Web Development / ASP.NET

Client Side Printing, Scanning in .NET

Rate me:
Please Sign up or sign in to vote.
4.83/5 (10 votes)
14 Nov 2012CPOL3 min read 90.5K   6.1K   41   42
To use client side hardware from an 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 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.

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

Image 1

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

C#
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 to the link for WCF application hosting basics.

The Formless NotifyIcon Application

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

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


Written By
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.

Comments and Discussions

 
QuestionHave you created the slution? Pin
Member 1468703812-Jan-20 19:48
Member 1468703812-Jan-20 19:48 
QuestionNeed WCF code Pin
suneta10-Jan-18 1:56
suneta10-Jan-18 1:56 
QuestionClient Side Printing, Scanning in .NET Pin
Ajay_Saini31-Oct-17 22:35
Ajay_Saini31-Oct-17 22:35 
QuestionThanks for sharing this article Pin
Sanket172215-Jun-17 22:45
Sanket172215-Jun-17 22:45 
AnswerRe: Thanks for sharing this article Pin
bionGO1-Oct-17 21:36
bionGO1-Oct-17 21:36 
GeneralRe: Thanks for sharing this article Pin
Sanket172215-Dec-17 19:59
Sanket172215-Dec-17 19:59 
AnswerRe: Thanks for sharing this article Pin
Member 129094082-Aug-18 19:41
Member 129094082-Aug-18 19:41 
QuestionAccess Client Side Printer and Scanner from web Application Pin
rahulbhadouria18-Apr-17 1:49
rahulbhadouria18-Apr-17 1:49 
QuestionMulti threading issue while access the scanner service Pin
prabakaranece@yahoo.co.in22-Mar-17 8:56
prabakaranece@yahoo.co.in22-Mar-17 8:56 
Questionwhere's the f***ing service Pin
Ahmed Ibrahim27-Mar-16 22:50
Ahmed Ibrahim27-Mar-16 22:50 
AnswerRe: where's the f***ing service Pin
varun bansal31-Mar-16 1:45
varun bansal31-Mar-16 1:45 
GeneralRe: where's the f***ing service Pin
Member 129094082-Aug-18 19:42
Member 129094082-Aug-18 19:42 
QuestionThere was no endpoint listening Pin
Member 1055331322-Feb-16 11:14
Member 1055331322-Feb-16 11:14 
AnswerRe: There was no endpoint listening Pin
Ajay_Saini31-Oct-17 22:37
Ajay_Saini31-Oct-17 22:37 
QuestionPlease share WCF Service Source Code Pin
Member 863550523-Nov-15 0:19
Member 863550523-Nov-15 0:19 
QuestionHow does it work ? Pin
tomer@adasystem.com15-Jul-15 19:25
tomer@adasystem.com15-Jul-15 19:25 
QuestionShare WCF code Pin
Apeksha0920-May-15 2:48
Apeksha0920-May-15 2:48 
QuestionPlease Add Source Code Of WCF Pin
jasira17-Jan-15 0:56
jasira17-Jan-15 0:56 
QuestionPlease Add Source Code Of WCF Pin
jasira17-Jan-15 0:54
jasira17-Jan-15 0:54 
QuestionPlease Add Source Code Of WCF Pin
jasira17-Jan-15 0:48
jasira17-Jan-15 0:48 
QuestionSource code for WCF Pin
safeer78626-Oct-14 22:13
safeer78626-Oct-14 22:13 
QuestionHow to access windows application from Service Pin
snprani14-Oct-14 1:04
snprani14-Oct-14 1:04 
Questionproblem Pin
EngHany24-Aug-14 21:45
EngHany24-Aug-14 21:45 
QuestionPrinting to client side Label Printer Pin
raslasagar19-Mar-14 2:58
raslasagar19-Mar-14 2:58 
AnswerRe: Printing to client side Label Printer Pin
varun bansal21-Mar-14 1:25
varun bansal21-Mar-14 1:25 

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.