Thursday 7 June 2018

Customization in DLL

Peripharal-->Printer DLL to Customised POS Receipt Width and Barcode Alignment

private void printDoc_BeginPrint(object sender, PrintEventArgs e)
        {
            //Code to add Alignment Left Printing
            var printDocument = sender as PrintDocument;

            if (printDocument != null)
            {
                printDocument.DefaultPageSettings.Margins.Left = 0;
                printDocument.DefaultPageSettings.Margins.Right = 0;
                printDocument.DefaultPageSettings.Margins.Top = 1;
            }

            TextFontName = defaultFontName;
            TextFontSize = defaultFontSize;
            TextFontCharWidth = defaultFontCharWidth;
        }
------------------------------------------------------------------------------------------
private void printDoc_PrintPage(object sender, PrintPageEventArgs e)
        {
            try
            {
                e.HasMorePages = false;
                using (Font textFontRegular = new Font(TextFontName, TextFontSize, FontStyle.Bold))//FontStyle.Regular
                {
                    float xCaretPos = 0, yCaretPos = 0;
                    float dpiXRatio = e.Graphics.DpiX / 96f; // 96dpi = 100%
                    float dpiYRatio = e.Graphics.DpiY / 96f; // 96dpi = 100%
                    float contentWidth = printText.Max(str => str.Replace(NORMAL_TEXT_MARKER, string.Empty).Replace(BOLD_TEXT_MARKER, string.Empty).Replace(DOUBLESIZE_TEXT_MARKER, string.Empty).Replace(DOUBLESIZE_BOLD_TEXT_MARKER, string.Empty).Length) * dpiXRatio; // Line with max length = content width

                    while (this.printTextLine < printText.Length)
                    {
                        string printingLine;
                        var heightStep = IsStringContainAnyOfMarkers(printText[this.printTextLine], DOUBLESIZE_TEXT_MARKER, DOUBLESIZE_BOLD_TEXT_MARKER) ? 2 * defaultLineHeight : defaultLineHeight;

                        if (yCaretPos + heightStep >= e.MarginBounds.Height)
                        {   // No more room - advance to next page
                            e.HasMorePages = true;
                            return;
                        }

                        if (IsStringContainAnyOfMarkers(printText[this.printTextLine], BOLD_TEXT_MARKER, DOUBLESIZE_TEXT_MARKER, DOUBLESIZE_BOLD_TEXT_MARKER))
                        {
                            // Text line printing with bold or double size Text in it.
                            xCaretPos = 0;

                            printingLine = printText[this.printTextLine];
                            while (printingLine.Length > 0)
                            {
                                if (IsStringContainAnyOfMarkers(printingLine, BOLD_TEXT_MARKER, DOUBLESIZE_TEXT_MARKER, DOUBLESIZE_BOLD_TEXT_MARKER))
                                {
                                    string firstTextMarker = GetFirstTextMarker(printingLine, NORMAL_TEXT_MARKER, BOLD_TEXT_MARKER,
                                        DOUBLESIZE_TEXT_MARKER, DOUBLESIZE_BOLD_TEXT_MARKER);

                                    using (var textFontForPrint = CreateFontForMarker(firstTextMarker, TextFontName, TextFontSize))
                                    {
                                        int firstMarkerIndex = printingLine.IndexOf(firstTextMarker);
                                        printingLine = printingLine.Remove(firstMarkerIndex, textMarkerWidth);
                                        string textToPrint = printingLine.Substring(0, printingLine.IndexOf(NORMAL_TEXT_MARKER));

                                        string textBeforeFirstMarker = textToPrint.Substring(0, firstMarkerIndex);
                                        e.Graphics.DrawString(textBeforeFirstMarker, textFontRegular, Brushes.Black,
                                            xCaretPos + e.MarginBounds.Left, yCaretPos + e.MarginBounds.Top);
                                        xCaretPos += textBeforeFirstMarker.Length * TextFontCharWidth;

                                        e.Graphics.DrawString(textToPrint.Substring(firstMarkerIndex),
                                            textFontForPrint, Brushes.Black, xCaretPos + e.MarginBounds.Left, yCaretPos + e.MarginBounds.Top);
                                        xCaretPos += textToPrint.Substring(firstMarkerIndex).Length * TextFontCharWidth;

                                        if (new[] { DOUBLESIZE_TEXT_MARKER, DOUBLESIZE_BOLD_TEXT_MARKER, BOLD_TEXT_MARKER }.Contains(firstTextMarker))
                                        {
                                            printingLine = printingLine.Insert(printingLine.IndexOf(NORMAL_TEXT_MARKER) == -1 ?
                                                0 : printingLine.IndexOf(NORMAL_TEXT_MARKER) + NORMAL_TEXT_MARKER.Length,
                                                new string(' ', textToPrint.Substring(firstMarkerIndex).Length));
                                        }

                                        printingLine = printingLine.Substring(printingLine.IndexOf(NORMAL_TEXT_MARKER) == -1 ?
                                            0 : printingLine.IndexOf(NORMAL_TEXT_MARKER) + NORMAL_TEXT_MARKER.Length);
                                    }
                                }
                                else
                                {
                                    printingLine = printingLine.Replace(NORMAL_TEXT_MARKER, string.Empty);
                                    e.Graphics.DrawString(printingLine, textFontRegular, Brushes.Black, xCaretPos + e.MarginBounds.Left, yCaretPos + e.MarginBounds.Top);
                                    printingLine = string.Empty;
                                }
                            }
                        }
                        else
                        {
                            // Text line printing with no bold Text in it.

                            printingLine = printText[this.printTextLine].Replace(NORMAL_TEXT_MARKER, string.Empty);

                            Match barCodeMarkerMatch = Regex.Match(printingLine, barCodeRegEx, RegexOptions.Compiled | RegexOptions.IgnoreCase);

                            if (barCodeMarkerMatch.Success)
                            {
                                // Get the receiptId
                                printingLine = barCodeMarkerMatch.Groups[1].ToString();

                                using (Image barcodeImage = barCode.Create(printingLine, e.Graphics.DpiX, e.Graphics.DpiY))
                                {
                                    if (barcodeImage != null)
                                    {
                                        float barcodeHeight = (barcodeImage.Height / dpiYRatio);

                                        if (yCaretPos + barcodeHeight >= e.MarginBounds.Height)
                                        {   // No more room - advance to next page
                                            e.HasMorePages = true;
                                            return;
                                        }
                                        // Code Added by Vijay
                                        // Render barcode in the center of receipt.
                                        //commented to give 80 margin on left
                                        //e.Graphics.DrawImage(barcodeImage, ((contentWidth - (barcodeImage.Width / dpiXRatio)) / 2) + e.MarginBounds.Left, yCaretPos + e.MarginBounds.Top);
                                        e.Graphics.DrawImage(barcodeImage, ((contentWidth - (barcodeImage.Width / dpiXRatio)) / 2) + 80, yCaretPos + e.MarginBounds.Top);
                                        yCaretPos += barcodeHeight;
                                    }
                                }
                            }
                            else
                            {
                                e.Graphics.DrawString(printingLine, textFontRegular, Brushes.Black, e.MarginBounds.Left, yCaretPos + e.MarginBounds.Top);
                            }
                        }
                        yCaretPos = yCaretPos + heightStep;

                        printTextLine += 1;

                    } // of while()
                } // of using()
            } // of try
            catch (Exception ex)
            {
                NetTracer.Warning("Peripheral [Printer] - Exception in print page");

                ApplicationExceptionHandler.HandleException(this.ToString(), ex);
            }
        }
