C# printing in dos mode

Mar 23 2009 3:17 AM
Dear all

Its been some time, i have been trying to print bills in dos mode using C# , i was unsuccessful so i have given my client printing in normal mode using crystal reports. I am using VS 2005 & SQL 2000. But i would like to give a try again printing bills in dos mode.

I am using continueous paper with 6 Inches height and 5 inches width on wep 800 dx printer. i would like to align text in the following way. i would like to know how to eject paper after printing each bill so that it wont print next bill in the balance of the first page.



BILL FORMAT

               COMPANY NAME
              COMPANY ADDRESS
              COMPANY ADDRESS2
                  PINCODE
                 TELEPHONE
--------------------------------------------
BILL NO : 123        BILL DATE:DD/MM/YYYY
CUSTOMER NAME : BILL GATES
CUSTOMER ADDRESS : MICROSOFT
--------------------------------------------
ITEM NO1                             1000.00
ITEM NO2                  530.00
ITEM NO3                              450.00
ITEM NO4                                1.50
ITEM NO6                             2500.00



--------------------------------------------
                             TOTAL : 4481.50 
--------------------------------------------
          THANK YOU VISIT AGAIN
--------------------------------------------


               COMPANY NAME
              COMPANY ADDRESS
              COMPANY ADDRESS2
                  PINCODE
                 TELEPHONE
--------------------------------------------
BILL NO : 124        BILL DATE:DD/MM/YYYY
CUSTOMER NAME : AB DEVILLERS
CUSTOMER ADDRESS : NEW YORK
--------------------------------------------
ITEM NO1                               25.00
ITEM NO2                  120.00
ITEM NO3                              369.00
ITEM NO4                                0.50
ITEM NO6                             1450.00



--------------------------------------------
                             TOTAL : 1964.50 
--------------------------------------------
          THANK YOU VISIT AGAIN
--------------------------------------------

I have used the below code provided by J O'Donnell, this code is pretty good its printing very fast but its printing only junk characters & printer gets stuck in between. I have sorted a little we must use only monotype fonts to make printing fast + we have to make printer mode to Generic / Text only. I would like to get guidance on how to send the printing values directly to printer and how to align it properly, so that when you look at the print out it must look nice.


using System;
using System.Text;
using System.Runtime.InteropServices;


[StructLayout( LayoutKind.Sequential)]
public struct DOCINFO
{
        [MarshalAs(UnmanagedType.LPWStr)]public string pDocName;
        [MarshalAs(UnmanagedType.LPWStr)]public string pOutputFile;
        [MarshalAs(UnmanagedType.LPWStr)]public string pDataType;
}


public class PrintDirect
{
        [ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false,
        CallingConvention=CallingConvention.StdCall )]
        public static extern long OpenPrinter(string pPrinterName,ref IntPtr phPrinter, int pDefault);
   
        [ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false,
        CallingConvention=CallingConvention.StdCall )]
        public static extern long StartDocPrinter(IntPtr hPrinter, int Level, ref DOCINFO pDocInfo);
       
        [ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true,
        CallingConvention=CallingConvention.StdCall)]
        public static extern long StartPagePrinter(IntPtr hPrinter);
       
        [ DllImport( "winspool.drv",CharSet=CharSet.Ansi,ExactSpelling=true,
        CallingConvention=CallingConvention.StdCall)]
        public static extern long WritePrinter(IntPtr hPrinter,string data, int buf,ref int pcWritten);
       
        [ DllImport( "winspool.drv" ,CharSet=CharSet.Unicode,ExactSpelling=true,
        CallingConvention=CallingConvention.StdCall)]
        public static extern long EndPagePrinter(IntPtr hPrinter);   
       
        [ DllImport( "winspool.drv" ,CharSet=CharSet.Unicode,ExactSpelling=true,
        CallingConvention=CallingConvention.StdCall)]
        public static extern long EndDocPrinter(IntPtr hPrinter);   
       
        [ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true,   
        CallingConvention=CallingConvention.StdCall )]
        public static extern long ClosePrinter(IntPtr hPrinter);
}


public class App
{
        public static void Main()
        {           
                System.IntPtr lhPrinter=new System.IntPtr();
               
                DOCINFO di = new DOCINFO();
                int pcWritten=0;
                string st1;

                               
                // text to print with a form feed character   
                st1="This is an example of printing directly to a printer\f";
                di.pDocName="my test document";
                di.pDataType="RAW";
               
                // the \x1b means an ascii escape character
                st1="\x1b*c600a6b0P\f";
                //lhPrinter contains the handle for the printer opened
                //If lhPrinter is 0 then an error has occured
                PrintDirect.OpenPrinter("WeP 800 DX",ref lhPrinter,0);               
                PrintDirect.StartDocPrinter(lhPrinter,1,ref di);
                PrintDirect.StartPagePrinter(lhPrinter);   
       
                try
                {
                        // Moves the cursor 900 dots (3 inches at 300 dpi) in from the left margin, and
            // 600 dots (2 inches at 300 dpi) down from the top margin.
                        st1="\x1b*p900x600Y";
                        PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten);       

                        // Using the print model commands for rectangle dimensions, "600a" specifies a rectangle
            // with a horizontal size or width of 600 dots, and "6b" specifies a vertical
            // size or height of 6 dots. The 0P selects the solid black rectangular area fill.
                        st1="\x1b*c600a6b0P";
                        PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten);       

                        // Specifies a rectangle with width of 6 dots, height of 600 dots, and a
                        // fill pattern of solid black.
                        st1="\x1b*c6a600b0P";
                        PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten);       

                        // Moves the current cursor position to 900 dots, from the left margin and
            // 1200 dots down from the top margin.
                        st1="\x1b*p900x1200Y";
                        PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten);                   
                       
                        // Specifies a rectangle with a width of 606 dots, a height of 6 dots and a
            // fill pattern of solid black.
                        st1="\x1b*c606a6b0P";
                        PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten);                       

                        // Moves the current cursor position to 1500 dots from the left margin and
            // 600 dots down from the top margin.
                        st1="\x1b*p1500x600Y";
                        PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten);                   

                        // Specifies a rectangle with a width of 6 dots, a height of 600 dots and a
            // fill pattern of solid black.
                        st1="\x1b*c6a600b0P";
                        PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten);                   

                        // Send a form feed character to the printer
                        st1="\f";
                        PrintDirect.WritePrinter(lhPrinter,st1,st1.Length,ref pcWritten);               
                }
                catch (Exception e)
                {
                        Console.WriteLine(e.Message);
                }
               
                PrintDirect.EndPagePrinter(lhPrinter);
                PrintDirect.EndDocPrinter(lhPrinter);
                PrintDirect.ClosePrinter(lhPrinter);
        }
}

Answers (1)