使用 .NET PrintDocument 的动态纸张高度
Dynamic paper height with .NET PrintDocument
我 运行 遇到了在热敏打印机上调整页面大小的问题。我是从这个答案开始的: 现在我有了下面粘贴的代码。
这很好用,但是尽管我计算并设置了页面的大小,但似乎每次都能打印完整的 A4 大小的页面。 (我正在 Sam4s Ellix II 和 Microsoft PDF 打印机上进行测试)- 这是个大问题,因为它需要经常打印 5-6 行长文本片段。
我需要支持多台热敏打印机,我只需要基本功能(所以不需要接收卡纸等信号)所以我决定使用 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;
}
打印到 POS 打印机时无需计算高度,因为驱动程序会处理纸张高度并在文档末尾进行切割。
转到 POS 打印机设置和 select 'Receipt' 作为纸张尺寸。通常还有控制打印机裁纸方式和时间的设置(全裁纸、部分裁纸、仅进纸……)
我 运行 遇到了在热敏打印机上调整页面大小的问题。我是从这个答案开始的: 现在我有了下面粘贴的代码。
这很好用,但是尽管我计算并设置了页面的大小,但似乎每次都能打印完整的 A4 大小的页面。 (我正在 Sam4s Ellix II 和 Microsoft PDF 打印机上进行测试)- 这是个大问题,因为它需要经常打印 5-6 行长文本片段。
我需要支持多台热敏打印机,我只需要基本功能(所以不需要接收卡纸等信号)所以我决定使用 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;
}
打印到 POS 打印机时无需计算高度,因为驱动程序会处理纸张高度并在文档末尾进行切割。 转到 POS 打印机设置和 select 'Receipt' 作为纸张尺寸。通常还有控制打印机裁纸方式和时间的设置(全裁纸、部分裁纸、仅进纸……)