I'm currently writing a library that gets strings, some arguments and outputs them on a POS thermal USB printer.
I'm not sure I'm approaching this the right way, and I'd like someone else to take a look at what I'm doing:
Fonte is a class I defined:
An example of Fonte:
Basically this code gets each string received by RecebePrint and draws it. The 1000 value for the RectangleF is arbitrary, and empirically doesn't matter, but it bothers me not knowing why it doesn't matter.
Once again, an arbitrary value for the page height, which doesn't seem to affect the final result. I don't know how to set a "paper size" since POS thermal paper come in 4cm by 80 metre rolls, and I don't think it would be wise to set paper height as 80 metres.
The actual app goes like
Since I'm a beginner on C# I'd love someone more experienced to take a look at this code. Later on, I'll be also printing barcodes and QR codes, but that's another subject, for further discussion.
I'm not sure I'm doing this the right way...
Also, there's a requirement I need to fulfill in which the library must print individual lines before all lines are fed. If I feed all lines at once during runtime:
I get what I need:
If I use:
I get:
I'm not sure I'm approaching this the right way, and I'd like someone else to take a look at what I'm doing:
C#:
public static void RecebePrint(string texto, Fonte font, StringFormat alignment, bool breakline)
{
Linha line = new Linha()
{
linha = texto,
fonte = font.tipo,
alinhamento = alignment,
quebralinha = breakline
};
transmitir.Add(line);
}
Fonte is a class I defined:
C#:
public class Fonte
{
public Font tipo;
}
An example of Fonte:
C#:
public static Fonte corpo = new Fonte { tipo = new Font("Arial Narrow", 8f, FontStyle.Regular) };
C#:
public static void NewLine(object sender, PrintPageEventArgs ev) {
SizeF size = new SizeF();
float currentUsedHeight = 0f;
foreach (Linha line in transmitir)
{
if (line.linha == "**END OF RECEIPT**")
{
ev.Graphics.DrawString("***", line.fonte, Brushes.Black, new RectangleF(0, currentUsedHeight+300, 1000, 1000), line.alinhamento);
break;
}
else
{
ev.Graphics.DrawString(line.linha, line.fonte, Brushes.Black, new RectangleF(0, currentUsedHeight, 290, 1000), line.alinhamento);
size = ev.Graphics.MeasureString(line.linha, line.fonte);
}
if (line.quebralinha is true)
{
if (ev.Graphics.MeasureString(line.linha, line.fonte).Width > 290)
{
currentUsedHeight += size.Height * 2;
}
else
{
currentUsedHeight += size.Height;
}
}
else if (line.quebralinha is false)
{
}
}
}
Basically this code gets each string received by RecebePrint and draws it. The 1000 value for the RectangleF is arbitrary, and empirically doesn't matter, but it bothers me not knowing why it doesn't matter.
C#:
public static void Printa() {
PrintDocument printDoc = new PrintDocument();
printDoc.PrinterSettings.PrinterName = "Daruma DR800";
printDoc.DefaultPageSettings.PaperSize = new PaperSize("Bobina", 400, 10000);
printDoc.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
printDoc.DocumentName = "Cupom";
if (!printDoc.PrinterSettings.IsValid)
throw new Exception("Não foi possível localizar a impressora");
printDoc.PrintPage += new PrintPageEventHandler(PrinterNS.PrintFunc.NewLine);
printDoc.Print();
LimpaSpooler();
}
Once again, an arbitrary value for the page height, which doesn't seem to affect the final result. I don't know how to set a "paper size" since POS thermal paper come in 4cm by 80 metre rolls, and I don't think it would be wise to set paper height as 80 metres.
The actual app goes like
C#:
PrintFunc.RecebePrint("CPF/CNPJ do Consumidor: CONSUMIDOR NÃO IDENTIFICADO", corpo, esquerda.align, true); //For each line, and when all lines are stored,
PrintFunc.Printa();
Since I'm a beginner on C# I'd love someone more experienced to take a look at this code. Later on, I'll be also printing barcodes and QR codes, but that's another subject, for further discussion.
I'm not sure I'm doing this the right way...
Also, there's a requirement I need to fulfill in which the library must print individual lines before all lines are fed. If I feed all lines at once during runtime:
C#:
PrintFunc.RecebePrint("CASA FARACO COMERCIAL DE GENEROS ALIMENTICIOS LTD", corpo, centro.align, true); PrintFunc.RecebePrint("RUA FREI VITAL DE FRESCAROLO 514 JARDIM JOAO XXIII-SAO PAULO-055696-030", corpo, centro.align, true);
PrintFunc.RecebePrint("CNPJ: 10.922.257/0001-54 IE: 148657180110 IM:", corpo, centro.align, true);
PrintFunc.RecebePrint(new string('-', 91), negrito, esquerda.align, true);
PrintFunc.RecebePrint("Extrato No. 021487", Titulo, centro.align, true);
PrintFunc.RecebePrint("CUPOM FISCAL ELETRÔNICO - SAT", Titulo, centro.align, true);
PrintFunc.RecebePrint(new string('-', 55), negrito, esquerda.align, true);
PrintFunc.RecebePrint("CPF/CNPJ do Consumidor: CONSUMIDOR NÃO IDENTIFICADO", corpo, esquerda.align, true);
PrintFunc.RecebePrint(new string('-', 55), corpo, esquerda.align, true);
PrintFunc.RecebePrint("001 0000004022 Pipoca Coberta Com C 1 UN X 3,950 (0,81) 3,95", corpo, esquerda.align, true);
PrintFunc.RecebePrint("002 0000001389 Kinder Chocolate-12, 1 UN X 1,000 (0,36) 1,00", corpo, esquerda.align, true);
PrintFunc.RecebePrint("003 0000002390 Confeitos Fini-Todos 1 UN X 4,750 (0,53) 4,75", corpo, esquerda.align, true);
PrintFunc.RecebePrint("VALOR TOTAL R$", Titulo, esquerda.align, false);
PrintFunc.RecebePrint("9,70", Titulo, direita.align, true);
PrintFunc.RecebePrint("Cartão de débito", corpo, esquerda.align, false);
PrintFunc.RecebePrint("9,70", corpo, direita.align, true);
PrintFunc.RecebePrint(" ", corpo, esquerda.align, true);
PrintFunc.RecebePrint("02.03.03.02 - Consulte o QRCode deste extrato atra´ves do App DeOlhoNaNota", corpo, esquerda.align, true);
PrintFunc.RecebePrint(new string('-', 55), corpo, esquerda.align, true);
PrintFunc.RecebePrint("OBSERVAÇÕES DO CONTRIBUINTE", Titulo, esquerda.align, true);
PrintFunc.RecebePrint("13/07/17 15:51 LJ0001 OP000004 CX003 SQ401499", corpo, esquerda.align, true);
PrintFunc.RecebePrint("OBRIGADO VOLTE SEMPRE!!", corpo, esquerda.align, true);
PrintFunc.RecebePrint("**FIM DA NOTA**", corpo, esquerda.align, true);
PrintFunc.Printa();
I get what I need:
If I use:
C#:
PrintFunc.RecebePrint("CASA FARACO COMERCIAL DE GENEROS ALIMENTICIOS LTD", corpo, centro.align, true);
PrintFunc.RecebePrint("RUA FREI VITAL DE FRESCAROLO 514 JARDIM JOAO XXIII-SAO PAULO-055696-030", corpo, centro.align, true);
PrintFunc.RecebePrint("CNPJ: 10.922.257/0001-54 IE: 148657180110 IM:", corpo, centro.align, true);
PrintFunc.RecebePrint(new string('-', 91), negrito, esquerda.align, true);
PrintFunc.RecebePrint("Extrato No. 021487", Titulo, centro.align, true);
PrintFunc.RecebePrint("CUPOM FISCAL ELETRÔNICO - SAT", Titulo, centro.align, true);
PrintFunc.RecebePrint(new string('-', 55), negrito, esquerda.align, true);
PrintFunc.RecebePrint("CPF/CNPJ do Consumidor: CONSUMIDOR NÃO IDENTIFICADO", corpo, esquerda.align, true);
PrintFunc.RecebePrint(new string('-', 55), corpo, esquerda.align, true);
PrintFunc.Printa();
PrintFunc.RecebePrint("001 0000004022 Pipoca Coberta Com C 1 UN X 3,950 (0,81) 3,95", corpo, esquerda.align, true);
PrintFunc.Printa();
PrintFunc.RecebePrint("002 0000001389 Kinder Chocolate-12, 1 UN X 1,000 (0,36) 1,00", corpo, esquerda.align, true);
PrintFunc.Printa();
PrintFunc.RecebePrint("003 0000002390 Confeitos Fini-Todos 1 UN X 4,750 (0,53) 4,75", corpo, esquerda.align, true);
PrintFunc.Printa();
PrintFunc.RecebePrint("VALOR TOTAL R$", Titulo, esquerda.align, false);
PrintFunc.RecebePrint("9,70", Titulo, direita.align, true);
PrintFunc.RecebePrint("Cartão de débito", corpo, esquerda.align, false);
PrintFunc.RecebePrint("9,70", corpo, direita.align, true);
PrintFunc.RecebePrint(" ", corpo, esquerda.align, true);
PrintFunc.RecebePrint("02.03.03.02 - Consulte o QRCode deste extrato atra´ves do App DeOlhoNaNota", corpo, esquerda.align, true);
PrintFunc.RecebePrint(new string('-', 55), corpo, esquerda.align, true);
PrintFunc.RecebePrint("OBSERVAÇÕES DO CONTRIBUINTE", Titulo, esquerda.align, true);
PrintFunc.RecebePrint("13/07/17 15:51 LJ0001 OP000004 CX003 SQ401499", corpo, esquerda.align, true);
PrintFunc.RecebePrint("OBRIGADO VOLTE SEMPRE!!", corpo, esquerda.align, true);
PrintFunc.RecebePrint("**FIM DA NOTA**", corpo, esquerda.align, true);
PrintFunc.Printa();
I get: