using System; using System.Drawing; using System.Drawing.Printing; using System.Linq; using System.Windows.Forms; namespace Neo.LegitimateLicences.ActiveX.Printing { public class AccreditationCertificatePrinter { #region Properties private readonly string _accreditationNumber; private readonly string _name; private readonly string _idNumber; private readonly string _issueDate; private readonly string _expiryDate; private readonly string _certificateNumber; private readonly string _printedBy; private readonly string _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 bool _isTest; private readonly Font _normal = new Font("Arial", 11); private readonly Font _bold = new Font("Arial", 11, FontStyle.Bold); private readonly Font _subheading = new Font("Arial", 12, FontStyle.Bold); private readonly Font _heading = new Font("Arial", 16, FontStyle.Bold); private readonly Font _small = new Font("Arial", 9); private readonly Font _verysmall = new Font("Arial", 7); private readonly Font _verysmallbold = new Font("Arial", 7); private Font _barcodeFont = new Font("3 of 9 Barcode", 20); private int _yCoord; #endregion #region AccreditationCertificatePrinter public AccreditationCertificatePrinter(string accreditationNumber, string name, string idNumber, string issueDate, string expiryDate, string certificateNumber, string printedBy, string printedDate, short copies, int height, int width, string printerPath, bool preview, bool isTest) { _accreditationNumber = accreditationNumber; _name = name; _idNumber = idNumber; _certificateNumber = certificateNumber; _issueDate = issueDate; _expiryDate = expiryDate; _printedBy = printedBy; _printedDate = printedDate; _isTest = isTest; _printedBy = printedBy; _printedDate = printedDate; _copies = copies; _printerPath = printerPath; _height = height; //in mm _width = width; //in mm _leftMargin = 55; _preview = preview; } #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 graphics = e.Graphics; Brush solidBrush = new SolidBrush(Color.Black); var pen = new Pen(solidBrush, 1); var dashed = new Pen(solidBrush, 1) { DashPattern = new float[] { 3, 1 } }; var rectangle = new Rectangle(275, 765, 250, 250); var date = DateTime.Now; #region Logo //System.Reflection.Assembly thisExe; //thisExe = System.Reflection.Assembly.GetExecutingAssembly(); //System.IO.Stream file = thisExe.GetManifestResourceStream("NPTR.ActiveX.Printing.Resources.departmentlogo.jpg"); //var image = Image.FromStream(file); var logo = (Image)Properties.Resources.ResourceManager.GetObject("TransportDepartmentLogo"); graphics.DrawImage(logo, 220, 50); #endregion #region Barcode var g = e.Graphics; var br = new SolidBrush(Color.Black); var top = 0; g.DrawString("*" + _accreditationNumber.Substring(3) + "*", _barcodeFont, br, 603, top + 16); // the Asterisk (*) is used to delimit the barcode, and is required as the start and stop charachters by the 3of9 Symbology. g.DrawString("Printed By: " + _printedBy, _verysmall, br, 603, top + 40); g.DrawString("Date: " + _printedDate, _verysmall, br, 603, top + 51); #endregion IncrementYCoord(200); var stringToPrint = "DEPARTMENT OF TRANSPORT"; graphics.DrawString(stringToPrint, _heading, solidBrush, 400 - (graphics.MeasureString(stringToPrint, _heading).Width / 2), _yCoord); IncrementYCoord(30); stringToPrint = "Private Bag X 193, Pretoria, 0001"; graphics.DrawString(stringToPrint, _normal, solidBrush, 400 - (graphics.MeasureString(stringToPrint, _normal).Width / 2), _yCoord); IncrementYCoord(25); stringToPrint = "Forum Building, cnr Struben and Bosman Streets, Pretoria"; graphics.DrawString(stringToPrint, _normal, solidBrush, 400 - (graphics.MeasureString(stringToPrint, _normal).Width / 2), _yCoord); IncrementYCoord(50); stringToPrint = "CERTIFICATE OF ACCREDITATION FOR TOURIST TRANSPORT OPERATORS"; graphics.DrawString(stringToPrint, _subheading, solidBrush, 400 - (graphics.MeasureString(stringToPrint, _subheading).Width / 2), _yCoord); IncrementYCoord(25); stringToPrint = "In accordance with sections 80, 81 and 82 of National Land Transport Act No 5 of 2009,"; graphics.DrawString(stringToPrint, _normal, solidBrush, 400 - (graphics.MeasureString(stringToPrint, _normal).Width / 2), _yCoord); IncrementYCoord(25); stringToPrint = "the National Public Transport Regulator hereby certifies that"; graphics.DrawString(stringToPrint, _normal, solidBrush, 400 - (graphics.MeasureString(stringToPrint, _normal).Width / 2), _yCoord); IncrementYCoord(50); stringToPrint = _name; graphics.DrawString(stringToPrint, _subheading, solidBrush, 400 - (graphics.MeasureString(stringToPrint, _subheading).Width / 2), _yCoord); IncrementYCoord(25); stringToPrint = _idNumber; graphics.DrawString(stringToPrint, _subheading, solidBrush, 400 - (graphics.MeasureString(stringToPrint, _subheading).Width / 2), _yCoord); IncrementYCoord(50); stringToPrint = "has complied with the requirements of the above Act"; graphics.DrawString(stringToPrint, _normal, solidBrush, 400 - (graphics.MeasureString(stringToPrint, _normal).Width / 2), _yCoord); IncrementYCoord(25); stringToPrint = "and has been accredited as a Tourist Transport Operator."; graphics.DrawString(stringToPrint, _normal, solidBrush, 400 - (graphics.MeasureString(stringToPrint, _normal).Width / 2), _yCoord); IncrementYCoord(50); stringToPrint = "Certificate Number:"; graphics.DrawString(stringToPrint, _bold, solidBrush, 250, _yCoord); stringToPrint = _accreditationNumber; graphics.DrawString(stringToPrint, _normal, solidBrush, 400, _yCoord); IncrementYCoord(25); stringToPrint = "Issue Date:"; graphics.DrawString(stringToPrint, _bold, solidBrush, 311, _yCoord); stringToPrint = _issueDate; graphics.DrawString(stringToPrint, _normal, solidBrush, 400, _yCoord); IncrementYCoord(25); stringToPrint = "Expiry Date:"; graphics.DrawString(stringToPrint, _bold, solidBrush, 304, _yCoord); stringToPrint = _expiryDate; graphics.DrawString(stringToPrint, _normal, solidBrush, 400, _yCoord); IncrementYCoord(100); stringToPrint = "CONDITION:"; graphics.DrawString(stringToPrint, _bold, solidBrush, 50, _yCoord); IncrementYCoord(25); stringToPrint = "THIS CERTIFICATE DOES NOT AUTHORISE THE CONVEYANCE OF PASSENGERS WITHOUT"; graphics.DrawString(stringToPrint, _bold, solidBrush, 50, _yCoord); IncrementYCoord(25); stringToPrint = "OPERATING LICENCE, VEHICLE CERTIFICATION AND TOKEN AS PROVIDED BY THE ACT."; graphics.DrawString(stringToPrint, _bold, solidBrush, 50, _yCoord); IncrementYCoord(100); stringToPrint = "___________________"; graphics.DrawString(stringToPrint, _normal, solidBrush, 50, _yCoord); stringToPrint = "___________________"; graphics.DrawString(stringToPrint, _normal, solidBrush, 550, _yCoord); IncrementYCoord(25); stringToPrint = "NPTR Chairperson"; graphics.DrawString(stringToPrint, _normal, solidBrush, 50, _yCoord); stringToPrint = "NPTR Secretary"; graphics.DrawString(stringToPrint, _normal, solidBrush, 550, _yCoord); IncrementYCoord(100); stringToPrint = "This certificate is issued without any alterations or erasure of any kind."; graphics.DrawString(stringToPrint, _normal, solidBrush, 50, _yCoord); #region Test Watermark if (_isTest) { graphics.TranslateTransform(150, 75); graphics.RotateTransform(e.PageSettings.Landscape ? 30 : 45); graphics.DrawString("TEST PAGE", new Font("Arial", 100, FontStyle.Bold), new SolidBrush(Color.FromArgb(64, Color.Black)), 0, 0); } #endregion } #endregion #region Get2LineDisplayText private static string Get2LineDisplayText(string original) { //Degenerate case with only 1 word if (!original.Any(Char.IsWhiteSpace)) { return original; } var mid = original.Length / 2; if (!Char.IsWhiteSpace(original[mid])) { for (var i = 1; i < mid; i += i) { if (Char.IsWhiteSpace(original[mid + i])) { mid = mid + i; break; } if (!Char.IsWhiteSpace(original[mid - i])) continue; mid = mid - i; break; } } return original.Substring(0, mid) + "," + original.Substring(mid + 1); } #endregion } }