Can we get a FileStream for a Printer? - c#

If I understand it right, a printer is also a file from the operating system's view. Can I get a .NET C# FileStream type for a printer? It seems wild to do this.
Thanks in advance.

A printer is modeled in Win32 as a set of bitmaps. This is wrapped by the PrintDocument, which basically sets up a callback that lets you draw each page individually and sends them one by one to the printing spooler which then sends the data (in .ps format usually) to the printer.
This is how modern printers operate, they draw pages, not text. Only the old school matrix printers drew text character by character, in a stream-like fashion. They used to use the "PRN:" special file. This has lost all meaning in a modern environment however.
The good news is, it's really easy to work with the PrintDocument class, you get a Graphics object for each page that you can use to print text anywhere on the page, as well as draw graphics.
You can simulate a stream-like printer if you so wish by caching all the data then just calling DrawText for every page.

Building a Windows Forms application, you can think of a printer as a Graphics Device.
The System.Drawing.Printing namespace provides print-related services for Windows Forms applications.
If you want to print from a Windows Presentation Foundation (WPF) application, see the System.Printing namespace. It provides classes that enable you to automate the management of print servers, print queues, and print jobs.

Assuming that you want to send the raw data to printer, please see How to send raw data to a printer by using Visual C# .NET

Since .Net 3.0 you can, by using the
PrintSystemJobInfo.JobStream
When you create a printjob in the spooler, e.g.
var job = LocalPrinter.GetDefaultQueue().AddJob();
You can write to the
job.JobStream
Closing that stream queues it for printing.
Interestingly, you can use that stream to write control codes and text to a generic/text-only printer (e.g. LPT1 parallel port impact printer)

Related

windows forms print pdf using axAcroPDF

ASP.NET 4.5, c#, VS2013
I have a windows form application in c#. The application creates a pdf and I wanted to print it to a specific printer.
I could do:
axAcroPDF1.printWithDialog()
and user selects a printer from the list.
But my users do not like this at all. The preferred printer in this case is way back in the list and they want this to be the default printer for this program only.
I am not able to see a default printer like property in axAcroPDF and I do not know how to set the default printer for this windows application only.
Please advise an optimum solution. I did not expect this to get so complicated.
Thanks,
I don't think you can set the default printer per application, only per user. (I'm all ears if this is not the case!)
You could try programmatically getting the default printer, setting it to what you require, and then setting it back to its original value once you're done with your PDF. You can accomplish this with the Windows API using SetDefaultPrinter and GetDefaultPrinter.
You'll need to know how to make Windows API calls from C#. Have a look at pinvoke if you're not sure where to start.

Detect how much percentage of the page (page coverage) being printed

In one of my windows application, I am in need of a module where we want to detect the coverage of the page being printed.
Right now, I am using Print Queue Watch library for detecting the printing job events.
But is there a way to detect how much part of the page being printed.?
Thanks in advance.
Since you progammed your application in C# using WinForms, I assume you used a PrintDocument and your printing takes place in a PrintPage event using PrintPageEventArgs.Graphics.
If this is the case, it should be possible to refractor the code which prints into a seperate method, which takes a an instance of Graphics as parameter (and probably additional parameters, like page size and so on).
Then, you could create a (white) Bitmap object with sufficient size (and the same aspect ratio as your paper), and supply it to the method described above. Then, you can go and count the pixels which have been colored and thus, caculate the percentage of the page which the printer would have printed on.
Keep in mind that you probably have to respect the margin around the printing area and different paper sizes for your calculation.
If the refractoring of the code prooves to heavy, you could create a wrapper for the Graphics object, which "duplicates" all draw calls to a Bitmap while printing, and then continue with counting the pixels.
(This answer assumes you want to monitor your application, not other applications on the system)
I know one way to do that. You need to create a Print Processor. The print processor is a driver that can change or read data before it is sent to the Print Monitor (This is the driver that emit things in proprietary format to the printer port).
See here a global view of the printing architecture http://technet.microsoft.com/en-us/library/cc976755.aspx.
Here is a description of what a Printer Processor does: https://serverfault.com/questions/304536/what-is-the-reason-for-choosing-a-different-print-processor.
How to change the print processor by using PowerShell: http://blogs.technet.com/b/chad/archive/2012/10/31/tip-52-mixing-the-old-and-new-setting-a-printer-s-print-processor-using-powershell-wmi-and-setprinter-exe.aspx
And here is a sample on how to do it (i.e., to create a print processor): http://code.msdn.microsoft.com/windowshardware/Genprint-4a71134f.
To make it work you will need to redirect GdiPlayPageEMF to a bitmap on memory, then you can count its pixels before it is send to the handle of the printer. You can search about GdiPlayPageEMF on Google, there will be interesting results on how to do this kind of thing.
Also, there are a lot of good info about printing here http://www.undocprint.org/winspool/spool_files#reading_job_data.
I hope this helps.

