问题
I'm trying to print to Dot Matrix printers (various models) out of C#, currently I'm using Win32 API (you can find alot of examples online) calls to send escape codes directly to the printer out of my C# application. This works great, but...
My problem is because I'm generating the escape codes and not relying on the windows print system the printouts can't be sent to any "normal" printers or to things like PDF print drivers. (This is now causing a problem as we're trying to use the application on a 2008 Terminal Server using Easy Print [Which is XPS based])
The question is: How can I print formatted documents (invoices on pre-printed stationary) to Dot Matrix printers (Epson, Oki and Panasonic... various models) out of C# not using direct printing, escape codes etc.
**Just to clarify, I'm trying things like GDI+ (System.Drawing.Printing) but the problem is that its very hard, to get things to line up like the old code did. (The old code sent the characters direct to the printer bypassing the windows driver.) Any suggestions how things could be improved so that they could use GDI+ but still line up like the old code did?
回答1:
You should probably use a reporting tool to make templates that allow you or users to correctly position the fields with regards to the pre-printed stationery.
Using dot-matrix printers, you basically have to work in either of 2 modes:
- simple type-writer mode of line/column text where you send escape sequences to manage a small number of fonts that are included in the printer hardware and have to manage line returns, etc.
- graphic output where the page is rasterized and the printer driver just drives the print head and pins to output the dots.
The first usage is mostly deprecated under Windows as it does not offer much in the way of controlling the output, and each printer having its own characteristics it becomes unwieldy and difficult for the software to predict and position things on the page (no WYSIWYG).
The second just uses a graphic page paradigm that makes positioning text and graphics independent of the actual capabilities of the printer.
When using pre-printed stationery, your job s to correctly position the data on the page.
Doing this by hand is resource-consuming and creating the layout in code is certainly not recommended since you'll get stuck with code to change should your printer, page format or printed stationery change.
The best is to just use the standard printing model offered by .Net and a reporting tool that allows you to define models and templates where the correct text and graphics will be positioned, and then drive this from code.
Visual Studio is shipped with a version of Crystal Reports but there are other, better reporting systems (I use the one from developer express for instance), some of them are even free.
回答2:
From my experience, it is easier to use two kinds of reports for the same data:
- one report for dot matrix printers using escape codes and anything else is required, which is saved in a text file and then printed using various methods (
type file.txt > lpt1
or selecting in code the default printer and usingNOTEPAD /P file.txt
) - see this page for more printing methods. - another report for laser/inkjet printers using a report builder tool (Crystal Reports, Report Manager, RLIB or anything available)
Since it is not uncommon to buy the right kind of printer for the right kind of report, this approach has the advantage of letting the customer decide: dot matrix printer for text reports in A3/A4 paper format (usually for the accounting department) or laser/inkjet printer for graphical reports.
回答3:
i don't know how to use Escape sequence in C#. But i have all Escape Sequence for Generic / Text Only printer. Hope it helps.
Generic Print Escape Sequence 1) Set Line Spacing a) 1/8 inch - 27,48 b) 1/6 inch - 27,50
2) Select Draft Quality a) 27,120,0 / 27,120,48
3) Letter Quality a) 27,120,1 / 27,120,49
4) Double Height a) 27,119,n i) n = 1 On ii) n = 0 Off
5) Bidirectional Printing a) 27,85,n i) 0 - Both Way ii) 1 - One Way
6) Increase character space a) 27,32,n (Increase by n / 12 inch)
7) Select Bold Font a) 27,69
8) Cancel Bold Font a) 27,70
9) Select Italic Font a) 27,52
10) Cancel Italic Font a) 27,53
11) Select a) 10cpi 27,8 b) 12cpi 27,77 c) 15cpi 27,103 d) 18cpi 27,103
12) Set Right Margin a) 27,81,n
13) Set Left Margin a) 27,108,n
14) Form Feed a) 12
15) Condensed Printing a) 0F On b) 12 Off
16) Double Strike Printing a) 27,71
17) Cancel Strike Printing a) 27,72
18) Under line a) 27,45,0 Off b) 27,45,1 On
19) Double Width a) 27,84,0 Off b) 27,84,1 ON
回答4:
First Convert the Sequence commands into character then pass to printer
Example Bold Font 27,69
string.Format("{0}{1}",Convert.ToChar(27),Convert.ToChar(69));
Perhaps a bit shorter as:
string.Format("{0}{1}",(char)(27),(char)(69));
回答5:
Take a look at the System.Drawing.Printing namespace.
回答6:
If your printer has driver to install, then you can use normal Windows print system. Most printers, including POS ones, have working Windows drivers available. (Most dot-matrix printers are Epson compatible anyway.) Some POS printer drivers allow send escape codes directly to printer too (using special fonts); probably you don't need such functionality.
If this is not the case, then you can add Generic/Text Only printer (driver) with help of Add Printer Wizard. Once done, you can configure certain commands (escape sequences) for it - Font size 10/12/17, Bold on/off, Underline on/off, job start/stop, paper feed and size select.
I'm using Generic printer associated with FILE port to test various reports for POS printers - it's easy to look at text file to validate numbers in printout. Of course for formatting specific printer driver is needed.
回答7:
It appears that what I would like to do is not possible.
My choices for printing are unmanaged direct to printer printing (using winspool.drv) which allows me to do whatever I like to the printer and allows me to easily line things up.VB.NET Example Or I can use GDI+ (System.Drawing.Printing) which is complicated to get things to line up but will work with non-dot matrix printers like XPS and PDF printers.
回答8:
You can refer this http://www.codeproject.com/Articles/29709/Line-Printer-Class-in-C hope helpd
来源:https://stackoverflow.com/questions/246233/dot-matrix-printing-in-c