我遇到了在热打印机上调整页面大小的问题。我从这个答案开始:https://stackoverflow.com/a/27165167/1030464,现在我有了粘贴在下面的代码。
这是很好的工作,尽管我计算和设置页面的大小,它似乎打印一个完整的A4大小的页面每次。(我正在Sam4s Ellix和Microsoft打印机上进行测试)--这是一个大问题,因为它需要经常打印5-6行长文本片段。
我需要支持多种热打印机,我只需要基本的功能(所以不需要接收信号,如卡纸,等等)。因此,我决定使用Windows打印机驱动程序,而不是.NET one的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;
}发布于 2016-02-25 09:22:04
当打印到POS打印机时,不必计算高度,因为驱动程序处理纸张高度,并在文档末尾切割。转到POS打印机设置并选择“收据”作为纸张大小。通常也有一些设置来控制打印机切纸的方式和时间(全切,部分裁剪,只送纸,.)
https://stackoverflow.com/questions/35392714
复制相似问题