246 lines
		
	
	
		
			7.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			246 lines
		
	
	
		
			7.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using C1.Win.C1FlexGrid;
 | 
						|
using System;
 | 
						|
using System.Collections.Generic;
 | 
						|
using System.Drawing;
 | 
						|
using System.Linq;
 | 
						|
using System.Text;
 | 
						|
using System.Text.RegularExpressions;
 | 
						|
using System.Threading.Tasks;
 | 
						|
using System.Windows.Forms;
 | 
						|
using System.Xml;
 | 
						|
using VEPROMS.CSLA.Library;
 | 
						|
using Volian.Controls.Library;
 | 
						|
 | 
						|
namespace Volian.Controls.Library
 | 
						|
{
 | 
						|
	public partial class frmImportWordContents : Form
 | 
						|
	{ 
 | 
						|
		private static string GetTableText()
 | 
						|
		{
 | 
						|
			return TblFlexGrid.GetSearchableText();
 | 
						|
		}
 | 
						|
		private static VlnFlexGrid _TblFlexGrid = null;
 | 
						|
 | 
						|
		public static VlnFlexGrid TblFlexGrid
 | 
						|
		{
 | 
						|
			get {
 | 
						|
				if (_TblFlexGrid == null) _TblFlexGrid = new VlnFlexGrid();
 | 
						|
				return frmImportWordContents._TblFlexGrid;
 | 
						|
			}
 | 
						|
			set { frmImportWordContents._TblFlexGrid = value; }
 | 
						|
		}
 | 
						|
 | 
						|
		private static void LoadTable2(XmlNode xn)
 | 
						|
		{
 | 
						|
			XmlNode xn2 = xn.SelectSingleNode("tr/td/table");
 | 
						|
			if (xn2 != null) xn = xn2;
 | 
						|
			VlnFlexGrid fg = TblFlexGrid;
 | 
						|
			fg.Font = new Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Point);
 | 
						|
			fg.Clear();
 | 
						|
			fg.Rows.Count = 1;
 | 
						|
			fg.Cols.Count = 1;
 | 
						|
			int rows = 0;
 | 
						|
			fg.MergedRanges.Clear();
 | 
						|
			foreach (XmlNode xr in xn.ChildNodes)
 | 
						|
			{
 | 
						|
				if (xr.Name == "tr")
 | 
						|
				{
 | 
						|
					++rows;
 | 
						|
					AddTableRow(xr, fg, rows);
 | 
						|
				}
 | 
						|
			}
 | 
						|
			fg.MyBorders = new VlnBorders(GridLinePattern.Single, fg.Rows.Count, fg.Cols.Count);
 | 
						|
			fg.MyShading = new VlnGridCellShading(Color.White, fg.Rows.Count, fg.Cols.Count); // C2021-004 add table cell shading information
 | 
						|
			fg.BringToFront();
 | 
						|
			fg.Invalidate();
 | 
						|
			Application.DoEvents();
 | 
						|
			//ShowColumnWidths(fg);
 | 
						|
			fg.MakeRTFcells(false);
 | 
						|
			//ShowMergedCells(fg);
 | 
						|
			//ShowColumnWidths(fg);
 | 
						|
			//Well, Can I save the table
 | 
						|
			//using (Step step = MakeCSLAStep(mySteps, mySteps.Count, null, {TableContent}, 20008, E_FromType.Table))
 | 
						|
			//{
 | 
						|
			//	Grid.MakeGrid(step.MyContent, fg.GetXMLData(), "");
 | 
						|
			//}
 | 
						|
		}
 | 
						|
		public static VlnFlexGrid _MyFlexGrid = null;
 | 
						|
		private static void AddTableRow(XmlNode xr, VlnFlexGrid fg, int rows)
 | 
						|
		{
 | 
						|
			if (rows > fg.Rows.Count)
 | 
						|
				fg.Rows.Count = rows;
 | 
						|
			int cols = 0;
 | 
						|
			foreach (XmlNode xc in xr.ChildNodes)
 | 
						|
			{
 | 
						|
				++cols;
 | 
						|
				//if (xc.InnerText.Contains("RC-V200"))
 | 
						|
				//	Console.WriteLine(xc.InnerText);
 | 
						|
				//if (xc.InnerText.Contains("RC-V121"))
 | 
						|
				//	Console.WriteLine(xc.InnerText);
 | 
						|
				//if (xc.InnerXml.Contains("AB 137") || xc.InnerXml.Contains("3013N01"))
 | 
						|
				//	Console.WriteLine("here");
 | 
						|
				CellRange cr2 = GetMyMergedRange(fg, rows - 1, cols - 1);
 | 
						|
				//Console.WriteLine("Check {0}", cr2);
 | 
						|
				while (cr2.c1 != cols - 1 || cr2.r1 != rows - 1)
 | 
						|
				{
 | 
						|
					cols++;
 | 
						|
					cr2 = GetMyMergedRange(fg, rows - 1, cols - 1);
 | 
						|
				}
 | 
						|
				AddMergedCells(fg, rows, cols, xc);
 | 
						|
       			// adjust text alignment
 | 
						|
                string align = GetAttribute(xc, "textalign");
 | 
						|
			    if (align != null)
 | 
						|
                {
 | 
						|
                 
 | 
						|
                    switch(align)
 | 
						|
                    {
 | 
						|
                        case "LeftTop":
 | 
						|
                            fg.ChangeCellAlign(cr2, TextAlignEnum.LeftTop);
 | 
						|
                            break;
 | 
						|
                        case "LeftCenter":
 | 
						|
                            fg.ChangeCellAlign(cr2, TextAlignEnum.LeftCenter);
 | 
						|
                            break;
 | 
						|
                        case "LeftBottom":
 | 
						|
                            fg.ChangeCellAlign(cr2, TextAlignEnum.LeftBottom);
 | 
						|
                            break;
 | 
						|
                        case "CenterTop":
 | 
						|
                            fg.ChangeCellAlign(cr2, TextAlignEnum.CenterTop);
 | 
						|
                            break;
 | 
						|
                        case "CenterCenter":
 | 
						|
                            fg.ChangeCellAlign(cr2, TextAlignEnum.CenterCenter);
 | 
						|
                            break;
 | 
						|
                        case "CenterBottom":
 | 
						|
                            fg.ChangeCellAlign(cr2, TextAlignEnum.CenterBottom);
 | 
						|
                            break;
 | 
						|
                        case "RightTop":
 | 
						|
                            fg.ChangeCellAlign(cr2, TextAlignEnum.RightTop);
 | 
						|
                            break;
 | 
						|
                        case "RightCenter":
 | 
						|
                            fg.ChangeCellAlign(cr2, TextAlignEnum.RightCenter);
 | 
						|
                            break;
 | 
						|
                        case "RightBottom":
 | 
						|
                            fg.ChangeCellAlign(cr2, TextAlignEnum.RightBottom);
 | 
						|
                            break;
 | 
						|
                    }
 | 
						|
                }
 | 
						|
				//ShowMergedCells(fg);
 | 
						|
				if (xc.Name == "td")
 | 
						|
				{
 | 
						|
					AddTableColumn(xc, fg, rows, cols);
 | 
						|
				}
 | 
						|
			}
 | 
						|
		}
 | 
						|
 | 
						|
		//private static void ShowColumnWidths(VlnFlexGrid fg)
 | 
						|
		//{
 | 
						|
		//	foreach (Column c in fg.Cols)
 | 
						|
		//		Console.WriteLine("Width[{0}]={1}", c.Index, c.Width);
 | 
						|
		//}
 | 
						|
		private static void ShowMergedCells(VlnFlexGrid fg)
 | 
						|
		{
 | 
						|
			for (int r = 0; r < fg.Rows.Count; r++)
 | 
						|
			{
 | 
						|
				for (int c = 0; c < fg.Cols.Count; c++)
 | 
						|
				{
 | 
						|
					CellRange cr3 = GetMyMergedRange(fg, r, c);
 | 
						|
					if (fg.MergedRanges.Contains(cr3))
 | 
						|
						Console.WriteLine("cr3 r={0},c={1},rng={2}", r, c, cr3);
 | 
						|
				}
 | 
						|
			}
 | 
						|
		}
 | 
						|
		private static int GetSpan(string span)
 | 
						|
		{
 | 
						|
			int retval = int.Parse("0" + (span ?? ""));
 | 
						|
			if (retval == 0) return 0;
 | 
						|
			return retval - 1;
 | 
						|
		}
 | 
						|
		private static string GetAttribute(XmlNode xc, string attName)
 | 
						|
		{
 | 
						|
			string retval = null;
 | 
						|
			XmlAttribute xa = xc.Attributes[attName];
 | 
						|
			if (xa != null)
 | 
						|
				retval = xa.Value;
 | 
						|
			return retval;
 | 
						|
		}
 | 
						|
		private static void AddMergedCells(VlnFlexGrid fg, int rows, int cols, XmlNode xc)
 | 
						|
		{
 | 
						|
			string colspan = GetAttribute(xc, "colspan");
 | 
						|
			string rowspan = GetAttribute(xc, "rowspan");
 | 
						|
			if (colspan != null || rowspan != null)
 | 
						|
			{
 | 
						|
				//AddMergedRanges
 | 
						|
				int r1 = rows;
 | 
						|
				int c1 = cols;
 | 
						|
				int r2 = r1 + GetSpan(rowspan);
 | 
						|
				if (r2 > fg.Rows.Count) fg.Rows.Count = r2;
 | 
						|
				int c2 = c1 + GetSpan(colspan);
 | 
						|
				if (c2 > fg.Cols.Count) fg.Cols.Count = c2;
 | 
						|
				CellRange cr = new CellRange();
 | 
						|
				cr.r1 = r1 - 1;
 | 
						|
				cr.r2 = r2 - 1;
 | 
						|
				cr.c1 = c1 - 1;
 | 
						|
				cr.c2 = c2 - 1;
 | 
						|
				fg.MergedRanges.Add(cr);
 | 
						|
				//Console.WriteLine("Merged {0}", cr);
 | 
						|
			}
 | 
						|
		}
 | 
						|
 | 
						|
		private static CellRange GetMyMergedRange(VlnFlexGrid fg, int r, int c)
 | 
						|
		{
 | 
						|
			foreach (CellRange cr in fg.MergedRanges)
 | 
						|
			{
 | 
						|
				if (cr.r1 <= r && cr.r2 >= r && cr.c1 <= c && cr.c2 >= c)
 | 
						|
					return cr;
 | 
						|
			}
 | 
						|
			return fg.GetMergedRange(r, c);
 | 
						|
		}
 | 
						|
		private static Regex regNumber = new Regex("^[0-9]+$", RegexOptions.Compiled);
 | 
						|
		private static void AddTableColumn(XmlNode xc, VlnFlexGrid fg, int rows, int cols)
 | 
						|
		{
 | 
						|
			//Console.WriteLine("Rows {0}, Cols {1}", rows, cols);
 | 
						|
			if (cols > fg.Cols.Count)
 | 
						|
				fg.Cols.Count = cols;
 | 
						|
			string width = GetAttribute(xc, "width");
 | 
						|
			if (width != null && width != "" && regNumber.IsMatch(width))
 | 
						|
			{
 | 
						|
				if (width.EndsWith("%"))
 | 
						|
					fg.Cols[cols - 1].Width = (int)(int.Parse(width.Replace("%", "")) * 96 * 6.5F / 100);
 | 
						|
				else
 | 
						|
				{
 | 
						|
					int w = int.Parse(width)*96/72;
 | 
						|
					//Console.WriteLine("w[{0}]={1}", cols - 1, w);
 | 
						|
					fg.Cols[cols - 1].Width = w;
 | 
						|
				}
 | 
						|
			}
 | 
						|
            StringBuilder sb = new StringBuilder();
 | 
						|
            string prefix = "";
 | 
						|
			foreach (XmlNode xn in xc.ChildNodes)
 | 
						|
			{
 | 
						|
				if (xn.Name == "p")
 | 
						|
				{
 | 
						|
					sb.Append(prefix + xn.InnerText);
 | 
						|
				}
 | 
						|
				if (xn.Name == "ul")
 | 
						|
				{
 | 
						|
					foreach (XmlNode xn2 in xn.ChildNodes)
 | 
						|
					{
 | 
						|
						if (xn2.Name == "li")
 | 
						|
						{
 | 
						|
							sb.Append(prefix + "*" + xn.InnerText);
 | 
						|
						}
 | 
						|
						if (xn2.Name == "p")
 | 
						|
						{
 | 
						|
							sb.Append(prefix + xn.InnerText);
 | 
						|
						}
 | 
						|
					}
 | 
						|
				}
 | 
						|
			}
 | 
						|
			if (width != null && width != "" && !regNumber.IsMatch(width))
 | 
						|
			{
 | 
						|
				Console.WriteLine("width is not a number {0}, rows {1}, cols {2}, Text='{3}'", width, rows, cols, xc.OuterXml);
 | 
						|
			}
 | 
						|
			fg[rows - 1, cols - 1] = sb.ToString();
 | 
						|
		}
 | 
						|
	}
 | 
						|
}
 |