Printing in Dot Matrix Printer with WPF C #

1

How do I print my tickets to a dot matrix printer (LX300) from routines made in WPF C #? Do you have anything specific for that? A drive or plugin?

    
asked by anonymous 11.08.2017 / 13:59

1 answer

1

Option 1 = Mount a text file, and send it to the printer, copying the file to it. Ex:

in C #:

File.Copy("arquivo.txt","\servidor\lx300");

or

File.Copy("arquivo.txt","lpt1");

Option 2 = Use a report generator, crystal, report viewer, and more, and send it to the printer normally, through the windows spooler.

Option 3 = Generate PrintDocument and send to printer, PrintDialog . Here is commented code:

//Fila de linhas que devem ser impressas
Queue<string> filaLinhas = new Queue<string>();


PrintDocument p = new PrintDocument();
//Evento PrintPage do PrintDocument
p.PrintPage += delegate(object sender1, PrintPageEventArgs ev)
{
    //Define a fonte utilizada para impressão
    Font printFont = new Font("Consolas", 11);
    float linesPerPage = 0;
    float yPos = 0;
    int count = 0;
    float leftMargin = ev.MarginBounds.Left;
    float topMargin = ev.MarginBounds.Top;
    string line = null;

    //Calcular o número de linhas por página
    linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);

    //Imprime cada linha da página
    while (count < linesPerPage && filaLinhas.Count >0 )
    {
        line = filaLinhas.Dequeue();
        yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
        ev.Graphics.DrawString(line, printFont, Brushes.Black, 0, yPos, new StringFormat());
        count++;
    }

    //Se existir mais linhas, gera outra página
    if (line != null && filaLinhas.Count >0)
        ev.HasMorePages = true;
    else
        ev.HasMorePages = false;
};

//Exibe o dialogo de impressão (se não for necessário, só pular o ShowDialog e chamar o .Print(); (Lembre-se de definir a impressora, ou será utilizada a padrão do windows)
PrintDialog diag = new PrintDialog();
diag.Document = p;
diag.PrinterSettings.PrinterName = "LX 300";
if (diag.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    p.Print();
}
    
11.08.2017 / 14:16