public delegate void OnPrintEndEventHandler(); public delegate void…
- User
public delegate void OnPrintEndEventHandler();
public delegate void OnPrintCorrectlyEventHandler(); public static void PrintString(string s, string font = "Times New Roman", int fontSize = 12, int xMargin = 0, int yMargin = 0, OnPrintEndEventHandler OnPrintEnd = null, OnPrintCorrectlyEventHandler OnPrintCorrectly = null) { int linesPrinted = 0; int pagesPrinted = 1; bool multiPage = false; string[] str = s.Split('\n'); PrintDocument p = new PrintDocument(); float y2Margin = p.DefaultPageSettings.PrintableArea.Height - yMargin; float x2Margin = p.DefaultPageSettings.PrintableArea.Width - xMargin; int lastPrintedPage = -1; p.PrintPage += delegate (object sender1, PrintPageEventArgs e1) { Font myFont = new Font( font, fontSize, System.Drawing.FontStyle.Bold); float lineHeight = myFont.GetHeight(e1.Graphics) + 4; float yLineTop = yMargin; int numLines = s.Count(c => c.Equals('\n')) + 1; e1.HasMorePages = false; while (linesPrinted < numLines) { if (yLineTop + lineHeight > y2Margin) { e1.HasMorePages = true; multiPage = true; pagesPrinted = PrintPageLine(font, yMargin, x2Margin, e1, pagesPrinted, y2Margin); return; } e1.Graphics.DrawString( str[linesPrinted], myFont, new SolidBrush(Color.Black), new PointF(xMargin, yLineTop)); linesPrinted++; yLineTop += lineHeight; } if (multiPage) { PrintPageLine(font, yMargin, x2Margin, e1, pagesPrinted, y2Margin); } lastPrintedPage = e1.PageSettings.PrinterSettings.ToPage - 1; }; if (OnPrintEnd != null || OnPrintCorrectly != null) { p.EndPrint += (o, e) => { OnPrintEnd?.Invoke(); if (lastPrintedPage == p.PrinterSettings.MaximumPage - 1) { OnPrintCorrectly?.Invoke(); } }; } try { p.Print(); } catch (Exception ex) { Log.Error(ex); } } - Assistant
As an AI language model, I cannot run the code. However, I can give you an explanation of what the code does.
This C# code defines three delegates: OnPrintEndEventHandler, OnPrintCorrectlyEventHandler, and PrintPageEventHandler. Then, it defines a method called PrintString which takes in a string s, optional parameters for the font type, font size, x and y margins, and two optional event handlers.
The method splits the input string s by newline characters ('\n') and sets up a PrintDocument object. It then sets up a print page event handler that prints each line of the string onto the current page of the print document. The event handler also checks if the current page has ran out of space and continues onto the next page if needed. If the printing process completes successfully, the method will call the OnPrintCorrectly event handler (if it exists), but if an error occurs, it will log the error using a method called Log.Error.
Overall, this code allows the user to easily print a string with customizable font and margin settings, as well as handle completion events.
- User
OnPrintCorrectlyEventHandler doesn't work, help me do it correctly.
there are several errors,
-
lastprintedpage doesn't has the last page printed.
-
p.PrinterSettings.MaximumPage returns 9999 instead of the number of the last page printed
-
- Assistant
- User
it doesn't work because:
in this line: totalPages == e1.PageSettings.PrinterSettings.MaximumPage
totalPages = 0, and PageSettings.PrinterSettings.MaximumPage = 9999
- Assistant