using System; using System.Collections.Generic; using System.Text; using System.Drawing; using LBWordLibrary; using VEPROMS.CSLA.Library; using System.Text.RegularExpressions; namespace Volian.Print.Library { public class ContinuousActionSummary : IDisposable { private LBApplicationClass _WordApp; private LBDocumentClass _WordDoc; private LBSelection _WordSel; private LBTable _WordTbl; private const string TheQuickBrownFox = "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog."; private VE_Font _pkFont; public ContinuousActionSummary() { _WordApp = new LBApplicationClass(); _WordDoc = _WordApp.Documents.Add(); _WordSel = _WordApp.Selection; _pkFont = new VE_Font("Arial", 11, E_Style.None, 12); // copied from Calvert Placekeepers. } public void Visible() { _WordApp.Visible = true; _WordApp.Activate(); } private ItemInfo GetHLSii(ItemInfo ii) { while (!ii.IsHigh) ii = ii.MyParent; return ii; } public ContinuousActionSummary(pkParagraphs myContActSteps, VE_Font pkFont) { ItemInfo prevHLSii = null; string txtPageNum = ""; // this gets asigned the page number continuous action step text. string txtDone = ""; // nothing is printed in the Done column _WordApp = new LBApplicationClass(); _WordDoc = _WordApp.Documents.Add(); _WordSel = _WordApp.Selection; //_WordApp.Visible = true; _pkFont = pkFont; AddTable(); AddHeader(); foreach (pkParagraph myContAct in myContActSteps) { if (myContAct.MyChildren.Count > 0) if (myContAct.MyParagraph.MyItemInfo.ActiveFormat.PlantFormat.FormatData.SectData.AccSectionData.ContinuousActionSummaryData.IncludeSectionNumAndTitle) // only print the section title if it has Continuous Action Steps AddSectionHeader(myContAct.MyParagraph.MyItemInfo.DisplayNumber, myContAct.MyParagraph.MyItemInfo.FormattedDisplayText); foreach (pkParagraph pgh in myContAct.MyChildren) // within each section... { ItemInfo hlsii = GetHLSii(pgh.MyParagraph.MyItemInfo); if (prevHLSii != hlsii) { if (_DoingSubsteps) FinishSubstep(txtDone, txtPageNum); // start Continuous Action text table cell StartSubStep(); txtPageNum = ""; prevHLSii = hlsii; } foreach (pkParagraph pghC in pgh.MyCautionsAndNotes) // this is a high level step with cautions and/or notes { StepConfig sc = pghC.MyParagraph.MyItemInfo.MyConfig as StepConfig; string doneStr = ""; string NoteCautionTab = pghC.MyParagraph.MyItemInfo.MyTab.CleanText.Trim(); NoteCautionTab += " "; if (txtPageNum == "") txtPageNum = (pghC.MyParagraph.MyItemInfo.PageNumber + 1).ToString(); AddContinuousAction(NoteCautionTab, GetContActStepText(pghC.MyParagraph.MyItemInfo), doneStr, txtPageNum, 0, 0); } string stpTab = pgh.MyParagraph.MyItemInfo.MyTab.CleanText.Trim() + " "; // this is either the High Level step tab or the sub-step tab float preTabLen = 0; if (pgh.MyParagraph.MyItemInfo.IsNote || pgh.MyParagraph.MyItemInfo.IsCaution) // only the caution and/or note is on the CAS - high level step not selected { stpTab = string.Format("{0} ", hlsii.MyTab.Text.Trim()) + " " + stpTab; } else if (!pgh.MyParagraph.MyItemInfo.IsHigh) // if the high level step was not included, adjust the tab for a sub-step/RNO { string tabPre = (_FirstLine) ? hlsii.MyTab.Text.Trim() + " " : ""; if (!_FirstLine) preTabLen = GetTextWidth(hlsii.MyTab.Text.Trim() + " ") / 72f; if (pgh.MyParagraph.MyItemInfo.IsInRNO) { stpTab = stpTab.TrimEnd() + "[R] "; } stpTab = tabPre + stpTab; } if (txtPageNum == "") txtPageNum = (pgh.MyParagraph.MyItemInfo.PageNumber + 1).ToString(); AddContinuousAction(stpTab, GetContActStepText(pgh.MyParagraph.MyItemInfo), "", txtPageNum, 0, preTabLen); if (pgh.MyChildren.Count > 0) // process the sub-steps under this parent { AddChildren(pgh, 1); } } if (_DoingSubsteps) FinishSubstep(txtDone, txtPageNum); } RepeatHeader(); } private void AddChildren(pkParagraph pgh, int level) { if (pgh.MyChildren.Count > 0) level++; foreach (pkParagraph cpgh in pgh.MyChildren) { if (cpgh.MyCautionsAndNotes.Count > 0) AddNotesOrCautions(cpgh,level); string stpTab = cpgh.MyParagraph.MyItemInfo.MyTab.CleanText.Trim() + (cpgh.MyParagraph.MyItemInfo.IsInRNO ? "[R] ": " "); if (cpgh.MyParagraph.MyItemInfo.IsNote || cpgh.MyParagraph.MyItemInfo.IsCaution) { stpTab = string.Format("{0} ", cpgh.MyParagraph.MyParent.MyTab.Text.Trim()) + " " + stpTab; } AddContinuousAction(stpTab, GetContActStepText(cpgh.MyParagraph.MyItemInfo), "", (cpgh.MyParagraph.MyItemInfo.PageNumber + 1).ToString(),level,0); AddChildren(cpgh, level); } } private string GetContActStepText(ItemInfo myItemInfo) { //string conActSumText = ""; //StepConfig sc = myItemInfo.MyConfig as StepConfig; //if (sc != null && sc.Step_AlternateContActSumText != null) // conActSumText = sc.Step_AlternateContActSumText; //if (conActSumText == "") // conActSumText = myItemInfo.FormattedDisplayText; //return conActSumText; return myItemInfo.FormattedDisplayText; } private void AddNotesOrCautions(pkParagraph pgh, int level) { foreach (pkParagraph cpgh in pgh.MyCautionsAndNotes) { string doneStr = ""; string NoteCautionTab = string.Format("{0} NOTE: ", cpgh.MyParagraph.MyParent.MyTab.Text); if (cpgh.MyParagraph.MyItemInfo.IsCaution) NoteCautionTab = string.Format("{0} CAUTION: ", cpgh.MyParagraph.MyParent.MyTab.Text); AddContinuousAction(NoteCautionTab, GetContActStepText(cpgh.MyParagraph.MyItemInfo), doneStr, (cpgh.MyParagraph.MyItemInfo.PageNumber + 1).ToString(),level,0); } } private void RepeatHeader() { _WordTbl.Rows.First.Select(); _WordSel.Rows.HeadingFormat = -1; } private bool _FirstSection = true; private void AddTable() { _FirstSection = true; _WordTbl = _WordDoc.Tables.Add(_WordSel.Range, 1, 4, 1, 0); _WordTbl.TopPadding = 6F; _WordTbl.BottomPadding = 6F; LBColumn col = _WordTbl.Columns.First; col.SetWidth(72 * .71F, LBWdRulerStyle.wdAdjustNone); col = col.Next; col.SetWidth(72 * 4.7F, LBWdRulerStyle.wdAdjustNone); col = col.Next; col.SetWidth(72 * .56F, LBWdRulerStyle.wdAdjustNone); col = col.Next; col.SetWidth(72 * .56F, LBWdRulerStyle.wdAdjustNone); _WordTbl.Rows.AllowBreakAcrossPages = 0; } private void AddHeader() { WriteCell("START", true, true); WriteCell("STEP CONTINUOUS ACTION", true, true); WriteCell("DONE", true, true); WriteCell("PAGE", true, false); } private void AddSectionHeader(string number, string title) { if (!_FirstSection) { Advance(5); _WordSel.MoveLeft(LBWdUnits.wdCell, 1, false); _WordSel.SelectRow(); _WordSel.Cells.Merge(); } _FirstSection = false; Advance(2); SetIndent(0, 0); WriteCell("SECTION " + number, true, false); WriteCell(" " + title, false, true); Advance(); } private float GetTextWidth(string txt) { System.Drawing.Font font = new System.Drawing.Font(_pkFont.Family, (float)_pkFont.Size); iTextSharp.text.Font iFont = Volian.Svg.Library.VolianPdf.GetFont(font); float w = 0; foreach (char c in txt) { w += iFont.BaseFont.GetWidthPointKerned(c.ToString(), (float)_pkFont.Size); } // indented text didn't always line up doing it this way //float w = iFont.BaseFont.GetWidthPointKerned(txt, (float)_pkFont.Size); return w; } private bool _FirstLine = false; private bool _DoingSubsteps = false; private void StartSubStep() { _FirstLine = true; _DoingSubsteps = true; Advance(2); //SetIndent(.64F, -.31F); } private void FinishSubstep(string done, string page) { _DoingSubsteps = false; Advance(); SetAlignment(LBWdParagraphAlignment.wdAlignParagraphCenter, LBWdCellVerticalAlignment.wdCellAlignVerticalBottom); WriteCell(done, false, true); SetAlignment(LBWdParagraphAlignment.wdAlignParagraphCenter, LBWdCellVerticalAlignment.wdCellAlignVerticalTop); WriteCell(page, false, false); } float prevTabIndent = 0; private void AddContinuousAction(string number, string text, string done, string page, int level, float preTabLen) { string celltxt = number; if (!_DoingSubsteps) Advance(2); else { if (!_FirstLine) { _WordSel.TypeParagraph(); celltxt = "\n" + celltxt; // not the first piece of text in the table cell, add a new line } } float tabIndent = GetTextWidth(number) / 72f; if (!_DoingSubsteps || _FirstLine || level == 0) prevTabIndent = preTabLen; if (text.Contains("\x05")) // hanging indent character { string[] parts = text.Split("\x05".ToCharArray()); float ind = tabIndent + (GetTextWidth(parts[0]) / 72f); SetIndent(ind, -ind); celltxt += (parts[0] + parts[1]); } else { SetIndent(prevTabIndent + tabIndent, -tabIndent); celltxt += text; } if (level == 0) prevTabIndent = tabIndent; WriteCell(celltxt, false, !_DoingSubsteps); if (!_DoingSubsteps) { SetAlignment(LBWdParagraphAlignment.wdAlignParagraphCenter, LBWdCellVerticalAlignment.wdCellAlignVerticalBottom); WriteCell(done, false, true); SetAlignment(LBWdParagraphAlignment.wdAlignParagraphCenter, LBWdCellVerticalAlignment.wdCellAlignVerticalTop); WriteCell(page, false, false); } _FirstLine = false; } static Regex rgx = new Regex(@"\\(b|ul|up[1-9]|dn0|up0|dn[1-9]|b0|ulnone)( |$|(?=\\))"); private void WriteRTF(string text) { Match m = rgx.Match(text); while (m.Success) { if (m.Index > 0) _WordSel.TypeText(text.Substring(0,m.Index)); switch (m.Groups[1].Value) { case "b": // bold on _WordSel.Font.Bold = 1; break; case "b0": // bold off _WordSel.Font.Bold = 0; break; case "ul": // underline on _WordSel.Font.Underline = LBWdUnderline.wdUnderlineSingle; break; case "ulnone": // underline off _WordSel.Font.Underline = LBWdUnderline.wdUnderlineNone; break; case "dn0": // superscript off _WordSel.Font.Superscript = 0; break; case "up0": // subscript off _WordSel.Font.Subscript = 0; break; default: if (m.Groups[1].Value.StartsWith("dn"))// subscript on _WordSel.Font.Subscript = 1; else // superscript on _WordSel.Font.Superscript = 1; break; } text = text.Substring(m.Index+m.Length); m = rgx.Match(text); } _WordSel.TypeText(text); } private void WriteCell(string text, bool bold, bool advance) { _WordSel.Font.Name = _pkFont.Family;//"Arial"; _WordSel.Font.Bold = bold ? 1 : 0; WriteRTF(text); if (advance) Advance(); } private void SetIndent(float left, float first) { _WordSel.ParagraphFormat.LeftIndent = left * 72; _WordSel.ParagraphFormat.FirstLineIndent = first * 72; } private void SetAlignment(LBWdParagraphAlignment hAlign, LBWdCellVerticalAlignment vAlign) { _WordSel.ParagraphFormat.Alignment = hAlign; _WordSel.Cells.VerticalAlignment = vAlign; } private void Advance(int count) { _WordSel.MoveRight(LBWdUnits.wdCell, count, false); } private void Advance() { Advance(1); } public void Dispose() { _WordApp.Quit(false); } } }