Here i am using
1) a clsPrintSettings class, which contains all print related methods which generate bill into text file
2) and a Print method which provides values of all variable to clsPrintSettings class and call required methods of clsPrintSettings class to generate bill into text file
1) 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<5;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<5;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<5;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<5;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 : " + "999");
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<=5;i++)
{
Lstr=Lstr +"-";
}
rdr.WriteLine(Lstr);
}
//----------------------------
public void SkipLine(int LineNos)
{
int i;
for(i=1;i<=5;i++)
{
rdr.WriteLine("");
}
}
//--------------------------
public void ReverseSkip(int LineNos)
{
int i;
for(i=1;i<=5;i++)
{
rdr.WriteLine(Convert.ToChar(27) + "j" + Convert.ToChar(36 * LineNos));
}
}
public void PrintBuffer()
{
//Create a batch File "Bill.bat"
//type bill.txt>prn
//exit
System.Diagnostics.Process.Start("bill.bat");
}
}
2) private void Form1_Load(object sender, EventArgs e)
{
PrintBill();
}
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 = "Restaurant" + " BILL";
obj.BillNo = "1000";
obj.BillDt = DateTime.Now.ToShortDateString();
obj.ClientName = "Mukul";
obj.Clerk = "Nishant";
obj.TotalAmt = Convert.ToDecimal("750");
obj.Discount = Convert.ToDecimal("0");
obj.NetAmount = Convert.ToDecimal("750");
DataTable dt1 = new DataTable();
dt1.Columns.Add(new DataColumn("SLNo", typeof(string)));
dt1.Columns.Add(new DataColumn("Name", typeof(string)));
dt1.Columns.Add(new DataColumn("Qty", typeof(string)));
dt1.Columns.Add(new DataColumn("MRP", typeof(string)));
dt1.Columns.Add(new DataColumn("Rate", typeof(string)));
dt1.Columns.Add(new DataColumn("T.Amt", typeof(string)));
dt1.Columns.Add(new DataColumn("Value", typeof(string)));
for (int i = 0; i < 5; i++)
{
DataRow newrow = dt1.NewRow();
newrow["SLNo"] = Convert.ToString(i + 1);
newrow["Name"] = "Easy";
newrow["Qty"] = "10";
newrow["MRP"] = "101";
newrow["Rate"] = "100";
newrow["T.Amt"] = "1000";
newrow["Value"] = "1000";
dt1.Rows.Add(newrow);
}
obj.dt = dt1;
obj.PrintHeader();
obj.PrintDetails();
obj.PrintFooter();
obj.SkipLine(3);
obj.Close();
//obj.PrintBuffer();
obj = null;
}
}
Help taken from this
link