/// <summary> /// Prints the page. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="PrintPageEventArgs" /> instance containing the event data.</param> private void PrintPageHandler(object sender, PrintPageEventArgs e) { const int LineHeight = 10; const string TextFontName = "Courier New"; const int TextFontSize = 7; e.HasMorePages = false; using (Font textFont = new Font(TextFontName, TextFontSize, FontStyle.Regular)) using (Font textFontBold = new Font(TextFontName, TextFontSize, FontStyle.Bold)) { float y = 0; float dpiXRatio = e.Graphics.DpiX / 96f; // 96dpi = 100% float dpiYRatio = e.Graphics.DpiY / 96f; // 96dpi = 100% // This calculation isn't exactly the width of the rendered text. // All the calculations occurring in the rendering code of PrintTextLine. It almost needs to run that code and use e.Graphics.MeasureString() // the first time to get the true contentWidth, then re-run the same logic using the true contentWidth for rendering. // // For now, the rendering is close, but it's not 'exact' center due to the mismatch in estimated vs. true size float contentWidth = this.parts.Where(x => x.TextType == TextType.Text).Select(p => p.Value).Max(str => str.Replace(NormalTextMarker, string.Empty).Replace(BoldTextMarker, string.Empty).Length) * dpiXRatio; // Line with max length = content width for (; this.printLine < this.parts.Count; this.printLine++) { var part = this.parts[this.printLine]; if (part.TextType == TextType.Text) { if (!this.PrintTextLine(e, LineHeight, textFont, textFontBold, dpiYRatio, contentWidth, dpiXRatio, part.Value, ref y)) { return; } } else if (part.TextType == TextType.LegacyLogo) { byte[] defaultLogoBytes = this.defaultLogo.GetBytes(); if (!DrawBitmapImage(e, defaultLogoBytes, contentWidth, ref y)) { return; } } else if (part.TextType == TextType.LogoWithBytes) { byte[] image = TextLogoParser.GetLogoImageBytes(part.Value); if (!DrawBitmapImage(e, image, contentWidth, ref y)) { return; } } } } }
/// <summary> /// Prints the logo embedded in a logo tag via base64 encoding. /// </summary> /// <param name="logoText">The logo text.</param> private void PrintLogo(string logoText) { byte[] image = TextLogoParser.GetLogoImageBytes(logoText); this.PrintLogoBytes(image); }
/// <summary> /// Appends the logo embedded in a logo tag via base64 encoding. /// </summary> /// <param name="receipt">The receipt content builder.</param> /// <param name="logoText">The logo text.</param> private void AppendLogo(StringBuilder receipt, string logoText) { byte[] image = TextLogoParser.GetLogoImageBytes(logoText); this.AppendLogoBytes(receipt, image); }