我使用以下代码成功将标签直接打印到Brother QL标签打印机上。问题是打印机似乎忽略了我试图通过c#在运行时设置的自定义长度,导致标签更长或更短。我可以在Windows打印机设置中更改页面长度,但我无法在运行时设置自定义标签长度,这取决于我在特定标签上呈现的信息量。请注意,我知道我可以使用bPac打印可变长度标签,但我真的想摆脱使用他们的模板。我发现它们不必要地繁琐。好吧,除非我无法设置自定义长度,否则不必要。
任何建议将不胜感激。
感谢您的阅读。
以下是我使用的代码:
private void SingleLabel()
{
label = new PrintDocument();
PaperSize pS = new PaperSize("Custom Size", 242, 55);
label.DefaultPageSettings.PaperSize = pS;
label.PrinterSettings.PrinterName = "Label_1";
label.PrinterSettings.DefaultPageSettings.PaperSize = pS;
label.PrintPage += new PrintPageEventHandler(label_PrintPage);
label.Print();
}
private void label_PrintPage(object sender, PrintPageEventArgs e)
{
SolidBrush brush = new SolidBrush(Color.Black);
Font header = new Font(FontFamily.GenericSansSerif, 12.0F, FontStyle.Bold);
e.Graphics.DrawString("Hello World", header, brush, 0, 0);
}
答案 0 :(得分:0)
我这样做了
public void OnPagePrint(object sender, PrintPageEventArgs e)
{
var availableWidth = 220;
var printAreaSize = DrawReceipt(availableWidth, e.Graphics, GetReceiptContent());
e.PageSettings.PaperSize = new PaperSize("CustomPaperSize", (int)printAreaSize.Width + 10, (int)printAreaSize.Height);
}
public SizeF DrawReceipt(float availableWidth, Graphics graphics, StringBuilder sb)
{
var printText = new PrintText(sb.ToString(), new Font("Consolas", 8));
var layoutArea = new SizeF(availableWidth, 0);
SizeF stringSize = graphics.MeasureString(printText.Text, printText.Font, layoutArea, printText.StringFormat);
RectangleF rectf = new RectangleF(new PointF(), new SizeF(availableWidth, stringSize.Height));
graphics.DrawString(printText.Text, printText.Font, Brushes.Black, rectf, printText.StringFormat);
foreach (var img in images)
{
graphics.DrawImage(img.Image, img.Location);
}
return stringSize;
}