DOS like printing in c# .net on dotmatrix printers


Dos like printing in c# .net on dotmatrix printers. Some time we have requirements for printing invoices . In such cases it is not good to use reports(eg. crystal reports) as bills .So the following is a method for dos like printing in c#.net

Here am using a clsPrintSettings class , which contains all print related methods
Bill is generated from the database and write it to text file

Code For Printing Class


public class clsPrintSettings
{
// Printing commands are depends on the Dotmatrix printer that we are using

System.IO.StreamWriter rdr;

private string Bold_On = "_G";
private string Bold_Off = "_H";
private string Width_On = "_W1"; //Chr(27) + Chr(87) + Chr(49) 'W1
private string Width_Off = "_W0";

//Public Const Compress_On = "¤" 'Chr(15) '¤"
//Public Const Compress_Off = "_" 'Chr(18) '_
private string ELITE_PITCH = "_M";
private string Compress_On = "_ð ; //Chr(15) '¤"
private string Compress_Off = "_";

private int ColWidth=60;
public string BillType;
public string BillNo;
public string BillDt;
public string Clerk;
public string ClientName="";
public decimal Discount;
public decimal TotalAmt;
public decimal NetAmount;
public decimal MRPTotal=0,SavedTotal=0;
public System.Data.DataTable dt;

public clsPrintSettings()
{
rdr=new System.IO.StreamWriter("bill.txt");
//rdr.AutoFlush();
}
public void Close()
{
rdr.Close();
}
public void PrintHeader()
{
//PrintLine();
rdr.WriteLine(Bold_On + "ABC SUPER MARKET, KERALA" + Bold_Off);
rdr.WriteLine("KGST : 23040117 DT 1/1/2001");
rdr.WriteLine("CST : 23040117 DT 1/1/2001");
PrintLine();
rdr.WriteLine(Bold_On + Width_On + BillType + Width_Off + Bold_Off );
PrintLine();
rdr.WriteLine("Customer : " +ClientName);
PrintLine();
}
public void PrintDetails()
{

rdr.WriteLine("SLNo |Name |Qty|MRP |Rate |T.Amt |Value ");
int i;
PrintLine();
for(i=0;i {
rdr.Write("{0,-4}",GetFormatedText(Convert.ToString(i+1),5)+ "|" );
rdr.Write("{0,-20}", GetFormatedText(dt.Rows[i].ItemArray[1].ToString(),21) + "|");
rdr.Write("{0,-2}",GetFormatedText(Convert.ToString(Math.Round(Convert.ToDecimal(dt.Rows[i].ItemArray[2].ToString()),0)),3)+ "|");
rdr.Write("{0,-6}",GetFormatedText(Convert.ToString(Math.Round(Convert.ToDecimal(dt.Rows[i].ItemArray[3].ToString()),2)),6)+ "|");
rdr.Write("{0,-6}",GetFormatedText(Convert.ToString(Math.Round(Convert.ToDecimal(dt.Rows[i].ItemArray[4].ToString()),2)),6)+ "|");
rdr.Write("{0,-6}",GetFormatedText(Convert.ToString(Math.Round(Convert.ToDecimal(dt.Rows[i].ItemArray[5].ToString()),2)),6)+ "|");
rdr.Write("{0,-6}",GetFormatedText(Convert.ToString(Math.Round(Convert.ToDecimal(dt.Rows[i].ItemArray[6].ToString()),2)),6)+ "");
rdr.WriteLine("");
MRPTotal= MRPTotal+ (Convert.ToDecimal(dt.Rows[i].ItemArray[2].ToString())*Convert.ToDecimal(dt.Rows[i].ItemArray[3].ToString()));

}
}
private string GetFormatedText(string Cont,int Length)
{
int rLoc=Length-Cont.Length;
if(rLoc<0)
{
Cont =Cont.Substring(0,Length);
}
else
{
int nos;
for(nos=0;nos Cont =Cont + " ";
}
return(Cont);
}
private string GetRightFormatedText(string Cont,int Length)
{
int rLoc=Length-Cont.Length;
if(rLoc<0)
{
Cont =Cont.Substring(0,Length);
}
else
{
int nos;
string space="";
for(nos=0;nos space += " ";
Cont = space + Cont;
}
return(Cont);
}
private string GetCenterdFormatedText(string Cont,int Length)
{
int rLoc=Length-Cont.Length;
if(rLoc<0)
{
Cont =Cont.Substring(0,Length);
}
else
{
int nos;
string space="";
for(nos=0;nos space += " ";
Cont = space + Cont;
}
return(Cont);
}
public void PrintFooter()
{
PrintLine();
rdr.WriteLine(" Total : " + Math.Round(TotalAmt,2).ToString() );
rdr.WriteLine(" Discount : " + Math.Round(Discount,2).ToString() );
rdr.WriteLine(" Net Amount : " + Math.Round(NetAmount,2).ToString() );
Miscellaneous.NumToString objMisc=new Miscellaneous.NumToString();
SavedTotal=MRPTotal-TotalAmt;
rdr.WriteLine(Compress_On + "You Have Saved Rs. " + Math.Round(SavedTotal,2).ToString() + Compress_Off);
PrintLine();
rdr.WriteLine("Clerk : " + Clerk);
rdr.WriteLine("Counter : " + clsSystem.MyCounter.ToString());
rdr.WriteLine();
rdr.WriteLine("Please check your items before leavining the counter");
rdr.WriteLine("Goods once sold will not be taken back");
// PrintLine();
}
public void PrintLine()
{
int i;
string Lstr="";
for(i=1;i {
Lstr=Lstr +"-";
}
rdr.WriteLine(Lstr);

}
public void SkipLine(int LineNos)
{
int i;
for(i=1;i {
rdr.WriteLine("");
}
}

public void ReverseSkip(int LineNos)
{
int i;
for(i=1;i {
rdr.WriteLine(Convert.ToChar(27) + "j" + Convert.ToChar(36 * LineNos));
}
}

public void PrintBuffer()
{
System.Diagnostics.Process.Start("bill.bat");
}
}




Call Print Class

private void PrintBill()
{
if(MessageBox.Show("Do You want to Print the Bill","Sales Bill",MessageBoxButtons.OKCancel)==DialogResult.OK)
{
clsPrintSettings obj=new clsPrintSettings();
obj.ReverseSkip(2);
obj.BillType = SaleType + " BILL";
obj.BillNo =txtSalesNo.Text;
obj.BillDt =dtpdate.Value.ToShortDateString();
obj.ClientName =txtClient.Text;
obj.Clerk =clsSystem.LoginStaff;
obj.TotalAmt = Convert.ToDecimal(txtTotAmt.Text );
obj.Discount = Convert.ToDecimal(txtDisc.Text );
obj.NetAmount =Convert.ToDecimal(txtNet.Text );
obj.dt =clsSalesItems.GetBillRecords(txtSalesNo.Text);
obj.PrintHeader();
obj.PrintDetails();
obj.PrintFooter();
obj.SkipLine(3);
obj.Close();
obj.PrintBuffer();
obj= null;
}
}



Create a batch File "Bill.bat"


type bill.txt>prn
exit


Comments

Guest Author: Arun13 Feb 2012

how to set line no. in detail section or specific size of print details like 14 line print in detail and balance print in next page with header or footer,if 2 line print in detail section or balance 12 line show blank,customize of paper size.the above code is right but some conclusion

Guest Author: ASAD22 Feb 2012

I have found it so use full ... but bold etc are not working ...
my client have Epson lq-300 .. kindly guide me
waiting for reply .

thanks

Guest Author: Manjurali01 Jul 2013

How to use this code in c#.net windows application

Author: Manjurali01 Jul 2013 Member Level: Bronze   Points : 0

how to use DOS like printing in c# .net on dotmatrix printers

Author: Dotnet Developer-201502 Jul 2013 Member Level: Gold   Points : 0

hi

how to change this numeric alignment for this


http://funwithcomputers-nishant.blogspot.in/2011/07/printing-in-c-on-dot-matrix-printer.html



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: