【发布时间】:2016-05-25 09:07:29
【问题描述】:
我在热敏打印机上调整页面大小时遇到了问题。我已经从这个答案开始:https://stackoverflow.com/a/27165167/1030464 现在我有我粘贴在下面的代码。
这很好用,但是尽管我计算并设置了页面大小,但似乎每次都打印一个完整的 A4 大小的页面。 (我在 Sam4s Ellix II 和 Microsoft PDF 打印机上进行测试) - 这是一个大问题,因为它经常需要打印 5-6 行长文本 sn-ps。
我需要支持多台热敏打印机,我只需要基本功能(所以不需要接收卡纸等信号)所以我决定使用 Windows 打印机驱动程序,而不是 .NET 的 POS一个。
我计算文本的高度并相应地调整纸张大小,但它对输出纸张大小没有影响。有人有这个问题的解决方案吗?
非常感谢
public int Print(DatabaseConnector dc)
{
try {
// Set up PrintDocument
PrintDocument recordDoc = new PrintDocument();
recordDoc.DocumentName = "PrintTask ID "+id.ToString();
recordDoc.PrintPage += new PrintPageEventHandler(PrintTask.PrintReceiptPage); // Filling in the stuff
// Print Controller
StandardPrintController spc = new StandardPrintController();
recordDoc.PrintController = spc; // This hides popup
// Printer Settings
PrinterSettings ps = new PrinterSettings();
ps.PrinterName = dc.ReadSetting("PrinterName");
recordDoc.PrinterSettings = ps;
recordDoc.Print();
// Clean up
recordDoc.Dispose();
}
catch (Exception exc)
{
((MainForm)Application.OpenForms[0]).msg(exc.Message);
}
return 1; // ignore this
}
private static void PrintReceiptPage(object sender, PrintPageEventArgs e)
{
try {
// Read settings
DatabaseConnector db = new DatabaseConnector();
PrintTask pt = db.ReadTask();
float x = float.Parse(db.ReadSetting("PaperMarginFromLeft"));
float y = float.Parse(db.ReadSetting("PaperMarginFromTop"));
float width = float.Parse(db.ReadSetting("PaperWidth"));
float height = 0F;
string text;
// Set up font
Font drawFont1 = new Font(db.ReadSetting("PrintFont"), Int32.Parse(db.ReadSetting("PrintFontSize")), FontStyle.Regular);
SolidBrush drawBrush = new SolidBrush(Color.Black);
// Set format of string.
StringFormat drawFormatLeft = new StringFormat();
drawFormatLeft.Alignment = StringAlignment.Near;
// Draw string to screen.
text = pt.getData();
e.Graphics.DrawString(text, drawFont1, drawBrush, new RectangleF(x, y, width, height), drawFormatLeft);
// calculate text size
SizeF textSize = e.Graphics.MeasureString(text, drawFont1);
y += textSize.Height;
// Set page size - has no effect
e.HasMorePages = false;
float inchHeight = PrintTask.PixelsToInchY(y, e.Graphics);
PaperSize originalPaperSize = e.PageSettings.PaperSize;
PaperSize scaledSize = new PaperSize("Custom", originalPaperSize.Width, (int)Math.Ceiling(inchHeight * 100));
e.PageSettings.PaperSize = scaledSize;
e.PageSettings.PrinterSettings.DefaultPageSettings.PaperSize = scaledSize;
}
catch (Exception exc)
{
((MainForm)Application.OpenForms[0]).msg(exc.Message);
}
}
public static float PixelsToInchX(float n, Graphics graphics)
{
return n * graphics.DpiX / 300;
}
public static float PixelsToInchY(float n, Graphics graphics)
{
return n * graphics.DpiY / 300;
}
【问题讨论】:
-
热敏打印机不使用页面,它们有一个连续的纸卷。使用 PrintDocument 类几乎是不正确的。不仅仅是因为尺寸,它通常也太慢了。 POS 是正确的方法。
标签: .net printing point-of-sale