/// /// Copyright (c) 2016 Sensus Metering Systems /// using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Printing; using Config.Entities; namespace ResultsBrowser.Output.Printers.QueryResults { public class QueryResultsPrintDocument : PrintDocument { const string FontFamilyName = "Arial"; //"Times New Roman"; const int SpacingOne = 18; const int SpacingOneAndHalf = (3 * SpacingOne) / 2; const int TitleX = 240; const int TitleY = 180; /// Depends on the size of the header const int FirstLineNr = 14; /// Depends on the size of the header const int Tab1 = 80; /// Positions of columns above the table const int Tab2 = 240; const int Tab3 = 430; const int Tab4 = 590; const int BorderW = 1; /// Table border line width const int MarginTop = 7; /// Margins of the text from table lines const int MarginLeft = 4; /// Margins of the text from table lines /// /// Data to print /// readonly string docVersion; readonly Results.Entities.WaterMeter wm; string zaehlertyp; string pruefvorlage; readonly IList results; readonly int resultsCount; readonly bool compound; /// true when MetersKind is MetersKind.Combined readonly int secondTableOffset; /// Calculated in the constructor /// Top coordinate of the table int top; /// /// Property variable for the Font the user wishes to use /// Font font; Font boldFont; Font fontTitle; /// /// Constructor /// /// Text to be printed public QueryResultsPrintDocument(string docVersion, Results.Entities.WaterMeter wm, PageOrientation pageOrientation) { this.docVersion = docVersion; this.wm = wm; compound = wm.Compound(); string[] info = wm.ProductName().Split(new char[] { ';' }); if (info.Length >= 2) { pruefvorlage = info[0]; zaehlertyp = info[1]; } else if (info.Length == 1) { pruefvorlage = info[0]; zaehlertyp = compound ? "Verbundzähler" : "Einzelzähler"; } else { pruefvorlage = wm.ProcedureName(); zaehlertyp = compound ? "Verbundzähler" : "Einzelzähler"; } results = wm.MeterTestRslts; this.resultsCount = compound ? (results.Count / 3) : results.Count; int resultsCount = wm.MeterTestRslts.Count; if (compound) resultsCount /= 3; secondTableOffset = (resultsCount + 2) * SpacingOneAndHalf; DefaultPageSettings.Landscape = (pageOrientation == PageOrientation.Landscape); } /// /// Override the default onbeginPrint method of the PrintDocument Object. /// /// /// protected override void OnBeginPrint(System.Drawing.Printing.PrintEventArgs e) { base.OnBeginPrint(e); if (font == null) { font = new Font(FontFamilyName, 10, FontStyle.Regular); } if (boldFont == null) { boldFont = new Font(FontFamilyName, 10, FontStyle.Bold); } if (fontTitle == null) { fontTitle = new Font(FontFamilyName, 24, FontStyle.Bold); } } /// /// Override the default OnPrintPage method of the PrintDocument. /// /// /// This provides the print logic for our document protected override void OnPrintPage(System.Drawing.Printing.PrintPageEventArgs e) { // Run base code base.OnPrintPage(e); /// Set print area size and margins (in dots using 80 dpi) int printHeight = base.DefaultPageSettings.PaperSize.Height - base.DefaultPageSettings.Margins.Top - base.DefaultPageSettings.Margins.Bottom; int printWidth = base.DefaultPageSettings.PaperSize.Width - base.DefaultPageSettings.Margins.Left - base.DefaultPageSettings.Margins.Right; int leftMargin = base.DefaultPageSettings.Margins.Left; /// X int topMargin = base.DefaultPageSettings.Margins.Top; /// Y /// Check if the user selected to print in Landscape mode /// if they did then we need to swap height/width parameters if (base.DefaultPageSettings.Landscape) { int tmp = printHeight; printHeight = printWidth; printWidth = tmp; } int x = leftMargin; int y = topMargin; /// Use the StringFormat class for the text layout of our document StringFormat format = new StringFormat(StringFormatFlags.LineLimit); RectangleF printArea = new RectangleF(TitleX, TitleY, 667, 50); e.Graphics.DrawString(wm.Batch.ProtocolTitle, this.fontTitle, Brushes.Black, printArea); int lineNr = FirstLineNr; PrintAt(e, Tab1, lineNr * SpacingOne, docVersion); lineNr++; PrintBoldAt(e, Tab1, lineNr * SpacingOne, "Prüfstand Nr :"); PrintBoldAt(e, Tab2, lineNr * SpacingOne, wm.BenchId().ToString()); PrintBoldAt(e, Tab3, lineNr * SpacingOne, "Datum :"); PrintBoldAt(e, Tab4, lineNr * SpacingOne, wm.Batch.EndTime.Date.ToString("d")); lineNr++; PrintAt(e, Tab1, lineNr * SpacingOne, "Zählertyp :"); PrintAt(e, Tab2, lineNr * SpacingOne, zaehlertyp); PrintAt(e, Tab3, lineNr * SpacingOne, "Eichjahr :"); PrintAt(e, Tab4, lineNr * SpacingOne, wm.YearOfProduction.ToString()); lineNr++; PrintAt(e, Tab1, lineNr * SpacingOne, "Prüfvorlage :"); PrintAt(e, Tab2, lineNr * SpacingOne, pruefvorlage); lineNr += 2; if (compound) { PrintBoldAt(e, Tab1, lineNr * SpacingOne, "Hauptzahler"); PrintBoldAt(e, Tab3, lineNr * SpacingOne, "Nebenzahler"); lineNr++; } PrintAt(e, Tab1, lineNr * SpacingOne, "Hersteller :"); PrintAt(e, Tab2, lineNr * SpacingOne, wm.Producer()); if (compound) { PrintAt(e, Tab3, lineNr * SpacingOne, "Hersteller :"); PrintAt(e, Tab4, lineNr * SpacingOne, wm.ProducerAux()); } lineNr++; PrintAt(e, Tab1, lineNr * SpacingOne, "Seriennummer :"); PrintAt(e, Tab2, lineNr * SpacingOne, wm.SerialNr); if (compound) { PrintAt(e, Tab3, lineNr * SpacingOne, "Seriennummer :"); PrintAt(e, Tab4, lineNr * SpacingOne, wm.SerialNrAux); } lineNr++; PrintAt(e, Tab1, lineNr * SpacingOne, "Qn :"); PrintAt(e, Tab2, lineNr * SpacingOne, wm.Q3_Qn().ToString()); if (compound) { PrintAt(e, Tab3, lineNr * SpacingOne, "Qn :"); PrintAt(e, Tab4, lineNr * SpacingOne, wm.Q3_Qn_Aux().ToString()); } lineNr++; PrintAt(e, Tab1, lineNr * SpacingOne, "Zulassungszeichen :"); PrintAt(e, Tab2, lineNr * SpacingOne, wm.ApprovalInfo()); if (compound) { PrintAt(e, Tab3, lineNr * SpacingOne, "Zulassungszeichen :"); PrintAt(e, Tab4, lineNr * SpacingOne, wm.ApprovalInfoAux()); } lineNr++; PrintAt(e, Tab1, lineNr * SpacingOne, "Metr. klasse :"); PrintAt(e, Tab2, lineNr * SpacingOne, wm.MetrologicalClass()); if (compound) { PrintAt(e, Tab3, lineNr * SpacingOne, "Metr. klasse :"); PrintAt(e, Tab4, lineNr * SpacingOne, wm.MetrologicalClassAux()); } lineNr++; PrintAt(e, Tab1, lineNr * SpacingOne, "Stand :"); PrintAt(e, Tab2, lineNr * SpacingOne, wm.EndState); if (compound) { PrintAt(e, Tab3, lineNr * SpacingOne, "Stand :"); PrintAt(e, Tab4, lineNr * SpacingOne, wm.EndStateAux); lineNr += 2; PrintAt(e, Tab1, lineNr * SpacingOne, "Umschaltpunkte :"); lineNr++; PrintAt(e, Tab1, lineNr * SpacingOne, "Qsteig"); PrintAt(e, Tab2, lineNr * SpacingOne, (1000.0 * wm.QRise).ToString("F1") + " l/h"); lineNr++; PrintAt(e, Tab1, lineNr * SpacingOne, "Qfall"); PrintAt(e, Tab2, lineNr * SpacingOne, (1000.0 * wm.QFall).ToString("F1") + " l/h"); } lineNr += 2; top = SpacingOne * lineNr; PrintTable(e); DrawTable(e); lineNr += (secondTableOffset * 2) / SpacingOne; PrintAt(e, Tab1, lineNr * SpacingOne, "Temperatur"); PrintAt(e, Tab2, lineNr * SpacingOne, wm.Batch.AmbTempMean().ToString("F1") + " \x00b0C"); lineNr++; PrintAt(e, Tab1, lineNr * SpacingOne, "Feuchtigkeit"); PrintAt(e, Tab2, lineNr * SpacingOne, wm.Batch.AmbHumiMean().ToString("F0") + " %"); lineNr++; PrintAt(e, Tab1, lineNr * SpacingOne, "Luftdruck"); PrintAt(e, Tab2, lineNr * SpacingOne, wm.Batch.AmbPressMean().ToString("F0") + " mbar"); lineNr++; PrintAt(e, Tab3, lineNr * SpacingOne, wm.Batch.EndTime.Date.ToString("d")); lineNr ++; PrintAt(e, Tab3, lineNr * SpacingOne, "Datum"); PrintAt(e, Tab4, lineNr * SpacingOne, "Unterschrift"); } /// /// Print table texts. /// void PrintTable(System.Drawing.Printing.PrintPageEventArgs e) { PrintAt(e, FromTest(-1, -1), "Prüflauf"); PrintAt(e, FromTest(-1, 0), "Q [l/h]"); PrintAt(e, FromTest(-1, 1), "Prüf-Vol.[l]"); PrintAt(e, FromTest(-1, 2), compound ? "HZ-Vol.[l]" : "Z-Vol.[l]"); PrintAt(e, FromTest(-1, 3), compound ? "NZ-Vol.[l]" : "-"); PrintAt(e, FromTest(-1, 4), "Fehler [%]"); PrintAt(e, FromTest(-1, 5), "Ergebnis"); PrintAt(e, FromTest(-1, 6), "Q [l/h]"); PrintAt(e, FromTest(-1, 7), "Temperatur"); PrintAt(e, FromTest(-1, 8), "Dichte"); PrintAt(e, FromTest(-1, 9), "Prüfzeit"); PrintAt(e, FromTest(-1, 10), "Druck Eing."); PrintAt(e, FromTest(-1, 11), "Druck Ausg."); int rowCnt = 0; foreach (var mtr in wm.MeterTestRslts) { if (!compound || mtr.CompoundMeterId == (byte)CompoundMeterId.Compound) { Point p = FromTest(rowCnt, -1); PrintAt(e, p.X, p.Y, mtr.Name()); PrintAt(e, p.X, p.Y + (resultsCount + 2) * SpacingOneAndHalf, mtr.Name()); PrintAt(e, FromTest(rowCnt, 0), mtr.TestRslt.FlowMass.ToString("F1")); PrintAt(e, FromTest(rowCnt, 1), mtr.TestRslt.VolumeCTV.ToString("F2")); if (compound) { Results.Entities.MeterTestRslt mainMtr = wm.GetMeterTestRslt(mtr.Name(), CompoundMeterId.CompoundMain); Results.Entities.MeterTestRslt auxMtr = wm.GetMeterTestRslt(mtr.Name(), CompoundMeterId.CompoundAux); PrintAt(e, FromTest(rowCnt, 2), mainMtr.VolumeMeter.ToString("F2")); PrintAt(e, FromTest(rowCnt, 3), auxMtr.VolumeMeter.ToString("F2")); PrintAt(e, FromTest(rowCnt, 4), mtr.Error.ToString("F2")); PrintAt(e, FromTest(rowCnt, 5), mtr.Passed ? "iO" : "NiO"); } else { PrintAt(e, FromTest(rowCnt, 2), mtr.VolumeMeter.ToString("F2")); PrintAt(e, FromTest(rowCnt, 3), "-"); PrintAt(e, FromTest(rowCnt, 4), mtr.Error.ToString("F2")); PrintAt(e, FromTest(rowCnt, 5), mtr.Passed ? "iO" : "NiO"); } PrintAt(e, FromTest(rowCnt, 6), mtr.TestRslt.FlowMass.ToString("F1")); PrintAt(e, FromTest(rowCnt, 7), mtr.TestRslt.TempDownMean.ToString("F2")); PrintAt(e, FromTest(rowCnt, 8), mtr.TestRslt.DensityDiv.ToString("F2")); PrintAt(e, FromTest(rowCnt, 9), mtr.TestTime.ToString("F1")); PrintAt(e, FromTest(rowCnt, 10), Config.Units.ConvertTo(Config.Unit.bar, mtr.TestRslt.PressUpMean).ToString("F3")); PrintAt(e, FromTest(rowCnt, 11), Config.Units.ConvertTo(Config.Unit.bar, mtr.TestRslt.PressDownMean).ToString("F3")); rowCnt++; } } } /// /// Draw table lines. /// void DrawTable(System.Drawing.Printing.PrintPageEventArgs e) { int mid = top + SpacingOneAndHalf; int bot = top + (resultsCount + 1) * SpacingOneAndHalf; int top2 = top + secondTableOffset; int mid2 = mid + secondTableOffset; int bot2 = bot + secondTableOffset; // Horizontal lines e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(80, top), new Point(747, top)); e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(80, mid), new Point(747, mid)); e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(80, bot), new Point(747, bot)); e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(80, top2), new Point(747, top2)); e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(80, mid2), new Point(747, mid2)); e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(80, bot2), new Point(747, bot2)); // Vertical lines e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point( 80, top), new Point( 80, bot)); e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(267, top), new Point(267, bot)); e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(347, top), new Point(347, bot)); e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(427, top), new Point(427, bot)); e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(507, top), new Point(507, bot)); e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(587, top), new Point(587, bot)); e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(667, top), new Point(667, bot)); e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(747, top), new Point(747, bot)); e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point( 80, top2), new Point( 80, bot2)); e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(267, top2), new Point(267, bot2)); e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(347, top2), new Point(347, bot2)); e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(427, top2), new Point(427, bot2)); e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(507, top2), new Point(507, bot2)); e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(587, top2), new Point(587, bot2)); e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(667, top2), new Point(667, bot2)); e.Graphics.DrawLine(new Pen(Color.Black, BorderW), new Point(747, top2), new Point(747, bot2)); } void PrintAt(System.Drawing.Printing.PrintPageEventArgs e, Point p, string text) { PrintAt(e, p.X, p.Y, text); } void PrintAt(System.Drawing.Printing.PrintPageEventArgs e, int x, int y, string text) { RectangleF printArea = new RectangleF(x, y, 667, SpacingOne); e.Graphics.DrawString(text, this.font, Brushes.Black, printArea); } void PrintBoldAt(System.Drawing.Printing.PrintPageEventArgs e, Point p, string text) { PrintBoldAt(e, p.X, p.Y, text); } void PrintBoldAt(System.Drawing.Printing.PrintPageEventArgs e, int x, int y, string text) { RectangleF printArea = new RectangleF(x, y, 667, SpacingOne); e.Graphics.DrawString(text, this.boldFont, Brushes.Black, printArea); } Point FromTest(int testNr, int parNr) { const int NrParamsInOneTableRow = 6; int x = 267 + (parNr % NrParamsInOneTableRow) * 80 + MarginLeft; int y = top + SpacingOneAndHalf + MarginTop + testNr * SpacingOneAndHalf + ((parNr >= NrParamsInOneTableRow) ? secondTableOffset : 0); if (parNr < 0) x = 80 + MarginLeft; /// When parNr == -1 return the left column x coordinate return new Point(x, y); } } }