Hi friends,
Now I've tried to print a receipt using a Dot Matrix Receipt printer. In the receipt each and every section should be printed in a various font sizes and two different colors. Can anyone help me on this.
///
/// Prints the cancel KOT orders.
///
/// <param name="connectionString" />The connection string.
/// <param name="orderDetailNumbers" />The order detail numbers.
/// <param name="userName" />Name of the user.
/// <returns>
/// Print Cancel KOT Orders
///
public override string PrintCancelKOTOrders(string connectionString, long[] orderDetailNumbers, string userName)
{
RawPrinterHelper receiptPrinter = new RawPrinterHelper();
DataTable reportTable = new DataTable();
ReceiptFunction receiptFunction = new ReceiptFunction();
reportTable = receiptFunction.GetCancelKOTNote(connectionString, orderDetailNumbers);
if (reportTable.Rows.Count > 0)
{
int length = 0;
int foodDept = -1;
string printerName = string.Empty;
string companyName = reportTable.Rows[0]["comName"].ToString();
int i = 0;
string displayString = string.Empty;
try
{
foreach (DataRow row in reportTable.Rows)
{
i += 1;
if (foodDept != (int)row["FoodDpId"])
{
displayString += "Order No : " + reportTable.Rows[0]["OrderId"].ToString().Trim() + "/" + row["FoodDept"].ToString().Trim() + Environment.NewLine;
displayString += "Order Date : " + DateTimeHelper.Now.Date.ToString("dd-MMM-yyyy") + " " + DateTimeHelper.Now.ToString("HH:mm:ss") + Environment.NewLine;
displayString += "Table(s) : " + reportTable.Rows[0]["tableIds"].ToString().Trim() + "/Res No: " + row["reservationId"].ToString().Trim() + Environment.NewLine;
displayString += "Order Canceled By: " + reportTable.Rows[0]["userName"].ToString().Trim() + Environment.NewLine;
////displayString += this.ColorSet(" CANCELLED ") + Environment.NewLine;
displayString += "C Item Quantity" + Environment.NewLine;
displayString += "----------------------------------------" + Environment.NewLine;
////////Console.ForegroundColor = System.ConsoleColor.Red;
////////Console.Write(displayString);
PrintDialog pd = new PrintDialog();
pd.PrinterSettings = new PrinterSettings();
pd.PrintToFile = true;
receiptPrinter.PrinterName = row["PrinterName"].ToString();
this.Print(receiptPrinter, displayString);
this.StyleTester();
displayString = string.Empty;
}
string foodName = row["Name"].ToString().Trim();
string foodQuantity = row["Quantity"].ToString().Trim();
string coverNumber = row["CoverNo"].ToString().Trim();
if (coverNumber.Equals("0"))
{
coverNumber = "TB";
}
else
{
coverNumber = "C" + coverNumber;
}
if (Convert.ToInt32(foodQuantity) == -1)
{
foodName = row["ItemName"].ToString().Trim();
}
length = (31 - foodName.Length);
if (length < 0)
{
length = 0;
}
if (foodName.Length <= 26)
{
if (row["Quantity"].ToString().Trim().Length > 1)
{
displayString += (coverNumber + " " + foodName + " ".PadLeft(length) + foodQuantity + Environment.NewLine);
}
else
{
displayString += (coverNumber + " " + foodName + " " + " ".PadLeft(length) + foodQuantity + Environment.NewLine);
}
}
else
{
int p = 0;
string strItemName = foodName;
int r = strItemName.Length % 26;
while (strItemName.Substring(p, p + 26).Length >= 26)
{
if (row["Quantity"].ToString().Trim().Length > 1)
{
displayString += coverNumber + (" " + strItemName.Substring(p, p + 26) + " " + " ".PadLeft(length) + row["Quantity"].ToString().Trim() + Environment.NewLine);
}
else
{
displayString += coverNumber + (" " + strItemName.Substring(p, p + 26) + " " + " ".PadLeft(length) + row["Quantity"].ToString().Trim() + Environment.NewLine);
}
strItemName = strItemName.Substring(p + 26, strItemName.Length - 26);
if (strItemName.Length < 26)
{
displayString += (" " + strItemName + Environment.NewLine);
break; // TODO: might not be correct. Was : Exit Do
}
}
}
foodDept = (int)row["FoodDpId"];
if ((reportTable.Rows.Count == i) || (row["FoodDpId"] != reportTable.Rows[i]["FoodDpId"]))
{
this.Print(receiptPrinter, displayString);
displayString = string.Empty;
}
}
if (reportTable.Rows[0]["Remarks"] != null)
{
displayString += Environment.NewLine;
displayString += "Reason : " + reportTable.Rows[0]["Remarks"].ToString().Trim() + Environment.NewLine;
}
displayString = this.CutPrinterPaper(displayString);
this.Print(receiptPrinter, displayString);
displayString = string.Empty;
return "Print success.";
}
catch (Exception ex)
{
return "Printer Error! Save or print this with another printer";
}
}
else
{
return "No records.";
}
}
/// <summary>
/// Prints the specified receipt printer.
/// </summary>
/// <param name="receiptPrinter">The receipt printer.</param>
/// <param name="printingText">The printing text.</param>
private void Print(RawPrinterHelper receiptPrinter, string printingText)
{
this.isPrintThreadAborted = false;
Thread printThread = new Thread(() => PrintOnThread(receiptPrinter, printingText));
Thread timeoutThread = new Thread(() => TimeoutThread(printThread, 20000));
printThread.Start();
timeoutThread.Start();
printThread.Join();
if (timeoutThread.IsAlive)
{
timeoutThread.Abort();
}
// check if the printing has timed out
if (this.isPrintThreadAborted)
{
throw new TimeoutException("Printing has timed out");
}
}
private void PrintOnThread(RawPrinterHelper receiptPrinter, string printingText)
{
if (!receiptPrinter.Open("Test Page"))
{
receiptPrinter.Open("Test Page");
}
receiptPrinter.Print(printingText);
////receiptPrinter.Print("ESC GS *2");
receiptPrinter.Close();
}
private void TimeoutThread(Thread threadToTimeout, int timeout)
{
Thread.Sleep(timeout);
if (threadToTimeout.IsAlive)
{
threadToTimeout.Abort();
this.isPrintThreadAborted = true;
}
}
/// <summary>
/// Colors the set.
/// </summary>
/// <param name="stringField">The string field.</param>
/// <returns>Color Set</returns>
private string ColorSet(string stringField)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(stringField);
return stringField;
}
I've tried the ColorSet method to change the color of the text. I it didn't work.
Can anyone help me on this?
Thanks in advance