I am successfully printing labels directly to a Brother QL label printer using the code below. The problem is that the printer seems to be ignoring the custom length I am trying to set at run time via c# to result in a longer or shorter label. I can change the page length in Windows Printer Settings but I am unable to set a custom label length at run time which would be dependent on the amount of information I'm rendering on a particular label. Note that I know I can use bPac to print a variable length label but I would really like to get away from using their templates. I've found them to be unnecessarily cumbersome. Well, unnecessary unless I'm unable to set a custom length otherwise.
Any suggestions would be appreciated.
Thank you for reading.
Here's the code I'm using:
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);
}
I made it in such way
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.