420 lines
22 KiB
C#
420 lines
22 KiB
C#
using CordonelPreadjustmentUi.Const;
|
|
using PdfSharp.Drawing;
|
|
using PdfSharp.Drawing.Layout;
|
|
using PdfSharp.Pdf;
|
|
using System;
|
|
using System.Drawing;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Xylem.Common.Ui.CordonelPreadjustmentUi
|
|
{
|
|
public static class GenerateReport
|
|
{
|
|
private static class ReportParams
|
|
{
|
|
public const Int32 LogoXPos = 500;
|
|
public const Int32 LogoYPos = 10;
|
|
public const Int32 LogoXSize = 50;
|
|
public const Int32 LogoYSize = 25;
|
|
public const Int32 HeadlineXPos = 0;
|
|
public const Int32 HeadlineYPos = 50;
|
|
public const Int32 Headline2XPos = 128;
|
|
public const Int32 Headline2YPos = 113;
|
|
public const Int32 PageNumberXPosition = 220;
|
|
public const Int32 PageNumberYPosition = -20;
|
|
public const Int32 StandardTextXPos = 128;
|
|
public const Int32 StandardTextYPos = 150;
|
|
public const Int32 ImageXSize = 400;
|
|
public const Int32 ImageYSize = 240;
|
|
public const Int32 ImageXPos = 125;
|
|
public const Int32 ImageYPos = 135;
|
|
public const Int32 IntroductionXPos = 100;
|
|
public const Int32 IntroductionYPos = 600;
|
|
public static readonly XFont font20 = new XFont("Eras Bold ITC ", 20, XFontStyle.Bold);
|
|
public static readonly XFont font14 = new XFont("Calibri", 14, XFontStyle.Bold);
|
|
public static readonly XFont font12 = new XFont("Calibri", 12, XFontStyle.BoldItalic);
|
|
public static readonly XFont fontStandard = new XFont("Calibri", 10, XFontStyle.Regular);
|
|
public static readonly XFont fontSmall = new XFont("Calibri", 9, XFontStyle.Regular);
|
|
public static readonly XFont fontVerySmall = new XFont("Calibri", 8, XFontStyle.Regular);
|
|
public const Int32 Column1 = 0;
|
|
public const Int32 Column2 = 1;
|
|
public const Int32 Column3 = 2;
|
|
public const Int32 Column4 = 3;
|
|
public const Int32 Column5 = 4;
|
|
}
|
|
public static void GenerateReturnNote(int meterIndex, String pcbID, String reason, String logData, CultureInfo culture, Image Logo)
|
|
{
|
|
String UserName = Environment.UserName;
|
|
DateTime Date = DateTime.Now;
|
|
String Content = String.Empty;
|
|
String Header = String.Empty;
|
|
String Filename = String.Empty;
|
|
String Headline = String.Empty;
|
|
|
|
try
|
|
{
|
|
if (culture == CultureInfo.CreateSpecificCulture("de-DE"))
|
|
{
|
|
Headline = "Rückläuferschein";
|
|
Header = $"ZÄHLER{meterIndex} fehlgeschlagen.\nLeiterplattennummer:\t{pcbID}\nGrund:\t{reason}\nDatum:\t{Date.ToString(Formats.HeaderFileDateTimeString)}\nBenutzername:\t{UserName}\n";
|
|
Filename = $"Rückläuferschein_ZÄHLER{meterIndex}_{Date.ToString(Formats.LogFileDateTimeString, culture)}.pdf";
|
|
}
|
|
else
|
|
{
|
|
Headline = "Return note";
|
|
Header = $"METER{meterIndex} failed.\nPCB-ID\t{pcbID}\nReason:\t{reason}\nDate:\t{Date.ToString(Formats.HeaderFileDateTimeString)}\nUser name:\t{UserName}\n";
|
|
Filename = $"Return note_METER{meterIndex}_{Date.ToString(Formats.LogFileDateTimeString, culture)}.pdf";
|
|
}
|
|
|
|
using (PdfDocument Report = new PdfDocument())
|
|
{
|
|
MemoryStream strm = new MemoryStream();
|
|
Logo.Save(strm, System.Drawing.Imaging.ImageFormat.Png);
|
|
|
|
XImage xLogo = XImage.FromStream(strm);
|
|
|
|
ReportPages.ReturnNote(Headline, $"{Header}\n\n{logData}", Report, xLogo);
|
|
SavePdf(Report, $@"..\{Filename}", culture);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
#region Error
|
|
MessageBox.Show("Return note generation failed", "Form closing", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
#endregion
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public static Boolean SavePdf(PdfDocument document, String fullpath, CultureInfo culture)
|
|
{
|
|
Boolean SavingOK = false;
|
|
try
|
|
{
|
|
var t = new Task(() =>
|
|
{
|
|
document.Save(@fullpath);
|
|
System.Diagnostics.Process.Start(@fullpath);
|
|
});
|
|
t.Start();
|
|
SavingOK = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
//ErrorLog.Log(LogErrReason.Exeption, "Files.SavePdf() failed", $"0x{ex.HResult.ToString("X", culture)}", String.Empty, culture);
|
|
}
|
|
|
|
return SavingOK;
|
|
}
|
|
|
|
public static class ReportPages
|
|
{
|
|
public static void Logfile(String text, PdfDocument report, XImage logo)
|
|
{
|
|
#region CA1062
|
|
if ((text == null) || (report == null))
|
|
{
|
|
return;
|
|
}
|
|
#endregion
|
|
|
|
const Int32 LinesPerPage = 60;
|
|
String[] Lines = text.Split('\n');
|
|
Double NumberOfNewlines = (double)Lines.Count();
|
|
Double aux = (NumberOfNewlines / LinesPerPage);
|
|
Int32 NumberOfPages = (Int32)Math.Ceiling(aux);
|
|
String[] StringArray = new String[NumberOfPages];
|
|
|
|
for (Int32 i = 0; i < (NumberOfNewlines); i++)
|
|
{
|
|
Int32 Pageindex = (Int32)Math.Floor(((Double)i / LinesPerPage));
|
|
StringArray[Pageindex] = StringArray[Pageindex] + Lines[i] + "\n";
|
|
}
|
|
|
|
//formatter.DrawString(text, ReportParams.fontStandard, XBrushes.Black, new XRect(50, 100, 0, 0), XStringFormats.TopLeft);
|
|
//Logfile.DrawString(text, ReportParams.fontStandard, XBrushes.Black, new XRect(10, 20, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
|
|
|
|
for (Int32 i = 0; i < NumberOfPages; i++)
|
|
{
|
|
PdfPage pdfPage = report.AddPage();
|
|
XGraphics Logfile = XGraphics.FromPdfPage(pdfPage);
|
|
var formatter = new XTextFormatter(Logfile);
|
|
var layoutRectangle = new XRect(1, 1, 1, 1);
|
|
Logfile.DrawImage(logo, new XRect(ReportParams.LogoXPos, ReportParams.LogoYPos, ReportParams.LogoXSize, ReportParams.LogoYSize));
|
|
formatter.DrawString(StringArray[i], ReportParams.fontSmall, XBrushes.Black, new XRect(50, 70, 600, 20), XStringFormats.TopLeft);
|
|
}
|
|
|
|
}
|
|
public static UInt16 TitlePage(UInt16 pagecounter, String introduction, String title, String date, String operatorName, String serialNo, PdfDocument report, XImage logo, CultureInfo culture)
|
|
{
|
|
#region CA1062
|
|
if (report == null)
|
|
{
|
|
return 0;
|
|
}
|
|
#endregion
|
|
try
|
|
{
|
|
#region Page "TITLEPAGE"
|
|
#region Prepare data
|
|
if (pagecounter < UInt16.MaxValue)
|
|
{
|
|
pagecounter++; // Pagenumber not on titlepage
|
|
}
|
|
|
|
report.Info.Title = title;
|
|
PdfPage pdfPage = report.AddPage();
|
|
XGraphics Titlepage = XGraphics.FromPdfPage(pdfPage);
|
|
var formatter = new XTextFormatter(Titlepage);
|
|
var layoutRectangle = new XRect(1, 1, 1, 1);
|
|
Titlepage.DrawImage(logo, new XRect(ReportParams.LogoXPos, ReportParams.LogoYPos, ReportParams.LogoXSize, ReportParams.LogoYSize));
|
|
Titlepage.DrawString($"{title}\n\n\n", ReportParams.font20, XBrushes.Black, new XRect(0, -200, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.Center);
|
|
Titlepage.DrawString(date, ReportParams.font14, XBrushes.Black, new XRect(0, -100, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.Center);
|
|
Titlepage.DrawString("by", ReportParams.font12, XBrushes.Black, new XRect(0, -50, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.Center);
|
|
Titlepage.DrawString(operatorName, ReportParams.font14, XBrushes.Black, new XRect(0, 0, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.Center);
|
|
Titlepage.DrawString($"Serial number: {serialNo}", ReportParams.font14, XBrushes.Black, new XRect(0, 100, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.Center);
|
|
formatter.DrawString(introduction, ReportParams.fontStandard, XBrushes.Black, new XRect(ReportParams.IntroductionXPos, ReportParams.IntroductionYPos, 400, 200), XStringFormats.TopLeft);
|
|
#endregion
|
|
#endregion
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
#region Error
|
|
//ErrorLog.Log(LogErrReason.Exeption, "ReportPages.TitlePage() failed", $"0x{ex.HResult.ToString("X", culture)}", String.Empty, culture);
|
|
#endregion
|
|
}
|
|
|
|
#region Return value
|
|
return pagecounter;
|
|
#endregion
|
|
}
|
|
|
|
public static void ReturnNote(String headline, String text, PdfDocument report, XImage logo)
|
|
{
|
|
#region CA1062
|
|
if ((text == null) || (report == null))
|
|
{
|
|
return;
|
|
}
|
|
#endregion
|
|
const Int32 LinesPerPage = 70;
|
|
String[] Lines = text.Split('\n');
|
|
Double NumberOfNewlines = (Double)Lines.Count();
|
|
Double aux = (NumberOfNewlines / LinesPerPage);
|
|
Int32 NumberOfPages = (Int32)Math.Ceiling(aux);
|
|
String[] StringArray = new String[NumberOfPages];
|
|
|
|
for (Int32 i = 0; i < (NumberOfNewlines); i++)
|
|
{
|
|
Int32 Pageindex = (Int32)Math.Floor(((Double)i / LinesPerPage));
|
|
StringArray[Pageindex] = $"{StringArray[Pageindex]}{Lines[i]}\n";
|
|
}
|
|
|
|
//formatter.DrawString(text, ReportParams.fontStandard, XBrushes.Black, new XRect(50, 100, 0, 0), XStringFormats.TopLeft);
|
|
//Logfile.DrawString(text, ReportParams.fontStandard, XBrushes.Black, new XRect(10, 20, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
|
|
|
|
for (Int32 i = 0; i < NumberOfPages; i++)
|
|
{
|
|
PdfPage pdfPage = report.AddPage();
|
|
XGraphics Logfile = XGraphics.FromPdfPage(pdfPage);
|
|
var formatter = new XTextFormatter(Logfile);
|
|
var layoutRectangle = new XRect(1, 1, 1, 1);
|
|
Logfile.DrawImage(logo, new XRect(ReportParams.LogoXPos, ReportParams.LogoYPos, ReportParams.LogoXSize, ReportParams.LogoYSize));
|
|
if (i == 0)
|
|
{
|
|
formatter.DrawString(headline, ReportParams.font20, XBrushes.Black, new XRect(250, 20, 600, 20), XStringFormats.TopLeft);
|
|
formatter.DrawString(StringArray[i], ReportParams.fontVerySmall, XBrushes.Black, new XRect(60, 50, 600, pdfPage.Height.Point - 30), XStringFormats.TopLeft);
|
|
}
|
|
else
|
|
{
|
|
formatter.DrawString(StringArray[i], ReportParams.fontVerySmall, XBrushes.Black, new XRect(50, 70, 600, pdfPage.Height.Point - 10), XStringFormats.TopLeft);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public static Int32 Settings(Int32 pagecounter, String textSoftwareVersion, String[] columns, Boolean enable, PdfDocument report, XImage logo, CultureInfo culture)
|
|
{
|
|
#region CA1062
|
|
if ((report == null) || (columns == null))
|
|
{
|
|
return 0;
|
|
}
|
|
#endregion
|
|
try
|
|
{
|
|
#region Page "SETTINGS"
|
|
#region Generate page
|
|
if (pagecounter < Int32.MaxValue)
|
|
{
|
|
pagecounter++;
|
|
}
|
|
|
|
PdfPage pdfPage = report.AddPage();
|
|
XGraphics Gp30Settings = XGraphics.FromPdfPage(pdfPage);
|
|
var formatterGp30Settings = new XTextFormatter(Gp30Settings);
|
|
var layoutRectangleGp30Settings = new XRect(1, 1, 1, 1);
|
|
#endregion
|
|
#region Set Image/ Logo
|
|
Gp30Settings.DrawImage(logo, new XRect(ReportParams.LogoXPos, ReportParams.LogoYPos, ReportParams.LogoXSize, ReportParams.LogoYSize));
|
|
#endregion
|
|
#region Headline1
|
|
Gp30Settings.DrawString("Settings", ReportParams.font20, XBrushes.Black, new XRect(ReportParams.HeadlineXPos, ReportParams.HeadlineYPos, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopCenter);
|
|
#endregion
|
|
#region Headline2 (first)
|
|
Gp30Settings.DrawString("Software Version", ReportParams.font12, XBrushes.Black, new XRect(ReportParams.Headline2XPos, ReportParams.Headline2YPos, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
|
|
#endregion
|
|
#region Data
|
|
formatterGp30Settings.DrawString(textSoftwareVersion, ReportParams.fontStandard, XBrushes.Black, new XRect(ReportParams.StandardTextXPos, ReportParams.StandardTextYPos, 400, 200), XStringFormats.TopLeft);
|
|
#endregion
|
|
#region GP30 settings
|
|
if (enable)
|
|
{
|
|
#region Headline2
|
|
Gp30Settings.DrawString("Values obtained from register readout", ReportParams.font12, XBrushes.Black, new XRect(ReportParams.Headline2XPos, ReportParams.Headline2YPos + 150, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
|
|
#endregion
|
|
#region Data/ text
|
|
formatterGp30Settings.DrawString(columns[ReportParams.Column1], ReportParams.fontStandard, XBrushes.Black, new XRect(ReportParams.StandardTextXPos, ReportParams.StandardTextYPos + 150, 400, 200), XStringFormats.TopLeft);
|
|
formatterGp30Settings.DrawString(columns[ReportParams.Column2], ReportParams.fontStandard, XBrushes.Black, new XRect(ReportParams.StandardTextXPos + 140, ReportParams.StandardTextYPos + 150, 400, 200), XStringFormats.TopLeft);
|
|
formatterGp30Settings.DrawString(columns[ReportParams.Column3], ReportParams.fontStandard, XBrushes.Black, new XRect(ReportParams.StandardTextXPos + 190, ReportParams.StandardTextYPos + 150, 400, 200), XStringFormats.TopLeft);
|
|
#endregion
|
|
}
|
|
#endregion
|
|
#region Set page number
|
|
Gp30Settings.DrawString($"Page {pagecounter.ToString(culture)}", ReportParams.font12, XBrushes.Black, new XRect(ReportParams.PageNumberXPosition, ReportParams.PageNumberYPosition, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.BottomCenter);
|
|
#endregion
|
|
#endregion
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
#region Error
|
|
//ErrorLog.Log(LogErrReason.Exeption, "ReportPages.Settings() failed", $"0x{ex.HResult.ToString("X", culture)}", String.Empty, culture);
|
|
#endregion
|
|
}
|
|
|
|
#region Return values
|
|
return pagecounter;
|
|
#endregion
|
|
}
|
|
|
|
public static UInt16 DecodedData(UInt16 pagecounter, String[] columns, Boolean enable, Boolean enableColumn2, Boolean enableColumn3, Boolean enableColumn4, PdfDocument report, XImage logo, CultureInfo culture)
|
|
{
|
|
const Int32 WaitingTime = 100;
|
|
|
|
#region CA1062
|
|
if ((report == null) || (columns == null))
|
|
{
|
|
return 0;
|
|
}
|
|
#endregion
|
|
|
|
try
|
|
{
|
|
#region Page "MEAN VALUES"
|
|
if ((enable) && ((enableColumn2) || (enableColumn3) || (enableColumn4)))
|
|
{
|
|
#region All PATHs
|
|
#region Prepare Data
|
|
if (pagecounter < UInt16.MaxValue)
|
|
{
|
|
pagecounter++;
|
|
}
|
|
|
|
PdfPage pdfPage = report.AddPage();
|
|
XGraphics XMean = XGraphics.FromPdfPage(pdfPage);
|
|
var formatterData = new XTextFormatter(XMean);
|
|
var layoutRectangleData = new XRect(1, 1, 1, 1);
|
|
int Xoffset = -20;
|
|
#endregion
|
|
#region Headline1
|
|
XMean.DrawString("Decoded Data", ReportParams.font20, XBrushes.Black, new XRect(ReportParams.HeadlineXPos, ReportParams.HeadlineYPos, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopCenter);
|
|
#endregion
|
|
#region Set text (First column)
|
|
if (!enableColumn2)
|
|
{
|
|
Xoffset = Xoffset + 40;
|
|
}
|
|
|
|
if (!enableColumn3)
|
|
{
|
|
Xoffset = Xoffset + 40;
|
|
}
|
|
|
|
if (!enableColumn4)
|
|
{
|
|
Xoffset = Xoffset + 40;
|
|
}
|
|
|
|
formatterData.DrawString(columns[ReportParams.Column1], ReportParams.fontStandard, XBrushes.Black, new XRect(ReportParams.StandardTextXPos + Xoffset, ReportParams.StandardTextYPos, 400, 200), XStringFormats.TopLeft);
|
|
Xoffset = Xoffset + 120;
|
|
#endregion
|
|
#region Set Image/Logo
|
|
XMean.DrawImage(logo, new XRect(ReportParams.LogoXPos, ReportParams.LogoYPos, ReportParams.LogoXSize, ReportParams.LogoYSize));
|
|
#endregion
|
|
#region Add pagenumber
|
|
XMean.DrawString("Page " + pagecounter.ToString(culture), ReportParams.font12, XBrushes.Black, new XRect(ReportParams.PageNumberXPosition, ReportParams.PageNumberYPosition, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.BottomCenter);
|
|
#endregion
|
|
#endregion
|
|
#region PATH0
|
|
if (enableColumn2)
|
|
{
|
|
Thread.Sleep(WaitingTime);
|
|
#region Headline2
|
|
XMean.DrawString("PATH1", ReportParams.font12, XBrushes.Black, new XRect(ReportParams.Headline2XPos + Xoffset, ReportParams.Headline2YPos + 20, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
|
|
#endregion
|
|
#region Set text
|
|
formatterData.DrawString(columns[ReportParams.Column2], ReportParams.fontStandard, XBrushes.Black, new XRect(ReportParams.StandardTextXPos + Xoffset, ReportParams.StandardTextYPos, 400, 200), XStringFormats.TopLeft);
|
|
Xoffset = Xoffset + 110;
|
|
#endregion
|
|
}
|
|
#endregion
|
|
#region PATH1
|
|
if (enableColumn3)
|
|
{
|
|
Thread.Sleep(WaitingTime);
|
|
#region Headline2
|
|
XMean.DrawString("PATH2", ReportParams.font12, XBrushes.Black, new XRect(ReportParams.Headline2XPos + Xoffset, ReportParams.Headline2YPos + 20, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
|
|
#endregion
|
|
#region Set text
|
|
formatterData.DrawString(columns[ReportParams.Column3], ReportParams.fontStandard, XBrushes.Black, new XRect(ReportParams.StandardTextXPos + Xoffset, ReportParams.StandardTextYPos, 400, 200), XStringFormats.TopLeft);
|
|
Xoffset = Xoffset + 110;
|
|
#endregion
|
|
}
|
|
#endregion
|
|
#region PATH2
|
|
if (enableColumn4)
|
|
{
|
|
Thread.Sleep(WaitingTime);
|
|
#region Headline2
|
|
XMean.DrawString("PATH3", ReportParams.font12, XBrushes.Black, new XRect(ReportParams.Headline2XPos + Xoffset, ReportParams.Headline2YPos + 20, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
|
|
#endregion
|
|
#region Set text
|
|
formatterData.DrawString(columns[ReportParams.Column4], ReportParams.fontStandard, XBrushes.Black, new XRect(ReportParams.StandardTextXPos + Xoffset, ReportParams.StandardTextYPos, 400, 200), XStringFormats.TopLeft);
|
|
#endregion
|
|
}
|
|
#endregion
|
|
}
|
|
#endregion
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
#region Error
|
|
//ErrorLog.Log(LogErrReason.Exeption, "ReportPages.DecodedData() failed", $"0x{ex.HResult.ToString("X", culture)}", String.Empty, culture);
|
|
#endregion
|
|
}
|
|
|
|
#region Return values
|
|
return pagecounter;
|
|
#endregion
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|