--------------------------------------------------------------------------------------------------------------------------
EOD-->Batch Printing DLL to customised Z Report and X Report Width

private static void AppendReportHeaderLine(this StringBuilder stringBuilder, int titleResourceId, string value, bool firstPart)
        {

            // int partWidth = (paperWidth / 2);
            int partWidth = (paperWidth / 3);//Code add by Vijay
            //  int titleWidth = (int)(partWidth * 0.5);
            int titleWidth = (int)(partWidth * 0.2);//Code add by Vijay
            // int valueWidth = (int)(partWidth * 0.4);
            int valueWidth = (int)(partWidth * 0.2);//Code add by Vijay

            string title = ApplicationLocalizer.Language.Translate(titleResourceId);
            string line = string.Format(lineFormat, title.PadRight(titleWidth), value.PadLeft(valueWidth));

            if (firstPart)
            {
                stringBuilder.Append(line.PadRight(partWidth));
            }
            else
            {
                stringBuilder.AppendLine(line.PadLeft(partWidth));
            }
        }
------------------------------------------------------------------------------
private static void AppendReportLine(this StringBuilder stringBuilder, string title, object value)
        {
            //stringBuilder.AppendLine(string.Format(lineFormat, title, value.ToString().PadLeft(paperWidth - title.Length - 2)));
            stringBuilder.AppendLine(string.Format(lineFormat, title, value.ToString().PadLeft(paperWidth - title.Length - 15)));//Code add for Width and length
        }