Printing text file on thermal printer with Printdocument?

Asked

Viewed 11,041 times

4

I am developing a system in Winforms with C# and now I need to generate a sales receipt coupon and print on a thermal printer. I’ve been reading about Printdocument but can’t find examples of how to generate coupon using this print API. I don’t know what the print setting is for reels like paper size, centering text, etc. I’m looking for some example of how to generate this. How to do this ?

I’m trying like this.

private PrintDocument printDocument = new PrintDocument();
private static String COMPROVANTE = Environment.CurrentDirectory + @"\comprovantes\comprovante.txt";
private String stringToPrint = "";

private void button1_Click(object sender, EventArgs e) {
            geraComprovante();
            imprimeComprovante();
        }

private void geraComprovante() {            
            FileStream fs = new FileStream(COMPROVANTE, FileMode.Create);
            StreamWriter writer = new StreamWriter(fs);
            writer.WriteLine("==========================================");
            writer.WriteLine("          NOME DA EMPRESA AQUI            ");
            writer.WriteLine("==========================================");
            writer.Close();
            fs.Close();
        }

        private void imprimeComprovante() {            
            FileStream fs = new FileStream(COMPROVANTE, FileMode.Open);
            StreamReader sr = new StreamReader(fs);
            stringToPrint = sr.ReadToEnd();
            printDocument.PrinterSettings.PrinterName = DefaultPrinter.GetDefaultPrinterName();            
            printDocument.PrintPage += new PrintPageEventHandler(printPage);
            printDocument.Print();
            sr.Close();
            fs.Close();
        }

        private void printPage(object sender, PrintPageEventArgs e) {
            int charactersOnPage = 0;
            int linesPerPage = 0;
            Graphics graphics = e.Graphics;

            // Sets the value of charactersOnPage to the number of characters 
            // of stringToPrint that will fit within the bounds of the page.
            graphics.MeasureString(stringToPrint, this.Font,
                e.MarginBounds.Size, StringFormat.GenericTypographic,
                out charactersOnPage, out linesPerPage);

            // Draws the string within the bounds of the page
            graphics.DrawString(stringToPrint, this.Font, Brushes.Black,
                e.MarginBounds, StringFormat.GenericTypographic);

            // Remove the portion of the string that has been printed.
            stringToPrint = stringToPrint.Substring(charactersOnPage);

            // Check to see if more pages are to be printed.
            e.HasMorePages = (stringToPrint.Length > 0);
        }
  • I don’t have the complete answer, but a hint: you can use the "Stringformat" in the last parameter of "Graphics.Drawstring" to center the text through the Alignment and Linealigment properties.

3 answers

4


Solved. I created a class that extends PrintDocument and created the voucher as needed. I didn’t even need to generate text file, I did it using the Graphics of PrintDocument msm. It works 100%.

did so.

using System.Drawing.Printing;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using TerminalControleDeVendas.domain;
using TerminalControleDeVendas.dao;


namespace TerminalControleDeVendas.utils {


    public class ImprimeVendaVista : PrintDocument{
        private Empresa empresa = new EmpresaDAO().getEmpresa();
        private Venda venda;        
        private Font bold = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold);
        private Font regular = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular);
        private Font regularItens = new Font(FontFamily.GenericSansSerif, 6,FontStyle.Regular);

        public ImprimeVendaVista(Venda venda){
            this.venda = venda;
            this.PrinterSettings.PrinterName = DefaultPrinter.GetDefaultPrinterName();
            this.OriginAtMargins = false;
            this.PrintPage += new PrintPageEventHandler(printPage);
        }

        private void printPage(object send, PrintPageEventArgs e) {
            Graphics graphics = e.Graphics;
            int offset = 105;

            //print header
            graphics.DrawString(empresa.razaoSocial, bold, Brushes.Black, 20, 0);            
            graphics.DrawString(empresa.endereco.endereco + " Nº " + empresa.endereco.numero, regular, Brushes.Black, 100, 15);
            graphics.DrawLine(Pens.Black, 20, 30, 310, 30);
            graphics.DrawString("CUPOM NÃO FISCAL", bold, Brushes.Black, 80, 35);
            graphics.DrawLine(Pens.Black, 20, 50, 310, 50);
            graphics.DrawString("PEDIDO: " + FormatId.formatLong(venda.id), regular, Brushes.Black, 20, 60);            
            graphics.DrawLine(Pens.Black, 20, 75, 310, 75);

            //itens header
            graphics.DrawString("PRODUTO", regular, Brushes.Black, 20, 80);
            graphics.DrawString("UNIT.", regular, Brushes.Black, 150, 80);
            graphics.DrawString("QTD.", regular, Brushes.Black, 200, 80);
            graphics.DrawString("TOTAL", regular, Brushes.Black, 245, 80);
            graphics.DrawLine(Pens.Black, 20, 95, 310, 95);

            //itens de venda
            foreach(ItemVenda iv in venda.items){
                string produto = iv.produto.descricao;                
                graphics.DrawString(produto.Length > 20 ? produto.Substring(0,20) + "..." : produto, regularItens, Brushes.Black, 20, offset);
                graphics.DrawString(FormataMonetario.format(iv.valorUn), regularItens, Brushes.Black, 155, offset);
                graphics.DrawString(Convert.ToString(iv.quantidade), regularItens, Brushes.Black, 215, offset);
                graphics.DrawString(FormataMonetario.format(iv.total), regularItens, Brushes.Black, 250, offset);
                offset += 20;
            }
            //total
            graphics.DrawLine(Pens.Black, 20, offset, 310, offset);
            offset += 5;

            decimal total = 0;
            foreach (ItemVenda iv in venda.items) {
                total += iv.total;
            }
            graphics.DrawString("TOTAL R$: ", bold, Brushes.Black, 20, offset);
            graphics.DrawString(FormataMonetario.format(total), bold, Brushes.Black, 230, offset);
            offset += 15;

            graphics.DrawLine(Pens.Black, 20, offset, 310, offset);
            offset += 5;

            //bottom
            graphics.DrawString("Data: " + DateTime.Now.ToString("dd/MM/yyyy"), regularItens, Brushes.Black, 20, offset);
            graphics.DrawString("HORA: " + DateTime.Now.ToString("HH:mm:ss"), regularItens, Brushes.Black, 220, offset);

            e.HasMorePages = false;

        }


    }
}

1

Friend, if I understand correctly, you want to print in a non fiscal printer right? Well, for this you have to integrate the respective model of the thermal printer that will be used, for example, if I use a Daruma printer, the manufacturer’s own website provides a dll for developers, in the dll itself already has all the functions of printing, connection to the equipment and etc. Try to take a look at...

  • No tax printer, it’s just thermal. Thank you

  • Fair! That printer I mentioned is nonfiscal, follows an image and tells me if it looks like this: https://http2.mlstatic.com/S_152221-MLB20746415003_052016-Y.jpg

-1

I define this.PrinterSettings.Printername = Printersettings.Printername; ;

Because it already extends Printdocument from the Base class.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.