Printing text file on thermal printer with PrintDocument?

3

I'm developing a system in WinForms with C # and now I need to generate a sales receipt coupon and print to a thermal printer. I've been reading about PrintDocument but I can not find examples of how to generate coupon using this print API. I do not know what the print setting is for coils such as paper size, centering the 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);
        }
    
asked by anonymous 25.09.2016 / 23:05

3 answers

1

Solved. I created a class that extends PrintDocument and created the proof as needed. I did not even need to generate a text file, I just did using Graphics of PrintDocument msm. It works 100%.

I 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;

        }


    }
}
    
02.10.2016 / 08:13
1

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

    
26.09.2016 / 20:41
0

Good morning,

I'm a beginner. I'd like some help. If it were possible.

The code snippet below:

this.PrinterSettings.PrinterName = DefaultPrinter.GetDefaultPrinterName();

The DefaulPrinter part is not recognized by the Vs. Where did you define it?

Thank you.

    
10.11.2017 / 13:06