Added Sub ConvertTable to change Word table definition to FlexGrid compatible XML

This commit is contained in:
Devin Jankowski 2020-02-28 15:19:13 +00:00
parent d0df8b68e8
commit 9d24334753
2 changed files with 824 additions and 481 deletions

View File

@ -59,6 +59,8 @@
this.btnTest = new DevComponents.DotNetBar.ButtonX();
this.txbImport = new System.Windows.Forms.TextBox();
this.lblFS = new DevComponents.DotNetBar.LabelX();
this.btnTableConvert = new DevComponents.DotNetBar.ButtonX();
this.pbTable = new System.Windows.Forms.ProgressBar();
this.SuspendLayout();
//
// txbWordFile
@ -451,11 +453,33 @@
this.lblFS.TabIndex = 30;
this.lblFS.Text = "FS";
//
// btnTableConvert
//
this.btnTableConvert.AccessibleRole = System.Windows.Forms.AccessibleRole.PushButton;
this.btnTableConvert.ColorTable = DevComponents.DotNetBar.eButtonColor.OrangeWithBackground;
this.btnTableConvert.Location = new System.Drawing.Point(405, 226);
this.btnTableConvert.Name = "btnTableConvert";
this.btnTableConvert.Shortcuts.Add(DevComponents.DotNetBar.eShortcut.F3);
this.btnTableConvert.Size = new System.Drawing.Size(31, 20);
this.btnTableConvert.Style = DevComponents.DotNetBar.eDotNetBarStyle.StyleManagerControlled;
this.btnTableConvert.TabIndex = 31;
this.btnTableConvert.Text = "Table";
this.btnTableConvert.Click += new System.EventHandler(this.btnTableConvert_Click);
//
// pbTable
//
this.pbTable.Location = new System.Drawing.Point(405, 252);
this.pbTable.Name = "pbTable";
this.pbTable.Size = new System.Drawing.Size(225, 20);
this.pbTable.TabIndex = 32;
//
// frmImportWordContents
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(731, 280);
this.Controls.Add(this.pbTable);
this.Controls.Add(this.btnTableConvert);
this.Controls.Add(this.lblFS);
this.Controls.Add(this.txbImport);
this.Controls.Add(this.btnTest);
@ -531,5 +555,7 @@
private DevComponents.DotNetBar.ButtonX btnTest;
private System.Windows.Forms.TextBox txbImport;
private DevComponents.DotNetBar.LabelX lblFS;
private DevComponents.DotNetBar.ButtonX btnTableConvert;
private System.Windows.Forms.ProgressBar pbTable;
}
}

View File

@ -7,6 +7,8 @@ using System.Text;
using System.Windows.Forms;
using System.IO;
using LBWordLibrary;
using System.Xml;
using VEPROMS.CSLA.Library;
namespace Volian.Controls.Library
{
@ -30,7 +32,7 @@ namespace Volian.Controls.Library
disableButtons();
_initializing = false;
// B2019-108 Enable/disable buttons
btnOpen.Enabled = File.Exists(txbWordFile.Text) && _WordApp==null;
btnOpen.Enabled = File.Exists(txbWordFile.Text) && _WordApp == null;
}
private void disableButtons()
@ -152,7 +154,7 @@ namespace Volian.Controls.Library
{
// Move the Start to the start of the next cell
_WordApp.Selection.Start = tbl.Range.Cells[i + 1].Range.Start;
// Select the entire cell
// Select the entire cell_WordApp.Selection.Start =
_WordApp.Selection.SelectCell();
return true; // Cell Found and moved
}
@ -331,7 +333,7 @@ namespace Volian.Controls.Library
vg.StartEditing();
MyStepRTB.Text = txbWrdText.Text;
// B2019-108 Set font for table cell
if (MyStepRTB.Parent is VlnFlexGrid && lblFS.Text !="FS")
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);
@ -517,8 +519,6 @@ namespace Volian.Controls.Library
//else
Clipboard.SetText(txbWrdText.Text);
}
catch
{ }
}
//private void btnTest_Click(object sender, EventArgs e)
//{
@ -557,6 +557,10 @@ namespace Volian.Controls.Library
// 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)
@ -564,9 +568,322 @@ namespace Volian.Controls.Library
}
catch
{
_WordApp=null;
_WordApp = null;
disableButtons();
}
catch
{ }
}
//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 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;
}
}
}