Click here to Skip to main content
15,039,359 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
dear all,

I am making a Web Part for a sharepoint site with .NET (visual studio 2010).

How can I add a button in my web part, clicking on which will print a .text file stored on my server's c: drive to

a printer on the client side without prompting for a 'print' dialogue box?

The file to be printed is in the server's c: drive where the sharepoint site is hosted.

The printer will be a local printer attached to the client machine.

I have used the code at http://www.dotnetspider.com/resources/43366-Printing-Datatable-Data-In-C.aspx for creating

the .text file.

Please suggest.

Thanks in advance.
Posted

You can't change how the browser works. However, you should know that the code that you've copied, will not work all, and if you change the folder it stores the txt file in, in order that your web site would have access to it, it will print your document using whatever printer is attached to the server. It's clear you don't understand this code, or how the web works. C# code runs on the server. When you test, the server and the client are the same, but when someone looks at your page, they are not. The only way to print a web page, is to use window.print() in a browser window that is showing what you want printed, and nothing more. You can't get around getting a dialogue box asking you what printer to use.
   
Comments
nagen bihari 26-Jul-12 1:34am
   
Thanks for the reply.

What about printing the .txt file to a network printer with an IP address?
I have seen .NET applications(in restaurants) where when one places an order and clicks confirm, a dynamic crystal report is generated and the same is printed from a network printer directly--without prompting for a print dialogue box.

Any suggestions?
bbirajdar 26-Jul-12 10:42am
   
Have you observed it keenly ? Those are desktop applications and not web applications..
nagen bihari 27-Jul-12 6:32am
   
Those are certainly Web Applications. I have the source code for a simple Web Based Application for Billing. But here the reports are generated in Crystal Report and directly send to a Network Printer on confirming the order. The end user only gets the illusion that-'click the confirm order button and get the slip from the printer'.
The same I want to do with a .txt file----which of course I don't know how!
bijumonjoseph 18-Oct-13 4:23am
   
Can you send me the code. I have a similar issue of printing receipt directly on the printer.

Regards
Biju
bijumonjoseph 18-Oct-13 4:23am
   
Can you send me the code. I have a similar issue of printing receipt directly on the printer.

Regards
Biju
Christian Graus 26-Jul-12 1:59am
   
You can do that, if this is an intranet application. But if it's running on the internet, you can't. The code you found is a hack at best, if you wrote actual printing code, you could control it better, but it would still print somewhere on the network of the server, and if your users are that close to the server, why not write a client app instead of a web one ?
nagen bihari 26-Jul-12 4:28am
   
Thanks.
Actually we are using it in our local network. The main problem is that ---we have Sharepoint server 2010 in our office and everybody is used to it. I am developing some web parts that can be deployed in the existing Sharepoint web pages.
In this context, I am making a Web Part which will take data from an existing sharepoint list, populate it in a grid view and if the user wants, will give a printout of the required information from a printer(local or network) on click of a button.
By far, I was able to take out the data from the sharepoint list, give it to a data table and export the contents of the table in to a text file.
Now I want the text file to be printed AUTOMATICALLY from any printer without a second click!

Is it possible?
Christian Graus 26-Jul-12 18:10pm
   
If you're using the browser to access your data, then the only way it's possible, is for you to write actual printing code, instead of the hack you linked to, and print it over the network, from the server.
nagen bihari 27-Jul-12 6:25am
   
Thank you.
Can you help me with the c# code to send it across a network printer?
Christian Graus 28-Jul-12 2:06am
   
You basically need to write code that actually prints, instead of writing code to send a print command to another application.
Dear all

Finally, I have solved the problem.

Made a .dll filr from the link http://stackoverflow.com/questions/3507187/how-to-print-a-pcl-file-in-c.

Added the dll to the GAC.

Call it in my code.....and voila.......It prints my text file.

Note that I am using a HP Laserjet P2015 PCL6.

......Thanks to all for your valuable guidance.

...and for those who thought it was IMPOSSIBLE, take a look at the above link.

Thank you all again!
   
Comments
bbirajdar 28-Jul-12 6:08am
   
Great..thank you for posting the solution
The print dialog prompt is a security feature built into the browsers so that you should not waste the users' printing paper by printing your company's pizza menus whenever the user visits your website ... :). You own the website, not the users' printer and the paper. Let him decide whether to print or not.

It cannot be disabled since this is how the browser works. Even if it could be disabled ( I don't know how, but if somebody really does ), it would be a security breach or hack for which this forum does not provide any code or solution.
   
v3
Comments
nagen bihari 27-Jul-12 6:24am
   
Thanks for the reply.
But what about a network printer? I want a predefined network printer to print a .txt file on click of a button without prompting for a print dialogue. 'JUST CLICK AND GET A PRINTOUT' type.
Any help with the code please?
bbirajdar 27-Jul-12 6:52am
   
You are not getting the point. Its the difference between the "desktop application" and "web application". A web application makes no difference between a network printer and a local printer. If you give a PRINT command via a web browser in ANY ANY way, it will show the prompt.. There is no bypassing for this functionality. So printing ( whether with local printer or network printer) using web app is NOT possible with your 'JUST CLICK AND GET A PRINTOUT'. But it works with windows app.


Some body here http://www.daniweb.com/web-development/aspnet/threads/200452/printing-at-client-side-without-print-dialog-box it works but use it at your own risk
nagen bihari 27-Jul-12 23:23pm
   
Thanx.
But don't say it is not possible. I have the code for a Web Application that print crystal reports to a predefined network printer just on click of a button without prompting for 'Print' dialogue. But not able to figure out how to do the same for a text file!
bijumonjoseph 18-Oct-13 4:21am
   
Can you send me the code. I have a similar issue and need to print receipt directly to the printer.

Thanks
Biju
Er Atul Sharma 15-May-15 4:04am
   
Hi,
can you send this code to me please, i have a similar issue and need to print directly to printer, on my mail id is atulsharmaaa1987@gmail.com ASAP., its urgent to me.
Christian Graus 28-Jul-12 2:06am
   
The problem is, you're not doing any printing at all, the application that launches the document is.

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




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900