using System; using System.Drawing; using System.Drawing.Printing; using System.Globalization; using System.Reflection; using System.Resources; using System.Windows.Forms; namespace Neo.LegitimateLicences.ActiveX.Printing { class PaymentAdvicePrinter { #region Properties private readonly string _applicationNumber; private readonly string _applicantName; private readonly string _associationName; private string _vehicleRegistrationNumber; private string _vehicleChassisNumber; private readonly string _amountDue; private readonly string _applicationDescription; private readonly string _idNumber; private readonly string _printedBy; private DateTime _printedDate; private readonly short _copies; private readonly string _printerPath; private readonly int _height; private readonly int _width; private readonly int _leftMargin; private readonly bool _preview; private readonly Font _normal = new Font("Arial", 10); private readonly Font _bold = new Font("Arial", 10, FontStyle.Bold); private readonly Brush _brush = new SolidBrush(Color.Black); #endregion #region ReceiptPrinter public PaymentAdvicePrinter(string applicationNumber, string applicantName, string associationName, string amountDue, string applicationDescription, string idNumber, string vehicleRegistrationNumber, string vehicleChassisNumber, string printedBy, short copies, string printerPath, int height, int width, bool preview) { _applicationNumber = applicationNumber; _applicantName = applicantName; _associationName = associationName; _vehicleRegistrationNumber = vehicleRegistrationNumber; _vehicleChassisNumber = vehicleChassisNumber; _amountDue = amountDue; _applicationDescription = applicationDescription; _idNumber = idNumber; _printedBy = printedBy; _printedDate = DateTime.Now; _copies = copies; _printerPath = printerPath; _height = height; //in mm _width = width; //in mm _leftMargin = 20; _preview = preview; } #endregion #region Print public void Print() { var printDocument = new PrintDocument { PrinterSettings = { Copies = this._copies } }; printDocument.PrintPage += new PrintPageEventHandler(ToPrinter); printDocument.PrinterSettings.PrinterName = _printerPath; var paperSize = new PaperSize("A4", Convert.ToInt32(_width / 0.254), Convert.ToInt32(_height / 0.254)); //convert to 100ths of an inch printDocument.DefaultPageSettings.PaperSize = paperSize; if (_preview) { var previewDialog = new PrintPreviewDialog { Document = printDocument }; previewDialog.ShowDialog(); } else { printDocument.Print(); } } #endregion #region ToPrinter private void ToPrinter(Object sender, PrintPageEventArgs e) { var graphics = e.Graphics; var pen1 = new Pen(_brush, 1); var pen2 = new Pen(_brush, 2); var dueDatePlaceHolder = DateTime.Now.AddDays(7); Brush br = new SolidBrush(Color.Black); var currentY = 0; #region Page Header currentY += 5; var logo = (Image)Properties.Resources.ResourceManager.GetObject("TransportDepartmentLogo"); if (logo != null) graphics.DrawImage(logo, _leftMargin - 5, currentY); currentY += 180; graphics.DrawLine(pen1, _leftMargin, currentY, _leftMargin + 740, currentY); currentY += 10; graphics.DrawString("Provincial Regulatory Entity: KZN", new Font("Arial", 14, FontStyle.Bold), br, 245, currentY); currentY += 30; graphics.DrawString("PAYMENT ADVICE FOR " + _applicationNumber, _bold, br, 275, currentY); #endregion #region Body currentY += 45; graphics.DrawString("Application Number: ", _bold, _brush, _leftMargin, currentY); graphics.DrawString(_applicationNumber, _normal, _brush, _leftMargin + 200, currentY); currentY += 22; graphics.DrawString("Application Type: ", _bold, _brush, _leftMargin, currentY); graphics.DrawString(_applicationDescription, _normal, _brush, _leftMargin + 200, currentY); currentY += 22; graphics.DrawString("Applicant Name: ", _bold, _brush, _leftMargin, currentY); graphics.DrawString(_applicantName, _normal, _brush, _leftMargin + 200, currentY); currentY += 22; graphics.DrawString("ID Number: ", _bold, _brush, _leftMargin, currentY); graphics.DrawString(_idNumber, _normal, _brush, _leftMargin + 200, currentY); currentY += 22; graphics.DrawString("Association Name: ", _bold, _brush, _leftMargin, currentY); graphics.DrawString(_associationName, _normal, _brush, _leftMargin + 200, currentY); currentY += 22; graphics.DrawString("Vehicle Registration Number: ", _bold, _brush, _leftMargin, currentY); graphics.DrawString(_vehicleRegistrationNumber, _normal, _brush, _leftMargin + 200, currentY); currentY += 22; graphics.DrawString("Vehicle Chassis Number: ", _bold, _brush, _leftMargin, currentY); graphics.DrawString(_vehicleChassisNumber, _normal, _brush, _leftMargin + 200, currentY); #endregion #region Footer const int footerPos = 385; graphics.DrawLine(pen1, _leftMargin, footerPos + 55, _leftMargin + 740, footerPos + 55); graphics.DrawString("Amount Due:", _bold, _brush, _leftMargin, footerPos + 70); graphics.DrawString((Convert.ToDecimal(_amountDue)).ToString("C", CultureInfo.GetCultureInfo("en-ZA")), _normal, _brush, _leftMargin + 100, footerPos + 70); graphics.DrawString("Due Date:", _bold, _brush, _leftMargin + 550, footerPos + 70); graphics.DrawString(dueDatePlaceHolder.ToString("dd MMM yyy"), _normal, _brush, _leftMargin + 650, footerPos + 70); graphics.DrawLine(pen1, _leftMargin, footerPos + 100, _leftMargin + 740, footerPos + 100); #endregion graphics.DrawString("Printed By:", _normal, _brush, _leftMargin, 1050); graphics.DrawString(_printedBy, _normal, _brush, _leftMargin + 100, 1050); graphics.DrawString("Printed Date:", _normal, _brush, _leftMargin + 550, 1050); graphics.DrawString(_printedDate.ToString("dd MMM yyyy"), _normal, _brush, _leftMargin + 650, 1050); } #endregion } }