/// <summary> /// Print the data on printer. /// </summary> /// <param name="header">The header.</param> /// <param name="lines">The lines.</param> /// <param name="footer">The footer.</param> public void Print(string header, string lines, string footer) { string textToPrint = (header + lines + footer).Replace(OposHelper.EscMarker, OposHelper.EscCharacter); var parts = TextLogoParser.Parse(textToPrint); foreach (var part in parts) { if (part.TextType == TextType.LegacyLogo) { this.PrintLegacyLogo(); } if (part.TextType == TextType.LogoWithBytes) { this.PrintLogo(part.Value); } if (part.TextType == TextType.Text) { this.PrintText(part.Value); } } this.oposPrinter.CutPaper(100); }
/// <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> /// Converts the OPOS formatted receipt into an xml Star WebPRNT request. /// </summary> /// <param name="data">OPOS receipt data.</param> /// <returns>Star WebPRNT request.</returns> private string ConvertToXml(string data) { if (string.IsNullOrWhiteSpace(data)) { return(data); } StringBuilder receipt = new StringBuilder(); var lines = data.Split(new[] { Environment.NewLine }, StringSplitOptions.None); foreach (var line in lines) { var textValues = line.Split(new[] { Seperator }, StringSplitOptions.RemoveEmptyEntries); foreach (var textValue in textValues) { bool bold = textValue.StartsWith(Bold, StringComparison.Ordinal); string textToPrint = textValue.StartsWith(CommandCharacter, StringComparison.Ordinal) ? textValue.Substring(CommandLength) : textValue; var fields = TextLogoParser.Parse(textToPrint); foreach (var field in fields) { switch (field.TextType) { case TextType.LegacyLogo: this.AppendLegacyLogo(receipt); break; case TextType.LogoWithBytes: this.AppendLogo(receipt, field.Value); break; case TextType.Text: this.AppendText(receipt, field.Value, bold); break; } } } receipt.Append(NewLine); } return(receipt.ToString()); }
/// <summary> /// Print the data on printer. /// </summary> /// <param name="header">The header.</param> /// <param name="lines">The lines.</param> /// <param name="footer">The footer.</param> public void Print(string header, string lines, string footer) { string textToPrint = header + lines + footer; if (!string.IsNullOrWhiteSpace(textToPrint)) { using (PrintDocument printDoc = new PrintDocument()) { printDoc.PrinterSettings.PrinterName = this.PrinterName; string subString = textToPrint.Replace(EscMarker, string.Empty); var printText = subString.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); this.parts = new List <TextPart>(); foreach (var line in printText) { var lineParts = TextLogoParser.Parse(line); if (null != lineParts) { this.parts.AddRange(lineParts); } } this.printLine = 0; printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.PrintPageHandler); #if DEBUG if ("Microsoft XPS Document Writer" == this.PrinterName) { printDoc.PrinterSettings.PrintFileName = Path.Combine(Path.GetTempPath(), "HardwareStation_Print_Result.xps"); printDoc.PrinterSettings.PrintToFile = true; NetTracer.Information(string.Format(CultureInfo.InvariantCulture, "Look for XPS file here: {0}", printDoc.PrinterSettings.PrintFileName)); } #endif printDoc.Print(); } } }
/// <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); }