using System; using System.Collections.Generic; using System.Text; using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.text.factories; using Volian.Svg.Library; using System.Text.RegularExpressions; using System.Xml; using VEPROMS.CSLA.Library; namespace Volian.Print.Library { public class VlnSvgPageHelper:SvgPageHelper { private ChangeBarDefinition _ChangeBarDefinition; public ChangeBarDefinition ChangeBarDefinition { get { return _ChangeBarDefinition; } set { _ChangeBarDefinition = value; } } private PdfLayer _TextLayer; public PdfLayer TextLayer { get { return _TextLayer; } set { _TextLayer = value; } } private PdfLayer _DebugLayer; public PdfLayer DebugLayer { get { return _DebugLayer; } set { _DebugLayer = value; } } private VEPROMS.CSLA.Library.SectionInfo _MySection; public VEPROMS.CSLA.Library.SectionInfo MySection { get { return _MySection; } set { _MySection = value; MySvg = BuildSvg(_MySection); } } private int _CurrentPageOf = 0; public int CurrentPageOf { get { return _CurrentPageOf; } set { _CurrentPageOf = value; } } private string _Rev = string.Empty; public string Rev { get { return _Rev; } set { _Rev = value; } } private string _RevDate = string.Empty; public string RevDate { get { return _RevDate; } set { _RevDate = value; } } public VlnSvgPageHelper(VEPROMS.CSLA.Library.SectionInfo mySection) { MySection = mySection; } private Volian.Svg.Library.Svg BuildSvg(VEPROMS.CSLA.Library.SectionInfo mySection) { VEPROMS.CSLA.Library.FormatInfo activeFormat = mySection.ActiveFormat; VEPROMS.CSLA.Library.DocStyle docStyle = mySection.MyDocStyle; Volian.Svg.Library.Svg mySvg = null; mySvg = SvgSerializer.StringDeserialize(BuildMyText(activeFormat)); mySvg.ViewBox.Height = 1100; mySvg.ViewBox.Width = 850; mySvg.Height = new SvgMeasurement(11, E_MeasurementUnits.IN); mySvg.Width = new SvgMeasurement(8.5F, E_MeasurementUnits.IN); mySvg.LeftMargin = (float)docStyle.Layout.LeftMargin; mySvg.TopMargin = 9.6F; VEPROMS.CSLA.Library.PageStyle pageStyle = docStyle.pagestyle; AddPageListItems(mySvg, pageStyle, mySection); //DocStyle docStyle = GetDocStyle(activeFormat, (section.MyContent.Type ?? 10000)); mySvg.ProcessText += new SvgProcessTextEvent(mySvg_ProcessText); return mySvg; } private static Regex regexFindToken = new Regex("{[^{}]*}"); private string mySvg_ProcessText(object sender, SvgProcessTextArgs args) { if (!args.MyText.Contains("{")) return args.MyText; return regexFindToken.Replace(args.MyText, new MatchEvaluator(ReplacePageListToken)); } private string BuildMyText(VEPROMS.CSLA.Library.FormatInfo activeFormat) { string sGenMac = activeFormat.GenMac; if (!sGenMac.Contains("xmlns")) sGenMac = sGenMac.Replace(".StringSerialize(mySvg); XmlDocument xDocPrintout = new XmlDocument(); xDocPrintout.LoadXml(str); XmlNode xn = xDocPrintout.DocumentElement.ChildNodes[0]; xn.AppendChild(xDocPrintout.ImportNode(xDocGenMac.DocumentElement, true)); return xDocPrintout.OuterXml; } private string FirstAndLast(string token) { // strip the curly braces and return the first and last character // For example Header1 becomes H1 and Box2 becomes B2 return token.Substring(1, 1) + token.Substring(token.Length - 2, 1); } private static Regex regexJustTokens = new Regex(@"^{([^{}\?]*}{)*[^{}\?]*}$"); private void AddPageListItems(Volian.Svg.Library.Svg mySvg, VEPROMS.CSLA.Library.PageStyle pageStyle, VEPROMS.CSLA.Library.SectionInfo section) { SvgGroup svgGroup = new SvgGroup(); foreach (VEPROMS.CSLA.Library.PageItem pageItem in pageStyle.PageItems) { if (regexJustTokens.IsMatch(pageItem.Token)) { MatchCollection matches = regexFindToken.Matches(pageItem.Token); foreach (Match match in matches) { string token = match.Value; switch (match.Value) { case "{HEADER1}": case "{HEADER2}": case "{HEADER3}": case "{HEADER4}": case "{HEADER5}": case "{BOX1}": case "{BOX2}": case "{BOX3}": case "{BOX4}": case "{BOX5}": case "{BOX6}": case "{BOX7}": case "{BOX8}": svgGroup.Add(PageItemToSvgUse(pageItem, FirstAndLast(token))); break; case "{DRAFTPAGE}": case "{REFERENCEPAGE}": case "{MASTERPAGE}": case "{SAMPLEPAGE}": //mySvg.SetValidWaterMark(token, _Watermark); // Need logic to limit watermark to tokens. break; case "{PROCTITLE}": SplitTitle(svgGroup, pageItem, section.MyProcedure.DisplayText, (int)section.ActiveFormat.PlantFormat.FormatData.ProcData.TitleLength); break; case "{EOPNUM}": svgGroup.Add(PageItemToSvgText(pageItem, section.MyProcedure.DisplayNumber)); break; case "{SECTIONLEVELTITLE}": SplitTitle(svgGroup, pageItem, section.DisplayText, section.ActiveFormat.PlantFormat.FormatData.SectData.SectionTitleLength); svgGroup.Add(PageItemToSvgText(pageItem, section.DisplayText)); break; case "{SECTIONLEVELNUMBER}": svgGroup.Add(PageItemToSvgText(pageItem, section.DisplayNumber)); break; default: Console.WriteLine("Token not processed {0}", token); break; } } } else { svgGroup.Add(PageItemToSvgText(pageItem, pageItem.Token)); } } mySvg.Add(svgGroup); } private void SplitTitle(SvgGroup svgGroup, VEPROMS.CSLA.Library.PageItem pageItem, string title, int? len) { // TODO: need to calculate len in either points or inches and use the font from the pageItem to determine the number of characters if (len == null || title.Length < len) { svgGroup.Add(PageItemToSvgText(pageItem, title)); return; } // Otherwise determine how many line to split the text into List titleLines = SplitText(title, (int)len); // Move up 6 Points per line to find the starting point float yOffset = -6 * (titleLines.Count - 1); foreach (string line in titleLines) { svgGroup.Add(PageItemToSvgText(pageItem, line, yOffset)); yOffset += 12; } } private List SplitText(string title, int len) { List retval = new List(); int strlen = title.Length; int start = 0; while ((strlen - start) > len) { int width = FindWidth(title, start, len); retval.Add(title.Substring(start, width)); start += width; while (title[start] == ' ') start++; } if (strlen - start > 0) retval.Add(title.Substring(start, strlen - start)); return retval; } private int FindWidth(string title, int start, int len) { for (int ii = start + len; ii > start; ii--) { if (title[ii] == ' ') { while (title[ii] == ' ') ii--; if (ii > start) return 2 + ii - start; return len; } } return len; } private SvgPart PageItemToSvgUse(VEPROMS.CSLA.Library.PageItem pageItem, string templateName) { SvgUse svgUse = new SvgUse(); svgUse.UseID = templateName; svgUse.X = new SvgMeasurement((float)(pageItem.Col ?? 0), E_MeasurementUnits.PT); svgUse.Y = new SvgMeasurement((float)(pageItem.Row ?? 0), E_MeasurementUnits.PT); return svgUse; } private static SvgText PageItemToSvgText(VEPROMS.CSLA.Library.PageItem pageItem, string text) { SvgText svgText = new SvgText(); svgText.Text = text; VEPROMS.CSLA.Library.E_Justify justify = pageItem.Justify ?? VEPROMS.CSLA.Library.E_Justify.PSLeft; if ((justify & VEPROMS.CSLA.Library.E_Justify.PSLeft) == VEPROMS.CSLA.Library.E_Justify.PSLeft) svgText.Justify = SvgJustify.Left; else if ((justify & VEPROMS.CSLA.Library.E_Justify.PSRight) == VEPROMS.CSLA.Library.E_Justify.PSRight) svgText.Justify = SvgJustify.Right; else svgText.Justify = SvgJustify.Center; svgText.Font = pageItem.Font.WindowsFont; svgText.X = new SvgMeasurement((float)(pageItem.Col ?? 0), E_MeasurementUnits.PT); svgText.Y = new SvgMeasurement((float)(pageItem.Row ?? 0), E_MeasurementUnits.PT); if (svgText.Font.Underline && svgText.Text.EndsWith(" ")) svgText.Text = svgText.Text.Substring(0, svgText.Text.Length - 1) + "\xA0";// replace last space with a hardspace return svgText; } private SvgPart PageItemToSvgText(VEPROMS.CSLA.Library.PageItem pageItem, string text, float yOffset) { SvgText svgText = new SvgText(); svgText.Text = text; VEPROMS.CSLA.Library.E_Justify justify = pageItem.Justify ?? VEPROMS.CSLA.Library.E_Justify.PSLeft; if ((justify & VEPROMS.CSLA.Library.E_Justify.PSLeft) == VEPROMS.CSLA.Library.E_Justify.PSLeft) svgText.Justify = SvgJustify.Left; else if ((justify & VEPROMS.CSLA.Library.E_Justify.PSRight) == VEPROMS.CSLA.Library.E_Justify.PSRight) svgText.Justify = SvgJustify.Right; else svgText.Justify = SvgJustify.Center; svgText.Font = pageItem.Font.WindowsFont; svgText.X = new SvgMeasurement((float)(pageItem.Col ?? 0), E_MeasurementUnits.PT); svgText.Y = new SvgMeasurement((float)(yOffset + pageItem.Row ?? 0), E_MeasurementUnits.PT); if (svgText.Font.Underline && svgText.Text.EndsWith(" ")) svgText.Text = svgText.Text.Substring(0, svgText.Text.Length - 1) + "\xA0";// replace last space with a hardspace return svgText; } private List _MissingTokens = new List(); protected override string ReplacePageListToken(Match match) { switch (match.Value) { case "{PAGE}": // Current Page Number return CurrentPageNumber.ToString(); case "{OF}": // Total Page Count for this section return CurrentPageOf.ToString(); case "{REVDATE}": // Revision Date return RevDate; case "{REV}": // Revision Number return Rev; } if (!_MissingTokens.Contains(match.Value)) { _MissingTokens.Add(match.Value); Console.WriteLine("\t\t\t\tcase \"{0}\": // Unhandled Token\r\n\t\t\t\t\treturn \"\";", match.Value); } return ""; } } public class ChangeBarDefinition { private DocVersionConfig.PrintChangeBar _MyChangeBarType; public DocVersionConfig.PrintChangeBar MyChangeBarType { get { return _MyChangeBarType; } set { _MyChangeBarType = value; } } private DocVersionConfig.PrintChangeBarLoc _MyChangeBarLoc; public DocVersionConfig.PrintChangeBarLoc MyChangeBarLoc { get { return _MyChangeBarLoc; } set { _MyChangeBarLoc = value; } } private DocVersionConfig.PrintChangeBarText _MyChangeBarText; public DocVersionConfig.PrintChangeBarText MyChangeBarText { get { return _MyChangeBarText; } set { _MyChangeBarText = value; } } private string _MyChangeBarMessage; public string MyChangeBarMessage { get { return _MyChangeBarMessage; } set { _MyChangeBarMessage = value; } } private int _MyChangeBarColumn; //TODO Should this be two dimensional array public int MyChangeBarColumn { get { return _MyChangeBarColumn; } set { _MyChangeBarColumn = value; } } public ChangeBarDefinition() { } public ChangeBarDefinition(DocVersionConfig.PrintChangeBar myCBT, DocVersionConfig.PrintChangeBarLoc myCBL, DocVersionConfig.PrintChangeBarText myCBTxt, string myCBMsg) { _MyChangeBarType = myCBT; _MyChangeBarLoc = myCBL; _MyChangeBarText = myCBTxt; _MyChangeBarMessage = myCBMsg; } } }