0
votes

I am developing a Point of Sale application in C# Windows Form. Now i am on a stage where i have to print Receipt for the customer. for that purpose i am using Thermal printer. I draw the receipt on a panel image is attached. First i try to print it using bitmap but it become faade when i print it on thermal printer. That code is bellow

Bitmap bp = new Bitmap(Panel1.Width, Panel1.Height);
        Panel1.DrawToBitmap(bp, new Rectangle(0, 0, Panel1.Width, Panel1.Height));
        e.Graphics.DrawImage(bp, 0, 0);
        PageSetupDialog psd = new PageSetupDialog();
        psd.Document = printDocument1; 

now i want any other procedure where i can create a good looking receipt like this. Thanx in advance.enter image description here

1

1 Answers

0
votes

There are two ways of doing this the one is complex as well as non generic such as once hard coded its difficult to change without again building the project

First Way

In this way you have to override onDraw() function for printing so that you have to calculate the location and size of each string to be printed on receipt its very complex and very tricky and for each minor change you have to change all calculations again

Second Way

You have to create your receipt in HTML ,CSS very easy and efficient as well as Generic for future you will just change the HTML template and your app will adopt your new receipt without the need of building application again so what you need to do is

Create Html code leaving the value fields with some replaceable character combination i.e

<h3>Total Bill : %bill%</h3>

in your program you will read the file and replace %bill% with the actual value

after doing this procedure you will finally get an HTML code of receipt then create a WebBrowser instance like this

WebBrowser browser = new WebBrowser();
 browser.DocumentCompleted +=new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
 browser.DocumentText = "your generated html code";

after this create an event of WebBrowser_DocumentCompleted like that

public void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
  WebBrowser wb = (WebBrowser)sender;
  //set here your thermal printer by default if it is not already
  wb.Print(); 
}

You are done!