using System; using System.Collections.Generic; using System.Text; using VEPROMS.CSLA.Library; using iTextSharp.text.pdf; using iTextSharp.text; using System.IO; using System.Text.RegularExpressions; using System.Windows.Forms; namespace Volian.Print.Library { public class PDFConsistencyCheckReport { private DocVersionInfo _MyDocVersion; public DocVersionInfo MyDocVersion { get { return _MyDocVersion; } set { _MyDocVersion = value; } } private string _FileName; public string FileName { get { return _FileName; } set { _FileName = value; } } private ItemInfoList _MyItemInfoList; public ItemInfoList MyItemInfoList { get { return _MyItemInfoList; } set { _MyItemInfoList = value; } } public PDFConsistencyCheckReport(string fileName, ItemInfoList myItemInfoList) { _FileName = fileName; _MyItemInfoList = myItemInfoList; } public void BuildReport() { iTextSharp.text.Document document = new iTextSharp.text.Document(PageSize.LETTER, 36, 36, 36, 36); if (!CreateResultsPDF(document)) return; try { BuildConsistencyReport(document); } catch (Exception ex) { StringBuilder msg = new StringBuilder(); document.Add(new Paragraph("Error:")); while (ex != null) { document.Add(new Paragraph(ex.GetType().Name)); document.Add(new Paragraph(ex.Message)); ex = ex.InnerException; } } finally { if (document.IsOpen()) { document.Close(); System.Diagnostics.Process.Start(_FileName); } } } private bool CreateResultsPDF(iTextSharp.text.Document document) { bool result = false; string suffix = ""; int i = 0; // Try to open a file for creating the PDF. while (result == false) { string fileName = _FileName.ToLower().Replace(".pdf", suffix + ".pdf"); try { PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create)); document.SetMargins(36, 36, 36, 36); document.Open(); _FileName = fileName; result = true; } catch (System.IO.IOException exIO) { if (exIO.Message.Contains("because it is being used by another process")) suffix = string.Format("_{0}", ++i);// If this file is in use, increment the suffix and try again else // If some other error, display a message and don't print the results { ShowException(exIO); return false; } } catch (Exception ex) { ShowException(ex); return false; // Could not open the output file } } return true; } private static void ShowException(Exception ex) { Console.WriteLine("{0} - {1}", ex.GetType().Name, ex.Message); StringBuilder msg = new StringBuilder(); string sep = ""; string indent = ""; while (ex != null) { msg.Append(string.Format("{0}{1}{2}:\r\n{1}{3}", sep, indent, ex.GetType().Name, ex.Message)); ex = ex.InnerException; sep = "\r\n"; indent += " "; } System.Windows.Forms.MessageBox.Show(msg.ToString(), "Error during PDF creation for search:", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation); } private PdfPCell BlankCell(int colSpan) { PdfPCell c = new PdfPCell(); c.Colspan = colSpan; c.Border = 0; return c; } private void BuildConsistencyReport(iTextSharp.text.Document doc) { iTextSharp.text.Font f1 = pdf.GetFont("Arial Unicode MS", 12, 1, Color.BLACK); iTextSharp.text.Font f2 = pdf.GetFont("Arial Unicode MS", 10, 0, Color.BLACK); iTextSharp.text.Font f3 = pdf.GetFont("Arial Unicode MS", 8, 0, Color.BLACK); iTextSharp.text.Font f4 = pdf.GetFont("Arial Unicode MS", 10, 2, Color.BLACK); int cols = 5; int borders = 0; int paddingBottom = 6; // float[] widths; PdfPTable t = CreatePdfPTable(cols); CreatePageHeader(t, f1, f4, cols, paddingBottom, "Approval Consistency Report", "Procedures currently selected to be approved"); //more stuff /* Csla.SortedBindingList sortedResults = new Csla.SortedBindingList(_SearchResults); sortedResults.ApplySort(_DisplayMember, ListSortDirection.Ascending); lbSrchResults.DataSource = sortedResults; */ Csla.SortedBindingList sortedProcs = new Csla.SortedBindingList(MyItemInfoList); sortedProcs.ApplySort("DisplayNumber", System.ComponentModel.ListSortDirection.Ascending); foreach (ProcedureInfo pi in sortedProcs) { if (pi.IsSelected) { PdfPTable tt = new PdfPTable(cols); tt.KeepTogether = true; tt.AddCell(AddSubTableCell(pi.DisplayNumber, f2, cols, paddingBottom)); tt.AddCell(AddSubTableCell(pi.MyDifferencesText, f3, cols, paddingBottom)); PdfPCell c = new PdfPCell(tt); c.Colspan = cols; t.AddCell(c); } } t.AddCell(BlankCell(cols)); doc.Add(t); //new page with new table doc.NewPage(); t = CreatePdfPTable(cols); CreatePageHeader(t, f1, f4, cols, paddingBottom, "Approval Consistency Report", "Procedures that should be selected to be approved in order to maintain consistency"); foreach (ProcedureInfo pi in MyItemInfoList) { if (!pi.IsSelected) { PdfPTable tt = new PdfPTable(cols); tt.KeepTogether = true; tt.AddCell(AddSubTableCell(pi.DisplayNumber, f2, cols, paddingBottom)); tt.AddCell(AddSubTableCell(pi.MyDifferencesText, f3, cols, paddingBottom)); PdfPCell c = new PdfPCell(tt); c.Colspan = cols; t.AddCell(c); } } doc.Add(t); } private PdfPCell AddSubTableCell(string text, iTextSharp.text.Font f, int cols, float pb) { Phrase h = new Phrase(); h.Font = f; h.Add(text); PdfPCell c = new PdfPCell(h); c.Colspan = cols; c.HorizontalAlignment = Element.ALIGN_LEFT; c.PaddingBottom = pb; return c; } private void CreatePageHeader(PdfPTable t, iTextSharp.text.Font f1, iTextSharp.text.Font f2, int cols, float pb, string hdr1, string hdr2) { //report title Phrase h = new Phrase(); h.Font = f1; h.Add(hdr1); PdfPCell c = new PdfPCell(h); c.Colspan = cols; c.HorizontalAlignment = Element.ALIGN_CENTER; c.PaddingBottom = pb; //c.Border = borders; t.AddCell(c); h = new Phrase(); h.Font = f2; h.Add(hdr2); c = new PdfPCell(h); c.Colspan = cols; c.HorizontalAlignment = Element.ALIGN_CENTER; c.PaddingBottom = pb; //c.Border = borders; t.AddCell(c); t.AddCell(BlankCell(cols)); } private PdfPTable CreatePdfPTable(int cols) { PdfPTable t = new PdfPTable(cols); t.HeaderRows = 3; t.DefaultCell.Padding = 4; t.WidthPercentage = 100; float[] widths = new float[] { 1f, 1f, 1f, 1f, 4f }; t.SetWidths(widths); //t.HorizontalAlignment = 0; return t; } public void BuildAllReport(DocVersionInfo dvi) { MyDocVersion = dvi; iTextSharp.text.Document document = new iTextSharp.text.Document(PageSize.LETTER, 36, 36, 36, 36); if (!CreateResultsPDF(document)) return; try { BuildAllInConsistenciesReport(document); } catch (Exception ex) { StringBuilder msg = new StringBuilder(); document.Add(new Paragraph("Error:")); while (ex != null) { document.Add(new Paragraph(ex.GetType().Name)); document.Add(new Paragraph(ex.Message)); ex = ex.InnerException; } } finally { if (document.IsOpen()) { document.Close(); System.Diagnostics.Process.Start(_FileName); } } } private void BuildAllInConsistenciesReport(iTextSharp.text.Document doc) { iTextSharp.text.Font f1 = pdf.GetFont("Arial Unicode MS", 12, 1, Color.BLACK); iTextSharp.text.Font f2 = pdf.GetFont("Arial Unicode MS", 10, 0, Color.BLACK); iTextSharp.text.Font f3 = pdf.GetFont("Arial Unicode MS", 8, 0, Color.BLACK); iTextSharp.text.Font f4 = pdf.GetFont("Arial Unicode MS", 10, 2, Color.BLACK); int cols = 3; int borders = 0; int paddingBottom = 6; PdfPTable t = new PdfPTable(cols); t.HeaderRows = 1; t.WidthPercentage = 100; float[] widths = new float[] { 1f, 1f, 10f }; t.SetWidths(widths); Phrase h = new Phrase(); h.Font = f1; h.Add("Approved Procedures Inconsistencies Report"); PdfPCell c = new PdfPCell(h); c.Colspan = cols; c.HorizontalAlignment = 1; t.AddCell(c); Dictionary types = new Dictionary(); foreach (ProcedureInfo pi in MyItemInfoList) { string type = pi.MyDifferences.ROConsistency.MyROChecks.Length > 0 ? "Reference Object Inconsistencies" : ""; if (type != string.Empty && !types.ContainsKey(0)) types.Add(0, type); type = pi.MyDifferences.TransitionConsistency.MyTransitionChecks.Length > 0 ? "Transition Inconsistencies" : ""; if (type != string.Empty && !types.ContainsKey(1)) types.Add(1, type); type = pi.MyDifferences.LibDocConsistency.MyLibDocChecks.Length > 0 ? "Library Document Inconsistencies" : ""; if (type != string.Empty && !types.ContainsKey(2)) types.Add(2, type); } // types.Clear(); if (types.Count == 0) { h = new Phrase(); h.Font = f2; h.Add("No Inconsistencies"); c = new PdfPCell(h); c.Colspan = cols; t.AddCell(c); } else { // Dictionary> ros = new Dictionary>(); Dictionary> ros = new Dictionary>(); DocVersionInfo dvi = MyItemInfoList[0].MyDocVersion; ROFstInfo rofst = dvi.DocVersionAssociations[0].MyROFst; //rofst.docVer = dvi; // ROFSTLookup lu = MyItemInfoList[0].MyDocVersion.DocVersionAssociations[0].MyROFst.ROFSTLookup; ROFSTLookup lu = rofst.GetROFSTLookup(dvi); if (types.ContainsKey(0)) { foreach (ProcedureInfo pi in MyItemInfoList) { foreach (ROCheck roc in pi.MyDifferences.ROConsistency.MyROChecks) { ROFSTLookup.rochild rocc = lu.GetRoChild12(roc.ROID); if (rocc.type == 1) { string newROValue = lu.GetRoValue(roc.ROID).Replace('`', '\xb0'); string rotitle = string.Format("{0} - Current Version: {1}", lu.GetROTitle(roc.ROID), lu.GetRoValue(roc.ROID).Replace('`','\xb0')); if (ros.ContainsKey(rotitle)) { ros[rotitle].Add(string.Format("Procedure {0} has an ROValue of: {1}", pi.DisplayNumber, roc.ROValue),roc.ROValue == newROValue); } else { ros.Add(rotitle, new Dictionary()); ros[rotitle].Add(string.Format("Procedure {0} has an ROValue of: {1}", pi.DisplayNumber, roc.ROValue),roc.ROValue == newROValue); } } } } } //jcb Dictionary> trs = new Dictionary>(); //Dictionary> trs = new Dictionary>(); if (types.ContainsKey(1)) { foreach (ProcedureInfo pi in MyItemInfoList) { foreach (TransitionCheck trc in pi.MyDifferences.TransitionConsistency.MyTransitionChecks) { TransitionInfo ti = TransitionInfo.Get(trc.TransitionID); string fromText = ti.MyContent.Text; //GO TO \\v \\v0 MatchCollection mc = Regex.Matches(fromText, @"\"); foreach (Match m in mc) if (m.Groups[2].Value == ti.TransitionID.ToString()) fromText = ItemInfo.ConvertToDisplayText(m.Groups[1].Value); string newValue = ti.MyContent.ContentItems[0].Path + "\r\nCurrent Version: " + fromText; string trtitle = "From Procedure " + newValue; if (trs.ContainsKey(trtitle)) { trs[trtitle].Add(string.Format("According to {0} the destination is: {1}", pi.DisplayNumber, ItemInfo.ConvertToDisplayText(trc.TransitionValue)), ItemInfo.ConvertToDisplayText(trc.TransitionValue) == fromText); } else { trs.Add(trtitle, new Dictionary()); trs[trtitle].Add(string.Format("According to {0} the destination is: {1}", pi.DisplayNumber, ItemInfo.ConvertToDisplayText(trc.TransitionValue)), ItemInfo.ConvertToDisplayText(trc.TransitionValue) == fromText); } } } } //end jcb Dictionary> lds = new Dictionary>(); //Dictionary> lds = new Dictionary>(); if (types.ContainsKey(2)) { foreach (ProcedureInfo pi in MyItemInfoList) { foreach (LibDocCheck ldc in pi.MyDifferences.LibDocConsistency.MyLibDocChecks) { DocumentInfo di = DocumentInfo.Get(ldc.DocID); string newValue = di.DTS.ToString(); string ldtitle = string.Format("{0} - Current Version: {1}", di.DocumentTitle, di.DTS.ToString()); if (lds.ContainsKey(ldtitle)) { lds[ldtitle].Add(string.Format("Procedure {0} has a Document Date of: {1}", pi.DisplayNumber, ldc.DocDate.ToString()),ldc.DocDate.ToString() == newValue); } else { lds.Add(ldtitle, new Dictionary()); lds[ldtitle].Add(string.Format("Procedure {0} has a Document Date of: {1}", pi.DisplayNumber, ldc.DocDate.ToString()), ldc.DocDate.ToString() == newValue); } } } } Color goodColor = new Color(System.Drawing.Color.PaleGreen); Color badColor = new Color(System.Drawing.Color.Khaki); if (types.ContainsKey(0)) { h = new Phrase(); h.Font = f2; h.Add(types[0]); c = new PdfPCell(h); c.Colspan = cols; t.AddCell(c); foreach (string rotitle in ros.Keys) { t.AddCell(BlankCell(1)); h = new Phrase(); h.Font = f3; h.Add(rotitle); c = new PdfPCell(h); c.Colspan = cols - 1; c.BackgroundColor = goodColor; t.AddCell(c); foreach (KeyValuePair d in ros[rotitle]) { t.AddCell(BlankCell(2)); h = new Phrase(); h.Font = f3; h.Add(d.Key); c = new PdfPCell(h); if (d.Value) c.BackgroundColor = goodColor; else c.BackgroundColor = badColor; t.AddCell(c); } } } //jcb if (types.ContainsKey(1)) { h = new Phrase(); h.Font = f2; h.Add(types[1]); c = new PdfPCell(h); c.Colspan = cols; t.AddCell(c); foreach (string trtitle in trs.Keys) { t.AddCell(BlankCell(1)); h = new Phrase(); h.Font = f3; h.Add(trtitle); c = new PdfPCell(h); c.Colspan = cols - 1; c.BackgroundColor = goodColor; t.AddCell(c); foreach (KeyValuePair d in trs[trtitle]) { t.AddCell(BlankCell(2)); h = new Phrase(); h.Font = f3; h.Add(d.Key); c = new PdfPCell(h); if (d.Value) c.BackgroundColor = goodColor; else c.BackgroundColor = badColor; t.AddCell(c); } } } //end jcb if (types.ContainsKey(2)) { h = new Phrase(); h.Font = f2; h.Add(types[2]); c = new PdfPCell(h); c.Colspan = cols; t.AddCell(c); foreach (string ldtitle in lds.Keys) { t.AddCell(BlankCell(1)); h = new Phrase(); h.Font = f3; h.Add(ldtitle); c = new PdfPCell(h); c.Colspan = cols - 1; c.BackgroundColor = goodColor; t.AddCell(c); foreach (KeyValuePair d in lds[ldtitle]) { t.AddCell(BlankCell(2)); h = new Phrase(); h.Font = f3; h.Add(d.Key); c = new PdfPCell(h); if (d.Value) c.BackgroundColor = goodColor; else c.BackgroundColor = badColor; t.AddCell(c); } } } } doc.Add(t); } } }