views:

62

answers:

3

Hi,

I'm developing a web application where user require to print a particular form with a single click. Based on my findings I understand that we need to create a printable version of the form and then send it to the printer.

But still I could not find out a way to send such printable version of the form directly to printer without displaying the generated form to user. Appreciate any suggestions from you.

Thank You

A: 

I do this by loading the print view into a hidden iframe and issuing the .print() javascript command

Jamiec
I should add that this just pops up the default Print dialog. It does not send it direct to printer - which is impossible for obvious reasons.
Jamiec
Thankz Jami, I tried your way, it works fine and pop up dialog would rather better approach than direct printing.
nimo
+1  A: 

This is the code you need:

 <img src="print_button.jpg"  onClick="window.print()" alt="Print this page" style="cursor: pointer;">

You will also need to make a print css file that will style the printed page and to put this in the header:

<link rel="stylesheet" media="print" type="text/css" href="print.css" />

That should take care of what you need.

Nicknameless
But you still get a popup print dialog box, don't you?
DOK
You do, but you absolutely should. As a developer you don't know which printer the user wants to use or if they actually want to print. You should never force your users to do things they don't want to do.
Nicknameless
A: 

If you can print from the server to a designated printer (i.e., you're in an intranet), you can do something like this. There's more code than you want to see here -- get the full code here. The key is that you nave a network printer configured with permissions for ASPNET (or the account it's running under) to print to it.

I have done this. In fact, we were printing so much that we moved the printing from the web server to a web service or Windows service on another machine, to get the load off the web server.

using System.Drawing.Printing;
using System.Text;
using System.Web;
using System.IO;

PrintDocument doc = new PrintDocument();
// set the printer name
doc.PrinterSettings.PrinterName = printerName;
// print the page
doc.Print();
DOK