POS Application Development - Receipt Printing

后端 未结 4 752
小蘑菇
小蘑菇 2020-12-12 23:45

I\'ve been building a POS application for a restaurant/bar.
The design part is done and for the past month I\'ve been coding it.
Everything works fine except now I

4条回答
  •  一向
    一向 (楼主)
    2020-12-13 00:20

    .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.

提交回复
热议问题