I'm printing a transaction report through Thermal Printer. Currently the transaction report is append to StringBuilder and the printout report was only having one pages and it does not print the leftover report based from the record that been collection.
The transaction record in StringBuilder having 174 lines and the printed on receipt is until Line #162. I have try to follow How to: Print a Multi-Page Text File but it still print one page.
And I have try add e.HasMorePages = true; on PrintPageEventArgs after call handler to print and the printer keep printing the same page non-stop until I cancel the printing on system tray.
I know that I might be misplaced the e.HasMorePages as the problem cannot be solved. Currently my code can work on printing standard food and beverages receipt. Below are my code:-
THIS WAS ON MAIN PRINTER CLASS 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;
}
// THIS WAS ON PRINTING CLASS 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);
}
Anyone can assist on how do I correctly use the HasMorePages? The report not always been exceed the page as if there is more transaction, there will be long report so the need of handling multi pages is still acquire.