我正在通过热敏打印机打印交易报告。当前,交易报告将追加到StringBuilder,并且打印输出报告只有一页,并且不会根据收集到的记录来打印剩余的报告。
StringBuilder中的交易记录有174行,并打印在收据上,直到第162行为止。我已尝试遵循How to: Print a Multi-Page Text File,但它仍打印一页。
我尝试添加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?报告不会总是超过页面,就好像有更多的事务一样,报告会很长,因此仍然需要处理多个页面。