How to design the page and print it on the thermal printer on Epson TM-T88V POS Printer

I am creating the Web Application, After completion of payment i need to get the receipt from the Epson TM-T88V POS Printer in c# using ASP.Net. and after displaying the relevant information on the paper i need to display the Barcode also.
Will it possible to design the page and print it.
Thanks in advance,
Vara Prasad.M
Sure this can be done, however I suspect you are wanting it to be seamless, which can't be done without Silverlight, Java or another embedded solution.
What you can do however is use HTML/CSS and a Barcode image generator to create a page that contains your receipt, then you can simply print via the browser onto your thermal printer.
The only thing that you may find is that your browser often adds a header/footer to your pages and these will appear on your receipt.
A better solution is to use the Silverlight or Java to handle this, as it would have direct communication with the printer, rather than having to rely on the browser.
Link to a possible barcode generator.
http://www.codeproject.com/Articles/20823/Barcode-Image-Generation-Library

telnet/terminal emulation for silverlight/wp7

I'm attempting to put together a telnet client for wp7 mango, so I can play some muds on my dev phone. I am having difficulty finding a decent open source terminal emulation library that will work for Silverlight (mango is roughly equivalent to the sl 4 stack). I see amuse, but that was written for wpf and sl doesn't seem to have that flow layout control it uses to display the telnet console. Any suggestions? I am already able to connect to a telnet server and get a two way stream, but of course i get mostly garbage without term emulation.
Edit: I ended up implementing my own ascii escape code (a sub-set of VT100 essentially, with unsupported options parsed but ignored) and telnet server command parsing engine (it wasn't as difficult as I thought it would be), and also my own rendering code using the RichTextBox control, which was necessary due to limitations of silverlight on the phone which aren't there in either silverlight for the browser or WPF in windows.
You could try to contact Stefan Olson, who implemented a FlowDocument Viewer for Silverlight (unfortunately he didn't have time yet to clean it up and release the code):
http://www.olsonsoft.com/blogs/stefanolson/post/Introduction-to-FlowDocument-for-Silverlight.aspx
Are you asking for a technical layout, or a complete component? In regards to the flow layout, all you need is to represent text, so a StackPanel with TextBlock's in should work just fine.
A TextBlock can support multiple Run elements, which pratically gives you a flow-layout.
Either way, for a phone you should look into customizing the UI to suit your needs, rather than expecting a component to do it for you. If there was a component for it, there most likely would be a app for it, and then there wouldn't be a reason to write another app for it, would there now?-)
I ended up implementing my own terminal emulation/telnet negotiation code from scratch.

.Net Printing from a web site?

Scenario- I have a website that tracks/manages data stored in a DB(I know this covers 90% of websites :)). I have users that need to print specialized labels and barcodes. The app will need to print to a specialized Zebra RFID printer as well as a specific printer for bar codes. All users must use IE 7. Only 3-4 users will print these items. I want to give the user a button they press and then the barcodes/reports are just printed.
Question-
I can create all the files that need to be printed on the server but can I use an XBAP to automate the specialized printing? If not is there any other way?
Access to special hardware from within Internet Explorer is not always easy - even for something as basic as a printer. You can try using the built-in printing facilities of IE7 - but you are likely to run into formatting and layout problems when printing something like barcodes.
XBAPs run an a sandbox and have limited access to hardware as well - but you may be able to print from one - it all depends on whether you need access to any Win32 print or device APIs.
A gauranteed way to implement something like this is using an ActiveX control - but that a much more complicated thing to implement - and it will only work on Internet Explorer - and on a machine that is configured to allow ActiveX controls to run.

Categories

Resources