问题
How can I print to Zebra Printer TLP 3842 using EPL Programming? The printer doesn't support Zebra Printer Language (ZPL) and I am using PrintDocument(). I been on this problem for two weeks and can't figure it out. This what I have so far in my code, that actually runs the printer:
private System.ComponentModel.Container Components;
private System.Windows.Forms.Button PrintButton;
private Font PrintFont;
private StreamReader StreamToPrint;
private void PrintButton_Click(Object Sender, EventArgs e)
{
try
{
StreamToPrint = new StreamReader("C:\\Users\\jcabrera\\Desktop\\MyFile.txt");
//string ZPL_STRING = "^XA^LL440,^FO50,50^A0N,50,50^FDTesting Zebra Printer^FS^XZ";
// ZPL Command(s)
try
{
PrintFont = new Font("Arial", 10);
PrintDocument PD = new PrintDocument();
PD.PrintPage += new PrintPageEventHandler(this.PD_PrintPage);
PD.Print();
}
finally
{
StreamToPrint.Close();
}
} catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void PD_PrintPage(object Sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null;
// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
PrintFont.GetHeight(ev.Graphics);
// Print each line of the file.
while (count < linesPerPage &&
((line = StreamToPrint.ReadLine()) != null))
{
yPos = topMargin + (count *
PrintFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, PrintFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
}
// If more lines exist, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
private void InitializeComponent()
{
this.Components = new System.ComponentModel.Container();
this.PrintButton = new System.Windows.Forms.Button();
this.ClientSize = new System.Drawing.Size(504, 381);
this.Text = "Print Example";
PrintButton.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
PrintButton.Location = new System.Drawing.Point(32, 110);
PrintButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
PrintButton.TabIndex = 0;
PrintButton.Text = "Print the file.";
PrintButton.Size = new System.Drawing.Size(136, 40);
PrintButton.Click += new System.EventHandler(PrintButton_Click);
this.Controls.Add(PrintButton);
}
Using Windows Application project.
回答1:
You need to add RawPrinterHelperClass to your project, and then print like this
string ZPL_STRING = "^XA^LL440,^FO50,50^A0N,50,50^FDTesting Zebra Printer^FS^XZ";
RawPrinterHelper.SendStringToPrinter("PrinterName", ZPL_STRING)
C# Class https://github.com/andyyou/SendToPrinter/blob/master/Printer/RawPrinterHelper.cs
来源:https://stackoverflow.com/questions/51754966/print-to-zebra-printer-tlp-3842