POS Application Development - Receipt Printing POS Application Development - Receipt Printing wpf wpf

POS Application Development - Receipt Printing


I know this is an old post, but for those still looking for a solution, I can tell you what I did.

After spending many hours messing with OPOS and POS for .Net, I ended up just abandoning those and just using the built-in System.Drawing.Printing libraries. The OPOS and POS for .Net ended up being a pain to get working and ultimately didn't work as well as the built-in libraries.

I'm using an Epson TM-T20II receipt printer.

Here's some code that worked well for me.

public static void PrintReceiptForTransaction(){    PrintDocument recordDoc = new PrintDocument();    recordDoc.DocumentName = "Customer Receipt";    recordDoc.PrintPage += new PrintPageEventHandler(ReceiptPrinter.PrintReceiptPage); // function below    recordDoc.PrintController = new StandardPrintController(); // hides status dialog popup                                                                // Comment if debugging     PrinterSettings ps = new PrinterSettings();    ps.PrinterName = "EPSON TM-T20II Receipt";    recordDoc.PrinterSettings = ps;    recordDoc.Print();    // --------------------------------------    // Uncomment if debugging - shows dialog instead    //PrintPreviewDialog printPrvDlg = new PrintPreviewDialog();    //printPrvDlg.Document = recordDoc;    //printPrvDlg.Width = 1200;    //printPrvDlg.Height = 800;    //printPrvDlg.ShowDialog();    // --------------------------------------    recordDoc.Dispose();}private static void PrintReceiptPage(object sender, PrintPageEventArgs e){    float x = 10;    float y = 5;    float width = 270.0F; // max width I found through trial and error    float height = 0F;    Font drawFontArial12Bold = new Font("Arial", 12, FontStyle.Bold);    Font drawFontArial10Regular = new Font("Arial", 10, FontStyle.Regular);    SolidBrush drawBrush = new SolidBrush(Color.Black);    // Set format of string.    StringFormat drawFormatCenter = new StringFormat();    drawFormatCenter.Alignment = StringAlignment.Center;    StringFormat drawFormatLeft = new StringFormat();    drawFormatLeft.Alignment = StringAlignment.Near;    StringFormat drawFormatRight = new StringFormat();    drawFormatRight.Alignment = StringAlignment.Far;    // Draw string to screen.    string text = "Company Name";    e.Graphics.DrawString(text, drawFontArial12Bold, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);    y += e.Graphics.MeasureString(text, drawFontArial12Bold).Height;    text = "Address";    e.Graphics.DrawString(text, drawFontArial10Regular, drawBrush, new RectangleF(x, y, width, height), drawFormatCenter);    y += e.Graphics.MeasureString(text, drawFontArial10Regular).Height;    // ... and so on}

Hopefully it helps someone skip all the messing around with custom drivers. :)


POS for .NET is probably the way to go.

Most receipt printer manufacturers will provide an OPOS service object.

And as this MSDN article states, POS for .NET is compatible with OPOS v1.8 service objects.

OPOS / UPOS (on which POS for .NET is based) is IMHO a poorly-designed API (designed by device manufacturers rather than application developers), but it's the best you have today.

I don't have any specific samples but the basics are the same as OPOS - you need to Open, Claim, Enable a device, then you can call its methods (such as Print). You might try looking at an OPOS sample, for example this PosPrinter1 sample, which will probably be very similar to POS for .NET.

This blog has some information about setting up POS for .NET that might be helpful.

UPDATE

Here's a VB Hello World for an OPOS printer. You first need to create a printer and add it to the registry with the required Logical Device Name = LDN. I believe the Epson ADK includes a utility to add a printer in the registry. This utility can also perform a health check on the printer to check it is installed correctly. Once you've done this, it should be easy enough to adapt the code below to POS for .NET

OPOSPOSPrinter.Open "MyPrinter"    ' LDN of your printer   OPOSPOSPrinter.Claim 500           ' Timeout   OPOSPOSPrinter.DeviceEnabled = True  '- Print   OPOSPOSPrinter.PrintNormal 2, "Hello world"  '- Close the printer   If OPOSPOSPrinter.Claimed then      OPOSPOSPrinter.Release   End If  OPOSPOSPrinter.Close  


.NET Printing

Printing under .NET isn't too difficult. Take a look here and on msdn.

Printing to a POS / receipt printer will be the same as printing to any other printer, assuming it is a Windows printer, network or otherwise. If you are using a serial printer, things may be a little more difficult because you will more then likely need to use manufacturer specific API's, fortunately though most good POS printers these days are fully supported by the OS.

First, you will need to add a reference to System.Printing dll to your project.

Then printing is as simple as

private void PrintText(string text){    var printDlg = new PrintDialog();    var doc = new FlowDocument(new Paragraph(new Run(text)));    doc.PagePadding = new Thickness(10);    printDlg.PrintDocument((doc as IDocumentPaginatorSource).DocumentPaginator, "Print Caption");}

To use..

PrintText("Hello World");

You can also leverage the PrintDialog.PrintVisual and define your document using xaml template.

The print settings can be set using the PrintDialog properties.

Getting the printer you want to print to

private PrintQueue FindPrinter(string printerName){    var printers = new PrintServer().GetPrintQueues();    foreach (var printer in printers)    {        if (printer.FullName == printerName)        {            return printer;        }    }    return LocalPrintServer.GetDefaultPrintQueue();}

A few things to keep in mind though when printing to a receipt printer, you will need to take into account formatting. More specifically you will need to consider the width of your page and how many characters you can fit on each line; this was a lot of trial and error for me, especially with different font sizes.

In most cases you don't really need to worry about paging, the printer will cut the paper automatically when it has completed your document.