【问题标题】:Dynamic paper height with .NET PrintDocument.NET PrintDocument 的动态纸张高度
【发布时间】: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


【解决方案1】:

打印到 POS 打印机时,您不必计算高度,因为驱动程序会处理纸张高度并在文档末尾剪切。 转到 POS 打印机设置并选择“收据”作为纸张尺寸。通常还有一些设置可以控制打印机切割纸张的方式和时间(全切、部分卷曲、仅进纸……)

【讨论】:

  • 哇,谢谢!我完全忘记检查驱动程序设置了!
  • 可能值得一提的是,我最近有一台没有此“收据”设置的单据打印机。然而,它确实有一个 3.14 x 3200 的页面大小(我想这是他们的“无限页面高度”的方式)。但是,在您最后打印的行之后,打印机仍会正确裁切单据。
猜你喜欢
  • 2017-09-30
  • 1970-01-01
  • 2011-01-09
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2011-12-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多