From 773b7c266b29458cae52997b65f91c64abb2bdad Mon Sep 17 00:00:00 2001 From: Rich Date: Mon, 6 Sep 2010 19:30:54 +0000 Subject: [PATCH] Search Results Report Changed Word conversion to use one instance of a Word Application unless an error occurs. Changed two timers to Static --- PROMS/DataLoader/Loader.cs | 4 +- PROMS/DataLoader/frmLoader.cs | 1 + PROMS/Volian.Print.Library/PDFReport.cs | 360 ++++++++++++++++++++++++ PROMS/Volian.Print.Library/pdf.cs | 145 ++++++++++ 4 files changed, 508 insertions(+), 2 deletions(-) create mode 100644 PROMS/Volian.Print.Library/PDFReport.cs create mode 100644 PROMS/Volian.Print.Library/pdf.cs diff --git a/PROMS/DataLoader/Loader.cs b/PROMS/DataLoader/Loader.cs index 3d75aa6f..1c399968 100644 --- a/PROMS/DataLoader/Loader.cs +++ b/PROMS/DataLoader/Loader.cs @@ -208,7 +208,7 @@ namespace DataLoader Security sec = new Security(VeSamPath, VePromsPath); return sec.Migrate(); } - private void WaitMS(int n) + private static void WaitMS(int n) { DateTime dtw = DateTime.Now.AddMilliseconds(n); while (DateTime.Now < dtw) @@ -216,7 +216,7 @@ namespace DataLoader Application.DoEvents(); } } - private void Wait(int n) + private static void Wait(int n) { DateTime dtw = DateTime.Now.AddSeconds(n); while (DateTime.Now < dtw) diff --git a/PROMS/DataLoader/frmLoader.cs b/PROMS/DataLoader/frmLoader.cs index dad47aef..a18e539f 100644 --- a/PROMS/DataLoader/frmLoader.cs +++ b/PROMS/DataLoader/frmLoader.cs @@ -459,6 +459,7 @@ namespace DataLoader if (Properties.Settings.Default["BackupFileName"].ToString() != "") this.tbxBackupFileName.Text = Properties.Settings.Default.BackupFileName; _Loading = false; + MSWordToPDF.CloseWordWhenDone = false; } private void frmLoader_FormClosing(object sender, FormClosingEventArgs e) diff --git a/PROMS/Volian.Print.Library/PDFReport.cs b/PROMS/Volian.Print.Library/PDFReport.cs new file mode 100644 index 00000000..add45bc4 --- /dev/null +++ b/PROMS/Volian.Print.Library/PDFReport.cs @@ -0,0 +1,360 @@ +using System; +using System.Collections.Generic; +using System.Text; +using VEPROMS.CSLA.Library; +using iTextSharp.text.pdf; +using iTextSharp.text; +using System.IO; +using VEPROMS.CSLA.Library; +using System.Text.RegularExpressions; +//using Volian.Generic; + +namespace Volian.Print.Library +{ + public class PDFReport + { + private ICollection _ResultList; + public ICollection ResultList + { + get { return _ResultList; } + set { _ResultList = value; } + } + private string _FileName; + public string FileName + { + get { return _FileName; } + set { _FileName = value; } + } + private string _ReportTitle; + public string ReportTitle + { + get { return _ReportTitle; } + set { _ReportTitle = value; } + } + private bool _ShowAnnotations = true; + public bool ShowAnnotations + { + get { return _ShowAnnotations; } + set { _ShowAnnotations = value; } + } + private string _SearchString; + public string SearchString + { + get { return _SearchString; } + set { _SearchString = VEPROMS.CSLA.Library.ItemInfo.ConvertToDisplayText(value); } + } + public PDFReport(string reportTitle, ICollection resultList, string fileName) + { + _ResultList = resultList; + _FileName = fileName; + _ReportTitle = VEPROMS.CSLA.Library.ItemInfo.ConvertToDisplayText(reportTitle); + } + public void Build2() + { + iTextSharp.text.Document document = new iTextSharp.text.Document(PageSize.LETTER, 36, 36, 36, 36); + try + { + PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(_FileName, FileMode.Create)); + + // we add some meta information to the document + document.SetMargins(36, 36, 36, 36); + // step 3: we open the document + document.Open(); + AddTable(document, 20, 80); + //AddTable(document, 10, 90); + //AddTable(document, 10, 100); + //AddTable(document, 15, 75); + //AddTable(document, 20, 70); + //AddTable(document, 25, 65); + //AddTable(document, 30, 60); + document.Close(); + } + catch (Exception ex) + { + } + } + + private void AddTable(iTextSharp.text.Document document, int width1, int width2) + { + float[] headerwidths = { width1, width2 }; + PdfPTable datatable = new PdfPTable(headerwidths); + datatable.HeaderRows = 3; + datatable.FooterRows = 1; + //datatable.Padding = 4; + //datatable.DefaultCell.Padding = 4; + //datatable.Spacing = 0; + //datatable.setBorder(Rectangle.NO_BORDER); + //datatable.Widths = headerwidths; + //datatable.WidthPercentage = 100; + datatable.TotalWidth = (width1 + width2) * 5; + datatable.LockedWidth = true; + // the first cell spans 10 columns + //iTextSharp.text.Font f1 = FontFactory.GetFont("Arial Unicode MS", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 14, Font.BOLD); + iTextSharp.text.Font f1 = pdf.GetFont("Arial Unicode MS", 14, 1, Color.BLACK); + + PdfPCell cell = new PdfPCell(new Phrase(ReportTitle, f1)); + //Font f2 = FontFactory.GetFont(FontFactory.HELVETICA, 10); + //FontFactory.RegisterDirectory(FontFind.FontDir); + //iTextSharp.text.Font f2 = FontFactory.GetFont("Arial Unicode MS", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 10); + iTextSharp.text.Font f2 = pdf.GetFont("Arial Unicode MS", 10, 0, Color.BLACK); + //iTextSharp.text.Font f3 = FontFactory.GetFont("Prestige Elite Tall", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 9); + iTextSharp.text.Font f3 = pdf.GetFont("Prestige Elite Tall", 9, 0, Color.BLACK); + cell.HorizontalAlignment = Element.ALIGN_CENTER; + //cell.Leading = 20; + cell.Colspan = 2; + //cell.Border = Rectangle.NO_BORDER; + cell.BackgroundColor = new Color(0xD0, 0xF0, 0xF0); + datatable.AddCell(cell); + Color subHeaderColor = new Color(0xD0, 0xF0, 0xD0); + // These cells span 2 rows + //datatable.DefaultCellBorderWidth = 2; + //datatable.DefaultHorizontalAlignment = 1; + //datatable.DefaultRowspan = 2; + datatable.DefaultCell.Padding = 4; + datatable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER; + AddCell(datatable, "Step", f2, subHeaderColor); + AddCell(datatable, "Text", f2, subHeaderColor); + AddCell(datatable, "Step", f2, subHeaderColor); + AddCell(datatable, "Text", f2, subHeaderColor); + datatable.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT; + //datatable.DefaultCellBorderWidth = 1; + //datatable.DefaultRowspan = 1; + //datatable.DefaultHorizontalAlignment = Element.ALIGN_LEFT; + string lastDvPath = ""; + string lastPath = ""; + Color AnnoColor = new Color(0xFF, 0xFF, 0xC0); + Color TextColor = Color.WHITE; + Color SectColor = Color.LIGHT_GRAY; + foreach (ItemInfo item in _ResultList) + { + if (item.SearchDVPath != lastDvPath) + AddGroup(datatable, item.SearchDVPath, lastDvPath, f2, false, new Color(0xC0, 0xFF, 0xC0)); + lastDvPath = item.SearchDVPath; + //if (item.SearchPath != lastPath) + string stepPath = AddGroup(datatable, item.SearchPath, lastPath, f2, true, new Color(0xE0, 0xFF, 0xE0)); + lastPath = item.SearchPath; + AddCell(datatable, stepPath, f2, (item.IsSection ? SectColor : TextColor)); + if (item.DisplayText.Contains("|")) // Need a better way to determine if it is a table. + AddCell(datatable, item.DisplayText, f3, TextColor); + else + AddCell(datatable, item.DisplayText, f2, (item.IsSection ? SectColor : TextColor)); + if (item.ItemAnnotationCount > 0) + { + foreach (AnnotationInfo ai in item.ItemAnnotations) + { + AddCell(datatable, ai.MyAnnotationType.Name, f2, AnnoColor); + AddCell(datatable, ai.SearchText, f2, AnnoColor); + } + } + } + document.Add(datatable); + } + //private static void AddCell(PdfPTable datatable, string str, iTextSharp.text.Font fnt, Color bgColor) + //{ + // PdfPCell cell = new PdfPCell(new Phrase(str, fnt)); + // cell.BackgroundColor = bgColor; + // datatable.AddCell(cell); + //} + private string AddGroup(PdfPTable datatable, string dvPath, string lastDvPath, Font f2, bool StripLast, Color bgColor) + { + int level = 0; + string retval = ""; + StringBuilder sb = new StringBuilder(); + string ttt = dvPath.Replace("\x11", " "); + string[] OldPath = lastDvPath.Split("\x7".ToCharArray()); + string[] NewPath = dvPath.Split("\x7".ToCharArray()); + string sep = ""; + bool foundMisMatch = false; + int n = NewPath.Length; + if (StripLast) + { + retval = NewPath[--n]; + if (retval.Contains("\x11")) + retval = retval.Substring(0, retval.IndexOf("\x11")); + } + for (int j = 0; j < n; j++) + { + if (NewPath[j] != "" && (foundMisMatch || OldPath.Length < j + 1 || NewPath[j] != OldPath[j])) + { + sb.Append(sep + "".PadLeft(2 * level) + NewPath[j].Replace("\x11", " ")).Replace(@"\u8209?", "-"); + sep = "\r\n"; + foundMisMatch = true; + } + level++; + } + if (sb.ToString() != "") + { + PdfPCell cell = new PdfPCell(new Phrase(sb.ToString(), f2)); + cell.Colspan = 2; + cell.BackgroundColor = bgColor; + cell.BorderWidthTop = 1; + datatable.AddCell(cell); + } + return retval; + } + public void Build() + { + iTextSharp.text.Document document = new iTextSharp.text.Document(PageSize.LETTER, 36, 36, 36, 36); + try + { + PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(_FileName, FileMode.Create)); + + // we add some meta information to the document + //document.AddAuthor("Gerald Henson"); + //document.AddSubject("This is the result of a Test."); + document.SetMargins(36, 36, 36, 36); + // step 3: we open the document + document.Open(); + BuildTable1(document); + //AddTable(document, 10, 90); + //AddTable(document, 10, 100); + //AddTable(document, 15, 75); + //AddTable(document, 20, 70); + //AddTable(document, 25, 65); + //AddTable(document, 30, 60); + } + 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 + { + document.Close(); + } + } + /// + /// Use Cells within Cells to see if I can limit the way Cells break from Page to Page + /// + /// + private void BuildTable1(iTextSharp.text.Document document) + { + float[] headerwidths = { 20, 80 }; + PdfPTable datatable = new PdfPTable(headerwidths); + datatable.HeaderRows = 3; + datatable.FooterRows = 1; + datatable.TotalWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin; + datatable.LockedWidth = true; + //FontFactory.RegisterDirectory(FontFind.FontDir); + //iTextSharp.text.Font f1 = FontFactory.GetFont("Arial Unicode MS", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 14, Font.BOLD); + //iTextSharp.text.Font f2 = FontFactory.GetFont("Arial Unicode MS", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 10); + //iTextSharp.text.Font f3 = FontFactory.GetFont("Prestige Elite Tall", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 9); + iTextSharp.text.Font f1 = pdf.GetFont("Arial Unicode MS", 14, 1, Color.BLACK); + iTextSharp.text.Font f2 = pdf.GetFont("Arial Unicode MS", 10, 0, Color.BLACK); + iTextSharp.text.Font f3 = pdf.GetFont("Prestige Elite Tall", 9, 0, Color.BLACK); + PdfPCell cell = new PdfPCell(new Phrase(ReportTitle, f1)); + cell.HorizontalAlignment = Element.ALIGN_CENTER; + cell.Colspan = 2; + cell.BackgroundColor = new Color(0xD0, 0xF0, 0xF0); + datatable.AddCell(cell); + Color subHeaderColor = new Color(0xD0, 0xF0, 0xD0); + datatable.DefaultCell.Padding = 4; + datatable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER; + AddCell(datatable, "Step", f2, subHeaderColor); + AddCell(datatable, "Text", f2, subHeaderColor); + AddCell(datatable, "Step", f2, subHeaderColor); + AddCell(datatable, "Text", f2, subHeaderColor); + datatable.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT; + string lastDvPath = ""; + string lastPath = ""; + Color AnnoColor = new Color(0xFF, 0xFF, 0xC0); + Color TextColor = Color.WHITE; + Color SectColor = Color.LIGHT_GRAY; + foreach (ItemInfo item in _ResultList) + { + if (item.SearchDVPath != lastDvPath) + AddGroup(datatable, item.SearchDVPath, lastDvPath, f2, false, new Color(0xC0, 0xFF, 0xC0)); + lastDvPath = item.SearchDVPath; + string stepPath = AddGroup(datatable, item.SearchPath, lastPath, f2, true, new Color(0xE0, 0xFF, 0xE0)); + lastPath = item.SearchPath; + AddCell(datatable, stepPath, f2, (item.IsSection ? SectColor : TextColor)); + if (item.DisplayText.Contains("|")) // Need a better way to determine if it is a table. + AddCell(datatable, item.DisplayText, f3, TextColor); + else + AddCell(datatable, item.DisplayText, f2, (item.IsSection ? SectColor : TextColor)); + if (item.ItemAnnotationCount > 0) + { + foreach (AnnotationInfo ai in item.ItemAnnotations) + { + AddCell(datatable, ai.MyAnnotationType.Name, f2, AnnoColor); + AddCell(datatable, ai.SearchText, f2, AnnoColor); + } + } + } + document.Add(datatable); + } + private void AddCell(PdfPTable datatable, string str, iTextSharp.text.Font fnt, Color bgColor) + { + iTextSharp.text.Font fntBold = new Font(fnt.BaseFont, fnt.Size, Font.BOLD, Color.RED); + Phrase p = BuildPhrase2(str, fnt, fntBold); + PdfPCell cell = new PdfPCell(p); + cell.PaddingBottom = 4; + cell.BackgroundColor = bgColor; + datatable.AddCell(cell); + } + + private Phrase BuildPhrase(string str, Font fnt, Font fntBold) + { + Phrase p = new Phrase(); + if (_SearchString != null) + { + while (str.ToLower().Contains(_SearchString.ToLower())) + { + int iStart = str.ToLower().IndexOf(_SearchString.ToLower()); + if (iStart > 0) p.Add(new Phrase(str.Substring(0, iStart), fnt)); + Phrase p2 = new Phrase(str.Substring(iStart, _SearchString.Length), fntBold); + p.Add(p2); + str = str.Substring(iStart + _SearchString.Length); + } + } + if (str.Length > 0) p.Add(new Phrase(str, fnt)); + return p; + } + private string _RegexSearchString = null; + private string RegexSearchString + { + get + { + if (_RegexSearchString == null) + { + _RegexSearchString = _SearchString; + if (_RegexSearchString[0] == '%') + _RegexSearchString = _RegexSearchString.Substring(1); + if (_RegexSearchString[_RegexSearchString.Length - 1] == '%') + _RegexSearchString = _RegexSearchString.Substring(0, _RegexSearchString.Length - 1); + _RegexSearchString = _RegexSearchString.Replace("%", ".*"); + } + return _RegexSearchString; + } + } + private Phrase BuildPhrase2(string str, Font fnt, Font fntBold) + { + Phrase p = new Phrase(); + int lastIndex = 0; + if ((_SearchString ?? "") != "") + { + Regex reg = new Regex(RegexSearchString, RegexOptions.Multiline | RegexOptions.IgnoreCase); + foreach (Match m in reg.Matches(str)) + { + if (m.Index > 0) + p.Add(new Phrase(str.Substring(lastIndex, m.Index - lastIndex), fnt)); + Chunk chk = new Chunk(m.Value, fnt); + //Phrase p2 = new Phrase(); + //foreach (Chunk chk in p2.Chunks) + chk.SetBackground(Color.YELLOW); + p.Add(chk); + lastIndex = m.Index + m.Length; + } + } + p.Add(new Phrase(str.Substring(lastIndex), fnt)); + return p; + } + + } +} diff --git a/PROMS/Volian.Print.Library/pdf.cs b/PROMS/Volian.Print.Library/pdf.cs new file mode 100644 index 00000000..73cc8758 --- /dev/null +++ b/PROMS/Volian.Print.Library/pdf.cs @@ -0,0 +1,145 @@ +using System; +using System.Collections.Generic; +using System.Text; +using iTextSharp.text; +using iTextSharp.text.pdf; +using System.IO; +using Microsoft.Win32; + +namespace Volian.Print.Library +{ + public class pdf + { + private Document _MyDocument; + public Document MyDocument + { + get { return _MyDocument; } + set { _MyDocument = value; } + } + private PdfWriter _MyWriter; + public PdfWriter MyWriter + { + get { return _MyWriter; } + set { _MyWriter = value; } + } + private string _MyFileName; + public string MyFileName + { + get { return _MyFileName; } + set { _MyFileName = value; } + } + public PdfContentByte ContentByte + { + get { return _MyWriter.DirectContent; } + } + public pdf(string myFileName) + { + _MyFileName = myFileName; + _MyDocument = new Document(PageSize.LETTER); + try + { + _MyWriter = PdfWriter.GetInstance(_MyDocument, new FileStream(myFileName, FileMode.Create)); + _MyWriter.SetAtLeastPdfVersion(PdfWriter.VERSION_1_7); + } + catch (Exception ex) + { + Console.WriteLine("{0} - {1}\r\n{2}", ex.GetType().Name, ex.Message, ex.StackTrace); + } + } + public pdf(string myFileName, Rectangle mySize) + { + _MyFileName = myFileName; + _MyDocument = new Document(mySize); + try + { + _MyWriter = PdfWriter.GetInstance(_MyDocument, new FileStream(myFileName, FileMode.Create)); + _MyWriter.SetAtLeastPdfVersion(PdfWriter.VERSION_1_7); + } + catch (Exception ex) + { + Console.WriteLine("{0} - {1}\r\n{2}", ex.GetType().Name, ex.Message, ex.StackTrace); + } + } + public void Close() + { + MyDocument.Close(); + } + private Color _WatermarkColor = Color.BLUE; + private float _WatermarkOpacity = .10F; + public void WatermarkPageNumber(int pageNumber) + { + Rectangle pageSize = ContentByte.PdfWriter.PageSize; // Get page size + BaseFont myBasefont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, Encoding.ASCII.EncodingName, false); + string strPage = pageNumber.ToString(); + float h = myBasefont.GetAscentPoint(strPage, 10); + float w = myBasefont.GetWidthPoint(strPage, 10); + float fontSizeH = 10 * pageSize.Height / h; + float fontSizeW = 10 * pageSize.Width / w; + float myFontSize = fontSizeH < fontSizeW ? fontSizeH : fontSizeW; + ContentByte.SaveState(); + ContentByte.BeginText(); + ContentByte.SetPatternFill(PatternPainter); + ContentByte.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE); + ContentByte.SetLineWidth(.5F); + ContentByte.SetFontAndSize(myBasefont, myFontSize);// Set font and size + ContentByte.SetColorStroke(_WatermarkColor); + PdfGState gs2 = new PdfGState(); + gs2.StrokeOpacity = _WatermarkOpacity; + ContentByte.SetGState(gs2); + ContentByte.ShowTextAlignedKerned(PdfContentByte.ALIGN_CENTER, strPage, pageSize.Width / 2, 0, 0);// Draw the text + ContentByte.EndText(); + ContentByte.FillStroke(); // Controlled by TextRenderMode above + ContentByte.RestoreState(); + + } + private PdfPatternPainter _PatternPainter; + + public PdfPatternPainter PatternPainter + { + get + { + if (_PatternPainter == null) + _PatternPainter = SetSquareDotPattern(2); + return _PatternPainter; + } + } + public PdfPatternPainter SetSquareDotPattern(float radius) + { + PdfPatternPainter myPatternPainter; + myPatternPainter = ContentByte.CreatePattern(radius * 4, radius * 2, radius * 4, radius * 2); + PdfGState gState = new PdfGState(); + gState.FillOpacity = _WatermarkOpacity; + myPatternPainter.SetGState(gState); + myPatternPainter.SetColorFill(_WatermarkColor); + myPatternPainter.Rectangle(0, 0, radius, radius); + myPatternPainter.Rectangle(radius * 2, radius, radius, radius); + myPatternPainter.Fill(); + return myPatternPainter; + } + public static iTextSharp.text.Font GetFont(string fontName, float fontSize, int fontStyle, Color fontColor) + { + RegisterFont(fontName); + return iTextSharp.text.FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, true, fontSize, fontStyle, fontColor); + } + public static void RegisterFont(string fontName) + { + if (!iTextSharp.text.FontFactory.IsRegistered(fontName)) + { + foreach (string name in FontKey.GetValueNames()) + { + if (name.StartsWith(fontName)) + { + string fontFile = (string)FontKey.GetValue(name); + iTextSharp.text.FontFactory.Register(fontFile.Contains("\\") ? fontFile : FontFolder + "\\" + fontFile); + } + } + } + } + private static RegistryKey _FontKey = Registry.LocalMachine.OpenSubKey("Software").OpenSubKey("Microsoft").OpenSubKey("Windows NT").OpenSubKey("CurrentVersion").OpenSubKey("Fonts"); + public static RegistryKey FontKey + { get { return _FontKey; } } + private static string _FontFolder = (String)Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("Explorer").OpenSubKey("Shell Folders").GetValue("Fonts"); + public static string FontFolder + { get { return _FontFolder; } } + } +}