c# - 如何在 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?报告并不总是超过页面,好像有更多的事务,会有很长的报告,所以仍然需要处理多页。
解决方案
推荐阅读
- ionic-framework - ionic 4 beta 未找到已安装的构建工具
- javascript - 链接的下拉菜单
- mips - 如何完成一个发出声音的mips程序?
- java - 如何在 Heroku 应用程序上设置自定义 SSLSocketFactory?
- java - 如何使用 Scanner 从 {int:int} 中提取整数?
- bash - 查找 IP 地址并验证它在哪个子网中
- python - 如何在 tkinter 中制作多行按钮?
- python - BeautifulSoup:查找具有特定属性的所有标签,而不是值
- keras - 如何在每个时期后的训练期间打印权重和偏差
- php - 如何从静态数组制作动态数组