使用.NET PrintDocument的动态纸张高度

mol*_*bal 9 .net printing point-of-sale

我在热敏打印机上调整页面大小时遇到​​了问题.我从这个答案开始:https://stackoverflow.com/a/27165167/1030464现在我有下面粘贴的代码.

这很好用,不过我计算并设置页面的大小,似乎每次打印一个完整的A4大小的页面.(我正在使用Sam4s Ellix II和Microsoft PDF打印机进行测试) - 这是一个很大的问题,因为它需要经常打印5-6行长的文本片段.

我需要支持多个热敏打印机,我只需要基本功能(所以不需要接收卡纸等信号)所以我决定使用Windows打印机驱动程序,而不是POS for .NET.

我计算文本的高度并相应地调整纸张尺寸,但它对输出纸张尺寸没有影响.有没有人有这个问题的解决方案?

非常感谢你

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;
}
Run Code Online (Sandbox Code Playgroud)

小智 4

打印到 POS 打印机时无需计算高度,因为驱动程序会处理纸张高度并在文档末尾进行剪切。转至 POS 打印机设置并选择“收据”作为纸张尺寸。通常还有一些设置来控制打印机裁纸的方式和时间(完全裁纸、部分裁纸、仅送纸......)