Dot Matrix printing in C#?

29,865

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

Solution 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 using NOTEPAD /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.

Solution 3

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));

Solution 4

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

Solution 5

Take a look at the System.Drawing.Printing namespace.

Share:
29,865
Dale
Author by

Dale

Updated on July 20, 2022

Comments

  • Dale
    Dale almost 2 years

    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?

    • Jitesh Patil
      Jitesh Patil about 14 years
      Can you give us links to the examples to print to dotmatrix printers.
  • Dale
    Dale over 15 years
    I'm trying that... using the GDI+ printing stuff in C# (System.Drawing.Printing) but the problem is that its still not the same (as good) as sending the text direct to the printer... For example what font do I need to use?
  • Glenner003
    Glenner003 over 15 years
    I think the Text printer driver is your best option. You should also consider speed, printing in graphic mode on a matrix printer is a lot slower than using escape codes. The downside is incompatibility with graphic printers, but with preprinted forms, using another printer is not much of use.