打印长期收据

时间:2019-01-27 08:05:17

标签: c# winforms

我正在尝试在Windowsforms应用程序中打印收据。我已经编写了一些测试代码来运行并打印从0到100的行。我的问题是它一直打印到第45行,并且没有给出任何错误。我有一台热敏打印机xPrinter xp-80c,在此打印机上,纸张的最大长度为30cm。当打印为pdf时,仅打印一页。 任何人都可以在这里提供帮助,并说出问题所在。 预先谢谢你。

public static void printNewMethodFromVideoDeleteMeWhenDone()
    {
        PrintDialog printDialog = new PrintDialog();
        PrintDocument printDocument = new PrintDocument();
        printDialog.Document = printDocument;
        printDocument.PrintPage += printDocument3_PrintPage;
        DialogResult result = printDialog.ShowDialog();
        if(result == DialogResult.OK)
        {
            printDocument.Print();
        }
    }
private static void printDocument3_PrintPage(object sender, PrintPageEventArgs e)
    {
        try
        {
            Graphics graphic = e.Graphics;
            Font font = new Font("Courier New", 12);
            float fontHeight = font.GetHeight();
            SolidBrush brush = new SolidBrush(Color.Black);

            int startX = 10;
            int startY = 10;
            int offset = 40;

            graphic.DrawString("Welcome to the shop", new Font("Courier New", 16), brush, startX, startY);

            for (int i = 0; i < 100; i++)
            {
                string productDescription = "Some text " + i.ToString().PadRight(30);
                string productTotal = string.Format("{0:c}", i.ToString());
                string line = productDescription + productTotal;

                graphic.DrawString(line, font, new SolidBrush(Color.Black), startX, startY + offset);
                offset = offset + (int)fontHeight + 5;
            }
            offset = offset + 20;
            graphic.DrawString("Total To Pay".PadRight(30) + string.Format("{0:c}", 100.ToString()), font, brush, startX, startY + offset);
        }
        catch(Exception ex)
        {
            // some code here.
        }
    }

1 个答案:

答案 0 :(得分:0)

循环使用C#编程来构建HTML文档,并使用webBrowser.Print()函数进行打印。这要容易得多。

调用webBrowser.ShowPrintPreview()将纸张和边距调整为零。

在webBrowser完成呈现文档后,创建一个documentReady事件以打印文档。

webBrowser.DocumentReady += webBrowser_documentReady;

以下是演示该想法的示例项目:Clickable URLs