using System; using System.Drawing; using System.Drawing.Printing; using System.Linq; using System.Windows.Forms; namespace Neo.LegitimateLicences.ActiveX.Printing { public class AccreditationDecalPrinter { #region Properties private readonly string _accreditationNumber; private readonly string _operatingLicenceNumber; private readonly string _vinNumber; private readonly string _engineNumber; private readonly string _vehicleRegistrationNumber; private readonly string _grossVehicleMass; private readonly string _tareWeight; private readonly string _vehicleMake; private readonly string _vehicleType; private readonly string _seatedCapacity; private readonly string _standingCapacity; private readonly string _verificationToken; private readonly string _verificationUrl; private readonly DateTime _dateOfExpiry; private readonly string _name; private readonly string _contact; private readonly string _address; private readonly short _copies; private readonly string _printerPath; private readonly int _height = 297; private readonly int _width = 210; private readonly bool _preview; private readonly bool _isTest; private readonly DateTime _printDate; private readonly Font _letterText = new Font("Arial", 10); private readonly Font _normal = new Font("Arial", 9, FontStyle.Bold); private readonly Font _heading = new Font("Arial", 9, FontStyle.Bold); private readonly Font _largeHeading = new Font("Arial", 12, FontStyle.Bold); private readonly Font _small = new Font("Arial", 8, FontStyle.Bold); private readonly Font _smallNotBold = new Font("Arial", 8); private int _yCoord; #endregion #region AccreditationDecalPrinter public AccreditationDecalPrinter(string accreditationNumber, string operatingLicenceNumber, string vinNumber, string engineNumber, string vehicleRegistrationNumber, string grossVehicleMass, string tareWeight, string vehicleMake, string vehicleType, string seatedCapacity, string standingCapacity, string verificationToken, string verificationUrl, DateTime dateOfExpiry, string name, string contact, string address, short copies, string printerPath, DateTime printDate, bool preview, bool isTest) { _accreditationNumber = accreditationNumber; _operatingLicenceNumber = operatingLicenceNumber; _vinNumber = vinNumber; _engineNumber = engineNumber; _vehicleRegistrationNumber = vehicleRegistrationNumber; _grossVehicleMass = grossVehicleMass; _tareWeight = tareWeight; _vehicleMake = vehicleMake; _vehicleType = vehicleType; _seatedCapacity = seatedCapacity; _standingCapacity = standingCapacity; _verificationToken = verificationToken; _verificationUrl = verificationUrl; _dateOfExpiry = dateOfExpiry; _name = name; _contact = contact; _address = address; _copies = copies; _printDate = printDate; _printerPath = printerPath; _preview = preview; _isTest = isTest; } #endregion #region Print public void Print() { var printDocument = new PrintDocument { PrinterSettings = { Copies = this._copies } }; printDocument.PrintPage += 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 //, 394, 197); printDocument.DefaultPageSettings.PaperSize = paperSize; if (_preview) { var printPreviewDialog = new PrintPreviewDialog { Document = printDocument }; ((ToolStripButton)((ToolStrip)printPreviewDialog.Controls[1]).Items[0]).Enabled = false; printPreviewDialog.ShowDialog(); } else { printDocument.Print(); } } #endregion #region IncrementYCoord private void IncrementYCoord(int height) { _yCoord += height; } #endregion #region toPrinter private void ToPrinter(Object sender, PrintPageEventArgs e) { var marginLeft = 10; var marginRight = 180; var showStandingCapacity = false; var decalLeft = 55; var decalWidth = 90; var decalTop = 0; _yCoord = 38; #region Brushes and pens var graphics = e.Graphics; graphics.PageUnit = GraphicsUnit.Millimeter; Brush blackBrush = new SolidBrush(Color.Black); var blackPen = new Pen(blackBrush, 1); Brush whiteBrush = new SolidBrush(Color.White); var whitePen = new Pen(whiteBrush, 1); Brush activeBrush = new SolidBrush(Color.White); Brush grayBrush = new SolidBrush(Color.Gray); var grayPen = new Pen(grayBrush, 1); #endregion #region text bit at the top graphics.DrawString(_name, _letterText, blackBrush, marginLeft, _yCoord); IncrementYCoord(4); foreach (var line in _address.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None)) { graphics.DrawString(line, _letterText, blackBrush, marginLeft, _yCoord); IncrementYCoord(4); } graphics.DrawString(_printDate.ToString("dd MMM yyyy"), _letterText, blackBrush, marginRight - 20, _yCoord); IncrementYCoord(10); graphics.DrawString("Dear " + _contact, _letterText, blackBrush, marginLeft, _yCoord); IncrementYCoord(10); graphics.DrawString("The token below is issued in accordance with Regulation 35(5) of the National Land Transport ", _letterText, blackBrush, marginLeft, _yCoord); IncrementYCoord(4); graphics.DrawString("Regulations 2009 and must be affixed to the lower, inside, left-hand corner of the windscreen", _letterText, blackBrush, marginLeft, _yCoord); IncrementYCoord(4); graphics.DrawString("of the vehicle in such a manner that the print on its face is clearly eligible from the outside", _letterText, blackBrush, marginLeft, _yCoord); IncrementYCoord(4); graphics.DrawString("to a person standing in front or to the left of the vehicle.", _letterText, blackBrush, marginLeft, _yCoord); IncrementYCoord(10); graphics.DrawString("Regards,", _letterText, blackBrush, marginLeft, _yCoord); IncrementYCoord(6); graphics.DrawString("NPTR,", _letterText, blackBrush, marginLeft, _yCoord); #endregion decalTop = _yCoord + 20; #region Draw background decal switch (_vehicleType) { case "Bus": activeBrush = blackBrush; graphics.DrawEllipse(whitePen, decalLeft, decalTop, decalWidth, decalWidth); graphics.FillEllipse(new SolidBrush(Color.FromArgb(112,173,1)), decalLeft, decalTop, decalWidth, decalWidth); showStandingCapacity = true; break; case "Midibus": activeBrush = blackBrush; graphics.DrawEllipse(whitePen, decalLeft, decalTop, decalWidth, decalWidth); graphics.FillEllipse(new SolidBrush(Color.FromArgb(112,173,1)), decalLeft, decalTop, decalWidth, decalWidth); break; case "Motorcar": activeBrush = blackBrush; graphics.DrawEllipse(whitePen, decalLeft, decalTop, decalWidth, decalWidth); graphics.FillEllipse(new SolidBrush(Color.FromArgb(255,102,94)), decalLeft, decalTop, decalWidth, decalWidth); break; case "Minibus": activeBrush = blackBrush; graphics.DrawEllipse(whitePen, decalLeft, decalTop, decalWidth, decalWidth); graphics.FillEllipse(new SolidBrush(Color.FromArgb(255,255,0)), decalLeft, decalTop, decalWidth, decalWidth); break; default: activeBrush = blackBrush; graphics.DrawEllipse(grayPen, decalLeft, decalTop, decalWidth, decalWidth); graphics.FillEllipse(new SolidBrush(Color.LightGray), decalLeft, decalTop, decalWidth, decalWidth); break; } #endregion #region Add decal text IncrementYCoord(32); graphics.DrawString("Tourist Transport Services", _heading, activeBrush, decalLeft + 24, _yCoord); IncrementYCoord(6); graphics.DrawString("RSA", _heading, activeBrush, decalLeft + 40, _yCoord); IncrementYCoord(6); graphics.DrawString("NATIONAL PUBLIC TRANSPORT REGULATOR", _heading, activeBrush, decalLeft + 10, _yCoord); IncrementYCoord(6); graphics.DrawString("Name: " + _name, _small, activeBrush, decalLeft + 10, _yCoord); IncrementYCoord(4); graphics.DrawString("Accreditation Number" + _accreditationNumber, _small, activeBrush, decalLeft + 10, _yCoord); IncrementYCoord(4); graphics.DrawString("Operating Licence Number: " + _operatingLicenceNumber, _small, activeBrush, decalLeft + 10, _yCoord); IncrementYCoord(4); if (showStandingCapacity) { graphics.DrawString("Vehicle Type & Capacity: " + _vehicleType + " (" + _seatedCapacity + " seated, " + _standingCapacity + " standing)", _small, activeBrush, decalLeft + 10, _yCoord); } else { graphics.DrawString("Vehicle Type & Capacity: " + _vehicleType + " (" + _seatedCapacity + " seated)", _small, activeBrush, decalLeft + 10, _yCoord); } #endregion #region add registration number IncrementYCoord(6); graphics.DrawString("Vehicle Registration Number", _small, activeBrush, decalLeft + 24, _yCoord); IncrementYCoord(4); graphics.DrawRectangle(whitePen, new Rectangle(decalLeft + 28, _yCoord, 30, 6)); graphics.FillRectangle(whiteBrush,new Rectangle(decalLeft + 28, _yCoord, 30, 6)); graphics.DrawString(_vehicleRegistrationNumber, _largeHeading, activeBrush, decalLeft + 32, _yCoord+1); IncrementYCoord(9); graphics.DrawString("Date of expiry: " + _dateOfExpiry.ToString("dd MMM yyyy"), _small, activeBrush, decalLeft + 25, _yCoord); #endregion #region verification text IncrementYCoord(6); graphics.DrawString("To verify browse to:", _smallNotBold, activeBrush, decalLeft + 32, _yCoord); IncrementYCoord(4); graphics.DrawString(_verificationUrl, _smallNotBold, activeBrush, decalLeft + 27, _yCoord); IncrementYCoord(4); graphics.DrawString("Verification Token: ", _smallNotBold, activeBrush, decalLeft + 34, _yCoord); IncrementYCoord(4); graphics.DrawString(_verificationToken, _small, activeBrush, decalLeft + 33, _yCoord); #endregion #region Test Watermark if (_isTest) { graphics.RotateTransform(e.PageSettings.Landscape ? 30 : 45); graphics.DrawString("TEST PAGE", new Font("Arial", 100, FontStyle.Bold), new SolidBrush(Color.FromArgb(64, Color.Black)), 50, 20); } #endregion } #endregion } }