首页 > 解决方案 > 如何在 C# 中设置打印机以继续打印下一页?

问题描述

我正在通过热敏打印机打印交易报告。目前,交易报告附加到 StringBuilder 并且打印输出报告只有一页,它不会根据收集的记录打印剩余的报告。

StringBuilder 中的交易记录有 174 行,打印在收据上直到第 162 行。我尝试遵循如何:打印多页文本文件,但它仍然打印一页。

我尝试添加 e.HasMorePages = true; 在调用处理程序进行打印之后在 PrintPageEventArgs 上,打印机会不停地打印相同的页面,直到我取消系统托盘上的打印。

我知道我可能错位了e.HasMorePages,因为问题无法解决。目前我的代码可以打印标准的食品和饮料收据。以下是我的代码: -

这是在主打印机课上PrinterMain.cs

public static bool StartReprintCollection()
{
    try
    {
        if (AppModel.CollectionPurpose.ReprintCollectionSummary != null)
        {
            PrintDocument PrintReceipt = new PrintDocument();
            PrintController printController = new StandardPrintController();
            PrintReceipt.PrintController = printController;

            PrintReceipt.PrintPage += new PrintPageEventHandler(PrinterJob.ReprintJournal);
            PrintReceipt.Print();
            PrintReceipt.PrintPage -= new PrintPageEventHandler(PrinterJob.ReprintJournal);
            PrintReceipt.Dispose();
            LogEvents($"Reprint collection journal started.", System.Diagnostics.EventLogEntryType.Information);
            return true;
        }
        else
        {
            LogEvents($"Collection journal records empty.", System.Diagnostics.EventLogEntryType.Information);
            return false;
        }
    }
    catch (Exception ex)
    {
        LogEvents($"Exception on Printer.StartReprintCollection. Message-{ex.Message}. Stack Trace-{ex.StackTrace}", System.Diagnostics.EventLogEntryType.Error);
    }
    return false;
}

// 这是在打印课上PrinterJob.cs

public static void ReprintJournal(object sender, PrintPageEventArgs e)
{
    try
    {
        DefinePageSettings(e);
        coordinateY = 20;
        string[] delim = { Environment.NewLine, "\n" };
        string[] lines = AppModel.CollectionPurpose.ReprintCollectionSummary.ToString().Split(delim, StringSplitOptions.None);
        foreach (string line in lines)
        {
            ReprintThis(line, e);
        }
        // I try to put e.HasMorePages = true here but result in non-stop print
    }
    catch (Exception ex)
    {
        string error = $"{PageTitle}  Exception on print collection journal receipt. Message: {ex.Message}. StackTrace: {ex.StackTrace}";
        LogEvents(error, System.Diagnostics.EventLogEntryType.Error);
    }
}
private static void ReprintThis(string Text, PrintPageEventArgs e)
{
    try
    {
        PrinterConfig.ReceiptFontProperties PrintFont = default(PrinterConfig.ReceiptFontProperties);
        PrintFont.FontFamily = AppModel.References.SupervisorPrint.FontFamily;
        PrintFont.FontSize = AppModel.References.SupervisorPrint.FontSize;
        PrintFont.FontAlignment = StringAlignment.Near;
        ReprintJournalText(e, PrintFont, Text);

    }
    catch (Exception ex)
    {
        string error = $"{PageTitle} Exception on send print formatted journal. Message: {ex.Message}. StackTrace: {ex.StackTrace}";
        LogEvents(error, System.Diagnostics.EventLogEntryType.Error);
    }
}
protected static void ReprintJournalText(PrintPageEventArgs e, PrinterConfig.ReceiptFontProperties FontInfo, string strText)
{
    Font PrintedFontProp = new Font(FontInfo.FontFamily, FontInfo.FontSize);
    Brush MyBrush = new SolidBrush(Color.Black);
    StringFormat MyStringFormat = new StringFormat();
    MyStringFormat.Alignment = StringAlignment.Near;
    Rectangle MyRect = new Rectangle(0, coordinateY, (int)PrtProp.availableWidth, 13);

    // This is the what I follow from Microsoft Docs
    int charactersOnPage = 0;
    int linesPerPage = 0;
    e.Graphics.MeasureString(strText, PrintedFontProp, e.MarginBounds.Size, StringFormat.GenericTypographic, out charactersOnPage, out linesPerPage);
    e.Graphics.DrawString(strText, PrintedFontProp, MyBrush, MyRect, MyStringFormat);
    strText = strText.Substring(charactersOnPage);
    e.HasMorePages = (strText.Length > 0);
    //This is the end of it

    SetCurrentY(PrintedFontProp.Height);
}

任何人都可以帮助我如何正确使用 HasMorePages?报告并不总是超过页面,好像有更多的事务,会有很长的报告,所以仍然需要处理多页。

标签: c#printing

解决方案


推荐阅读