SourceCode/PROMS/Volian.Controls.Library/frmImportWordContents.cs

886 lines
31 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using LBWordLibrary;
using System.Xml;
using VEPROMS.CSLA.Library;
namespace Volian.Controls.Library
{
public partial class frmImportWordContents : Form
{
LBApplicationClass _WordApp;
bool _initializing = false;
private StepRTB _MyStepRTB = null;
public StepRTB MyStepRTB
{
get { return _MyStepRTB; }
set { _MyStepRTB = value; }
}
public frmImportWordContents()
{
_initializing = true;
InitializeComponent();
// C2019-021 Allow the Number field to be edited.
txbWordFile.Text = Properties.Settings.Default.ImportWordFilePath;
disableButtons();
_initializing = false;
// B2019-108 Enable/disable buttons
btnOpen.Enabled = File.Exists(txbWordFile.Text) && _WordApp == null;
}
private void disableButtons()
{
// B2019-108 Enable/disable buttons
btnOpen.Enabled = File.Exists(txbWordFile.Text) && _WordApp == null; ;
btnNext.Enabled = false;
btnPage.Enabled = false;
btnPrev.Enabled = false;
btnCurrent.Enabled = false;
btnInsertNext.Enabled = false;
btnReplaceNext.Enabled = false;
}
private void ofd_FileOk(object sender, CancelEventArgs e)
{
_initializing = true;
txbWordFile.Text = ofd.FileName;
_initializing = false;
}
private void btnBrowse_Click(object sender, EventArgs e)
{
// if we have a word doc open, then close it and reset WordApp
if (_WordApp != null)
{
try
{
_WordApp.Quit();
}
catch
{
// will error if user already closed Word
}
_WordApp = null;
disableButtons();
}
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
btnOpen_Click(sender, e);
}
}
private void btnOpen_Click(object sender, EventArgs e)
{
if (!File.Exists(txbWordFile.Text))
{
MessageBox.Show(string.Format("{0}", txbWordFile.Text), "File Does Not Exist", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
disableButtons();
return;
}
_WordApp = new LBApplicationClass();
_WordApp.Documents.Open(txbWordFile.Text);
_WordApp.Visible = true;
// B2019-108 Enable/disable buttons
EnableButtons();
Properties.Settings.Default.ImportWordFilePath = txbWordFile.Text;
Properties.Settings.Default.Save();
}
private void EnableButtons()
{
btnOpen.Enabled = File.Exists(txbWordFile.Text) && _WordApp == null; ;
btnNext.Enabled = true;
btnPage.Enabled = true;
btnPrev.Enabled = true;
btnCurrent.Enabled = true;
btnInsertNext.Enabled = true;
btnReplaceNext.Enabled = true;
}
private void btnPage_Click(object sender, EventArgs e)
{
_WordApp.Selection.MoveDown(LBWdUnits.wdScreen, 1, 0);
}
private void btnPrev_Click(object sender, EventArgs e)
{
// C2019-021 RHM 5/15/2019 Added new methods and properties for Import Word Content
_WordApp.Selection.MoveUp(LBWdUnits.wdParagraph, 1, 0); // Move to previous paragraph
_WordApp.Selection.MoveUp(LBWdUnits.wdParagraph, 1, 1); // Select paragraph
//_WordApp.Selection.MoveEnd(LBWdUnits.wdCharacter, -1); // Move 1 Character Back
try
{
// C2019-021 Use generic function CopyWordText
CopyWordText();
if (txbWrdText.Text.TrimEnd("\r\n\f".ToCharArray()) == "")
btnPrev_Click(sender, e);
else
Clipboard.SetText(txbWrdText.Text);
}
catch
{ }
}
// C2019-021 RHM 5/15/2019 Added new methods and properties for Import Word Content
/// <summary>
/// MoveToNextCell
/// Find the next Cell including merged cells
/// Loop through columns then rows
/// </summary>
/// <returns>
/// Yes - if Processed as a cell
/// No - Not Processed
/// </returns>
private bool MoveToNextCell()
{
// Get the row and column for the current selection within the table.
int rowIndex = _WordApp.Selection.Cells[1].RowIndex;
int columnIndex = _WordApp.Selection.Cells[1].ColumnIndex;
// Only do this for tables
if (_WordApp.Selection.Tables != null && _WordApp.Selection.Tables.Count > 0)
{
LBTable tbl = _WordApp.Selection.Tables[1];
if (tbl.Range.Cells != null && tbl.Range.Cells.Count > 0)
{
// Make sure the entire cell is selected
_WordApp.Selection.SelectCell();
// Loop through the cells of the table
for (int i = 1; i < tbl.Range.Cells.Count; i++)
{
LBCell myCell = tbl.Range.Cells[i];
// if this cell has matching indices, then move to the next cell
if (myCell.RowIndex == rowIndex && myCell.ColumnIndex == columnIndex)
{
// Move the Start to the start of the next cell
_WordApp.Selection.Start = tbl.Range.Cells[i + 1].Range.Start;
// Select the entire cell_WordApp.Selection.Start =
_WordApp.Selection.SelectCell();
return true; // Cell Found and moved
}
}
}
}
return false; // Cell not found
}
private void btnNext_Click(object sender, EventArgs e)
{
// C2019-021 Either move by cells or paragraphs
if (_WordApp.Selection.Cells != null && _WordApp.Selection.Cells.Count > 0)
{
if (!MoveToNextCell())
{
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 2, 0);// Move beyond the table
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 1, 1);// Select a Paragraph
}
}
else // not a table
{
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 1, 0); // Move to the next paragraph
_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 1, 1); // Select a Paragraph
}
try
{
// C2019-021 Use Generic CopyWordText
CopyWordText();
if (_WordApp.Selection.Cells != null && _WordApp.Selection.Cells.Count > 0)
{
Clipboard.SetText(txbWrdText.Text);
}
else
{
// C2019-021 If the word text only contains whitespace skip to next
if (txbWrdText.Text.TrimEnd("\f\r\n".ToCharArray()) == "")
btnNext_Click(sender, e);
else
Clipboard.SetText(txbWrdText.Text);
}
}
catch
{ }
}
// C2019-021 Generic CopyWordText (Copy Text, Style and Number to fields on form)
private void CopyWordText()
{
// B2019-108 Display Font Size
// B2019-153 Get Font size from the beginning of the selection if the font size
// for the entire selection is invalid (999999) (Mixed font sizes)
lblFS.Text = GetFontSize() + " Pts";
txvStyle.Text = _WordApp.Selection.Style.NameLocal;
txbLevel.Text = _WordApp.Selection.Style.ListLevelNumber.ToString();
tbxNumber.Text = _WordApp.Selection.Range.ListFormat.ListString;
// C2019-021 RHM 5/15/2019 Added new methods and properties for Import Word Content
try
{
//Console.WriteLine("Cell Count = {0}", _WordApp.Selection.Cells.Count);
lblTable.Text = "Table";
lblTable.FontBold = true;
}
catch
{
lblTable.Text = "Not Table";
lblTable.FontBold = false;
}
// B2019-108 Replace Special Characters
string txt = ShowText(_WordApp.Selection.Text.TrimEnd("\r\a".ToCharArray())
.Replace("\x1D", "-")// Hyphen
.Replace("\x1E", "-")// Hyphen
.Replace("\x2013", "-")// Hyphen
.Replace("\xa0", " ")// Space
.Replace("\x0b", " ")// Space Soft Return
.Replace("\x201C", "\"")// Space
.Replace("\x201D", "\"")// Space
.Replace("\x09INITIAL", "")// Space
);
if (txt.Contains("\x09_____")) // Tab Signoff
txt = txt.Substring(0, txt.IndexOf("\x09_____")).TrimEnd(" \x09".ToCharArray());// Trim spaces and Tabs
txbWrdText.Text = txt;
_WordApp.Activate();
}
// B2019-153 Get Font size from the beginning of the selection if the font size
// for the entire selection is invalid (999999) (Mixed font sizes)
private string GetFontSize()
{
try
{
float retval = _WordApp.Selection.Font.Size;
if (retval > 14)
{
LBRange rng = _WordApp.Selection.Range;
rng.End = rng.Start;
retval = rng.Font.Size;
if (retval > 14)
retval = 12;
}
return retval.ToString();
}
catch (Exception ex)
{
return "12";
}
}
// C2019-021 RHM 5/15/2019 Added newe methods and properties for Import Word Content
private string ShowText(string txt)
{
// This was for debugging
//StringBuilder sb = new StringBuilder();
//foreach (char c in txt)
// if (((int)c) >= ' ' && ((int)c) < '\x7F')
// sb.Append(c);
// else
// sb.Append(string.Format("\\u{0:X4} ", ((int)c)));
//return sb.ToString();
return txt;
}
private void txbWordFile_TextChanged(object sender, EventArgs e)
{
// B2019-108 Enable/disable buttons
btnOpen.Enabled = File.Exists(txbWordFile.Text) && _WordApp == null;
}
private void frmImportWordContents_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
if (_WordApp != null)
_WordApp.Quit();
// B2019-108 Reset WordApp when closed.
_WordApp = null;
}
catch
{
// incase user manually closed word
}
}
//C2019-021 Remove old method.
//private void btnNextIns_Click(object sender, EventArgs e)
//{
// if (MyStepRTB != null)
// {
// if (MyStepRTB.MyItemInfo.IsSection)
// {
// MyStepRTB.RtbSendKeys("^{DOWN}"); // <Ctrl><down arrow> - next edit window
// MyStepRTB.RtbSendKeys("^+b"); // insert previous
// }
// else
// MyStepRTB.RtbSendKeys("^+i"); // insert next (same type and level)
// MyStepRTB.RtbSendKeys("^v"); // clipboard paste
// Application.DoEvents();
// }
// this.Focus(); // set focus back to this form
//}
//private void btnNextRpl_Click(object sender, EventArgs e)
//{
// if (MyStepRTB != null)
// {
// MyStepRTB.RtbSendKeys("^{DOWN}"); // <Ctrl><down arrow> - next edit window
// MyStepRTB.RtbSendKeys("^a"); // select all
// MyStepRTB.RtbSendKeys("^v"); // clipboard paste
// Application.DoEvents();
// }
// this.Focus(); // set focus back to this form
//}
// C2019-021 New function to replace the PROMS text with the word text and move to the next paragraph in word.
private void btnReplaceNext_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
if (MyStepRTB.Parent is VlnFlexGrid)//Replace text in a Grid (Table cell)
{
VlnFlexGrid vg = MyStepRTB.Parent as VlnFlexGrid;
vg.StartEditing();
MyStepRTB.Text = txbWrdText.Text;
// B2019-108 Set font for table cell
if (MyStepRTB.Parent is VlnFlexGrid && lblFS.Text != "FS")
{
Font fs = MyStepRTB.Font;
fs = new Font(fs.FontFamily, float.Parse(lblFS.Text.Replace("Pts", "")), fs.Style);
MyStepRTB.Font = fs;
}
vg[vg.Row, vg.Col] = MyStepRTB.Rtf;
vg.FinishEditing(false);
}
else if (MyStepRTB.MyItemInfo.IsSection && tbxNumber.Text != "")
{
using (VEPROMS.CSLA.Library.Item ii = MyStepRTB.MyItemInfo.Get())
{
ii.MyContent.Text = txbWrdText.Text.Trim("\r\n\f".ToCharArray());
ii.MyContent.Number = tbxNumber.Text;
ii.MyContent.Save();
}
EditItem ei = MyStepRTB.Parent as EditItem;
ei.RefreshTab();
}
else
{
MyStepRTB.Text = txbWrdText.Text.Trim("\r\n".ToCharArray());
MyStepRTB.Select(MyStepRTB.TextLength, 0);
}
btnNext_Click(sender, e);
}
}
// C2019-021 New function to insert the Word text into PROMS and move to the next paragraph in Word
private void btnInsertNext_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
MyStepRTB.SelectedText = txbWrdText.Text.Trim("\r\n\f".ToCharArray());
MyStepRTB.Select(MyStepRTB.TextLength, 0);
btnNext_Click(sender, e);
}
}
// C2019-021 New Function to create a after node in PROMS
private void btnAfter_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
if (MyStepRTB.Parent is VlnFlexGrid)
{
VlnFlexGrid vg = MyStepRTB.Parent as VlnFlexGrid;
//vg.FinishEditing(false);
vg.SelectNextCell();
}
else
{
EditItem ei = MyStepRTB.Parent as EditItem;
if (ei != null)
{
ei.AddSiblingAfter();
}
}
}
}
// C2019-021 Add a New Section in PROMS (Default Type = 10000)
private void btnSect_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
EditItem ei = MyStepRTB.Parent as EditItem;
if (ei.MyItemInfo.IsProcedure)
{
ei.AddChild(VEPROMS.CSLA.Library.E_FromType.Section, 10000);// TODO: Need Type for Procedure Steps
return;
}
while (ei != null && !ei.MyItemInfo.IsSection)
if (ei.MyParentEditItem != null)
ei = ei.MyParentEditItem;
else
ei = ei.MyPreviousEditItem;
if (ei != null)
{
ei.AddSiblingAfter();
}
}
}
// C201-021 New Function Add a new High level step
private void btnHigh_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
EditItem ei = GetEditItem(MyStepRTB);
if (ei != null && ei.MyItemInfo.IsSection)
{
ei.AddChild(VEPROMS.CSLA.Library.E_FromType.Step, 20002);
return;
}
while (ei != null && !ei.MyItemInfo.IsHigh)
if (ei.MyParentEditItem != null)
ei = ei.MyParentEditItem;
else
ei = ei.MyPreviousEditItem;
if (ei != null)
{
ei.AddSiblingAfter();
}
}
}
private EditItem GetEditItem(Control Ctrl)
{
while (Ctrl != null)
{
Ctrl = Ctrl.Parent;
if (Ctrl is EditItem) return Ctrl as EditItem;
}
return null;
}
// C2019-021 New Function Add a new sequential substep
private void btnSEQ_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
EditItem ei = MyStepRTB.Parent as EditItem;
ei.AddChild(VEPROMS.CSLA.Library.E_FromType.Step, 20001);
}
}
// C2019-021 New Function Add a new parent step after
private void btnParentAfter_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
EditItem ei = MyStepRTB.Parent as EditItem;
while (ei.MyPreviousEditItem != null)
ei = ei.MyPreviousEditItem;
if (ei != null && ei.MyParentEditItem != null)
{
ei.MyParentEditItem.AddSiblingAfter();
}
}
}
// C2019-021 New Function Split the text in the current proms window into two
// The text before the selection is kept in the current PROMS window
// The selected text is deleted
// the text after the selection is cut to the clipboard
private void btnSplit_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
MyStepRTB.SelectedText = "";
MyStepRTB.Select(MyStepRTB.SelectionStart, MyStepRTB.TextLength - MyStepRTB.SelectionStart);
Clipboard.SetText(MyStepRTB.SelectedText);
MyStepRTB.SelectedText = "";
}
}
// C2019-021 New Function - Performs a paste in the current window
private void btnPaste_Click(object sender, EventArgs e)
{
if (MyStepRTB != null)
{
MyStepRTB.SelectedText = Clipboard.GetText();
}
}
// C2019-021 RHM 5/15/2019 Added new methods and properties for Import Word Content
private void btnCurrent_Click(object sender, EventArgs e)
{
// B2019-108 Corrected curent button code
//_WordApp.Selection.MoveUp(LBWdUnits.wdParagraph, 1, 0); // Select paragraph
//_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 1, 1); // Select paragraph
//_WordApp.Selection.MoveEnd(LBWdUnits.wdCharacter, -1); // Exclude the last character
try
{
_WordApp.Selection.Cells[1].Range.Select();
}
catch
{
_WordApp.Selection.Paragraphs[1].Range.Select();
}
try
{
// C2019-021 Use Generic CopyWordText
CopyWordText();
// C2019-021If the word text only contains whitespace skip to next
//if (txbWrdText.Text.TrimEnd("\f\r\n\a".ToCharArray()) == "")
// btnNext_Click(sender, e);
//else
Clipboard.SetText(txbWrdText.Text);
}
catch
{
// Ask RHM what to do here 2/28/2020 JBS
}
}
//private void btnTest_Click(object sender, EventArgs e)
//{
// if (!MoveToNextCell())
// {
// _WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 2, 0);
// _WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 1, 1);
// }
// _WordApp.Activate();
// //int cols = _WordApp.Selection.Tables[1].Columns.Count;
//}
// B2019-108 Show special Characters
private void txbWrdText_TextChanged(object sender, EventArgs e)
{
txbImport.Text = fixText(txbWrdText.Text);
}
private string fixText(string txt)
{
StringBuilder sb = new StringBuilder();
foreach (char c in txt)
{
int ic = (int)c;
if (ic < 32 || ic > '\x7F')
{
sb.Append(string.Format("[x{0:x2}]", ic));
//MessageBox.Show(string.Format("[x{0:x2} - {1}]", ic, txt), "Special Character");
}
else
sb.Append(c);
}
return sb.ToString();
}
// B2019-108 Handle WordApp status
private void frmImportWordContents_Activated(object sender, EventArgs e)
{
// B2019-108 Corrected curent button code
//_WordApp.Selection.MoveUp(LBWdUnits.wdParagraph, 1, 0); // Select paragraph
//_WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 1, 1); // Select paragraph
//_WordApp.Selection.MoveEnd(LBWdUnits.wdCharacter, -1); // Exclude the last character
try
{
if (_WordApp != null || _WordApp.WindowState == LBWdWindowState.wdWindowStateMinimize)
_WordApp.WindowState = LBWdWindowState.wdWindowStateNormal;
}
catch
{
_WordApp = null;
disableButtons();
}
}
//private void btnTest_Click(object sender, EventArgs e)
//{
// if (!MoveToNextCell())
// {
// _WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 2, 0);
// _WordApp.Selection.MoveDown(LBWdUnits.wdParagraph, 1, 1);
// }
// _WordApp.Activate();
// //int cols = _WordApp.Selection.Tables[1].Columns.Count;
//}
private void btnTableConvert_Click(object sender, EventArgs e)
{
string xml = ConvertWordToXML();
if (xml == null)
{
MessageBox.Show("Make sure that the Selection is within a table in Word");
return;
}
XmlDocument xd = new XmlDocument();
xml = xml.Replace("\x0B", "\r\n");
try
{
xd.LoadXml(xml);
}
catch (Exception ex)
{
Console.WriteLine("{0} - {1}", ex.GetType().Name, ex.Message);
StringBuilder sb = new StringBuilder();
foreach (char c in xml)
{
int ic = c;
if (ic < 32 || ic > 128)
sb.Append(string.Format("<<<<{0:X4}>>>>", ic));
else
sb.Append(c);
}
MessageBox.Show(ex.Message + "\r\n" + sb.ToString(), ex.GetType().Name);
return;
}
LoadTable2(xd.DocumentElement);
int type = 20008;
ItemInfo myTable;
if(MyStepRTB.MyItemInfo.IsTable)
{
using( Item itm = MyStepRTB.MyItemInfo.Get())
{
itm.MyContent.MyGrid.Data = TblFlexGrid.GetXMLData();
itm.MyContent.Text= TblFlexGrid.GetSearchableText();
itm.Save();
ItemInfo.Refresh(itm);
ContentInfo.Refresh(itm.MyContent);
GridInfo.Refresh(itm.MyContent.MyGrid);
}
}
else
{
//using (Step stp = Step.MakeStep(MyStepRTB.MyItemInfo, null, null, TblFlexGrid.GetSearchableText() , 20008, E_FromType.Table))
//{
// myTable = ItemInfo.Get(stp.ItemID);
// Grid.MakeGrid(stp.MyContent, TblFlexGrid.GetXMLData(), "");
//}
//MyStepRTB.MyItemInfo.MyContent.RefreshContentParts();
EditItem ei = MyStepRTB.Parent as EditItem;
ei.AddChild(E_FromType.Table, 20008, TblFlexGrid);
if (ei != null) ei.SetAllTabs();
}
}
private string ConvertWordToXML()
{
string sXML = null;
try
{
DateTime tstart = DateTime.Now;
if (_WordApp.Selection.Tables != null && _WordApp.Selection.Tables.Count > 0)
{
LBTable tbl = _WordApp.Selection.Tables[1];
if (tbl.Range.Cells != null && tbl.Range.Cells.Count > 0)
{
// size variables to match table row and column dimensions
int[] Wcol = new int[tbl.Columns.Count];
int[,] Wcell = new int[tbl.Rows.Count, tbl.Columns.Count];
int[,] Hcell = new int[tbl.Rows.Count, tbl.Columns.Count];
int[,] SpanC = new int[tbl.Rows.Count, tbl.Columns.Count];
int[,] SpanR = new int[tbl.Rows.Count, tbl.Columns.Count];
DateTime tend = DateTime.Now; Console.WriteLine("{0} before Loop", TimeSpan.FromTicks(tend.Ticks - tstart.Ticks).TotalMilliseconds); tstart = tend;
pbTable.Maximum = tbl.Columns.Count * tbl.Rows.Count * tbl.Range.Cells.Count;
pbTable.Value = 0;
int iR = 0;
int iC = 0;
// capture widths for valid cells
for (int i = 1; i <= tbl.Range.Cells.Count; i++)
{
//LBCell myCell = tbl.Range.Cells[i];
// convert points to pixels and save value
int w = (int)(tbl.Range.Cells[i].Width * 8 / 6);
iC = tbl.Range.Cells[i].ColumnIndex - 1;
Wcell[tbl.Range.Cells[i].RowIndex - 1, iC] = w;
Hcell[tbl.Range.Cells[i].RowIndex - 1, iC] = (int)(tbl.Range.Cells[i].Height * 8 / 6);
if (Wcol[iC] == 0)
{
Wcol[iC] = w;
}
else
{
if (Wcol[iC] > w)
{
Wcol[iC] = w;
}
}
}
tend = DateTime.Now; Console.WriteLine("{0} before table width", TimeSpan.FromTicks(tend.Ticks - tstart.Ticks).TotalMilliseconds); tstart = tend;
int TableWidth = 0;
for (iC = 0; iC < tbl.Columns.Count; iC++)
{
TableWidth = TableWidth + Wcol[iC];
}
tend = DateTime.Now; Console.WriteLine("{0} before Rollup columns", TimeSpan.FromTicks(tend.Ticks - tstart.Ticks).TotalMilliseconds); tstart = tend;
// roll up columns
for (iR = 0; iR < tbl.Rows.Count; iR++)
{
int tmp = 0;
for (iC = 0; iC < tbl.Columns.Count; iC++)
{
if (Wcell[iR, iC] > TableWidth)
{
Wcell[iR, iC] = Wcol[iC];
SpanC[iR, iC] = 1;
}
else if (Wcell[iR, iC] > 0)
{
SpanC[iR, iC] = 1;
tmp = Wcell[iR, iC] - Wcol[iC];
while (tmp > 0)
{
for (int i = tbl.Columns.Count - 1; i > iC + 1; i--)
{
Wcell[iR, i] = Wcell[iR, i - 1];
Wcell[iR, i - 1] = 0;
}
tmp = tmp - Wcol[iC + SpanC[iR, iC]];
SpanC[iR, iC]++;
}
iC = iC + SpanC[iR, iC] - 1;
}
}
}
tend = DateTime.Now; Console.WriteLine("{0} before Rollup rows", TimeSpan.FromTicks(tend.Ticks - tstart.Ticks).TotalMilliseconds); tstart = tend;
//roll up row spans
for (iC = 0; iC < tbl.Columns.Count; iC++)
{
int LastGood = 0;
for (iR = 0; iR < tbl.Rows.Count; iR++)
{
if (Wcell[iR, iC] > 0)
{
// valid cell
LastGood = iR;
SpanR[LastGood, iC] = 1;
}
else
{
// invalid cell
SpanR[LastGood, iC]++;
}
}
}
tend = DateTime.Now; Console.WriteLine("{0} before Build", TimeSpan.FromTicks(tend.Ticks - tstart.Ticks).TotalMilliseconds); tstart = tend;
// build xml for this table
iR = -1;
int offset = 0;
sXML = "<table>";
for (int i = 1; i <= tbl.Range.Cells.Count; i++)
{
LBCell myCell = tbl.Range.Cells[i];
// if this cell has matching indices save its text
if (iR != myCell.RowIndex - 1)
{
// this is a new row
if (myCell.RowIndex > 1)
{
// close previous row
sXML += "</tr>";
}
// start new row
sXML += "<tr>";
iR = myCell.RowIndex - 1;
offset = 0;
}
sXML += "<td";
iC = myCell.ColumnIndex - 1 + offset;
if (SpanR[iR, iC] > 1)
{
sXML += " rowspan=\"" + SpanR[iR, iC] + "\"";
}
if (SpanC[iR, iC] > 1)
{
sXML += " colspan=\"" + SpanC[iR, iC] + "\"";
offset += SpanC[iR, iC] - 1;
}
else if (Wcol[iC] > 0)
{
sXML += " width=\"" + Wcol[iC] + "\"";
Wcol[iC] = Wcol[iC] * -1;
}
string textalign = "";
string ha;
switch (myCell.Range.ParagraphFormat.Alignment)
{
case LBWdParagraphAlignment.wdAlignParagraphCenter:
textalign += "Center";
ha = "\\qc";
break;
case LBWdParagraphAlignment.wdAlignParagraphRight:
textalign += "Right";
ha = "\\qr";
break;
default:
textalign += "Left";
ha = "\\ql";
break;
}
switch (myCell.VerticalAlignment)
{
case LBWdCellVerticalAlignment.wdCellAlignVerticalBottom:
textalign += "Bottom";
break;
case LBWdCellVerticalAlignment.wdCellAlignVerticalCenter:
textalign += "Center";
break;
case default(LBWdCellVerticalAlignment):
textalign += "Top";
break;
}
sXML += " textalign=\"" + textalign + "\"";
sXML += ">";
// select text from current cell
_WordApp.Selection.Start = myCell.Range.Start;
_WordApp.Selection.SelectCell();
_WordApp.Selection.End = _WordApp.Selection.End - 1;
if (_WordApp.Selection.End > _WordApp.Selection.Start)
{
// capture formatted text
StepRTB rtbStep = new StepRTB();
_WordApp.Selection.Copy();
rtbStep.Paste();
rtbStep.SelectAll();
Console.WriteLine("RTF before {0}", rtbStep.Rtf);
Console.WriteLine("RTF after {0}", rtbStep.Rtf);
// rtbStep.Rtf.Replace("\\f1 P\\f0 ", "\\u10004?"); // check mark within parenthesis
string strp = rtbStep.Rtf.Replace("\\par\r\n", "!!!");
strp = DisplayText.StaticStripRtfCommands(strp, true);
Console.WriteLine("RTF clean {0}", strp);
strp = strp.Remove(strp.LastIndexOf("!!!"));
StringBuilder sb = new StringBuilder();
sb.Append(strp);
sb.Replace("!!!", "\\par");
// clean up special characters
{
sb.Replace("\x1D", "-");// Hyphen
sb.Replace("\x1E", "-");// Hyphen
sb.Replace("\x2013", "-");// Hyphen
sb.Replace("\xa0", " ");// Space
sb.Replace("\x0b", " ");// Space Soft Return
sb.Replace("\x201C", "\"");// Space
sb.Replace("\x201D", "\"");// Space
sb.Replace("\x09INITIAL", "");// Space
sb.Replace("\x09_____", ""); // Tab Signoff
//sb.Replace("(P)", "(\\u10004?)"); // check mark within parenthesis
//sb.Replace("\\u9633?", "&#9633;"); //box
}
// save resulting text in xml structure
sXML += "<p>" + ha + sb + "</p></td>";
}
else
{
sXML += "<p></p></td>";
}
}
if (iR >= 0)
{
// at least one row hase been written so close the last row
sXML += "</tr>";
}
sXML += "</table>";
tend = DateTime.Now; Console.WriteLine("{0} After Build", TimeSpan.FromTicks(tend.Ticks - tstart.Ticks).TotalMilliseconds); tstart = tend;
}
}
}
catch (Exception ex)
{
while (ex != null)
{
Console.WriteLine("Column{0} - {1}", ex.GetType().Name, ex.Message);
ex = ex.InnerException;
}
}
return sXML;
}
}
}