我正在使用PrintDocument类来打印到我的Brother标签打印机。当我执行Print()方法时,打印机开始闪烁红色错误灯,但其他所有操作均返回成功。
我可以在激光打印机上运行相同的代码,并且一切正常。
我如何查看导致标签打印机错误的原因?
码:
public class Test
{
private Font printFont;
private List<string> _documentLinesToPrint = new List<string>();
public void Run()
{
_documentLinesToPrint.Add("Test1");
_documentLinesToPrint.Add("Test2");
printFont = new Font("Arial", 10);
var pd = new PrintDocument();
pd.DefaultPageSettings.Margins = new Margins(25, 25, 25, 25);
pd.DefaultPageSettings.PaperSize = new PaperSize("Label", 400, 237);
var printerSettings = new System.Drawing.Printing.PrinterSettings();
printerSettings.PrinterName ="Brother QL-570 LE";
pd.PrinterSettings = printerSettings;
pd.PrinterSettings.Copies = 1;
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.Print();
}
// The PrintPage event is raised for each page to be printed.
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) && (count < _documentLinesToPrint.Count))
{
line = _documentLinesToPrint[count];
yPos = topMargin + (count *
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
line = null;
count++;
}
// If more lines exist, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
}
PrintDocument是一个非常基本的API。您将获得简单的常规打印,但是这样做的代价是功能的减少并非特定于打印驱动程序。我的HP打印机通常会给我打印错误而不是异常。看到类似的事情发生在您身上也就不足为奇了。
闪烁可能是您可以查询的代码。如果失败,则可以尝试保存为图像格式(PDF或XPS)。或使用第三方库或编写自己的PCL文件。有很多选择。如果调试诸如边距之类的计算,则创建一个可以查看的输出而不是内存中的输出。您可以查看PDF,看它是否古怪。请记住,在PC上的外观可能与输出略有不同,尤其是在边缘附近打印时。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句