// will delete the currently selected row // based on the current user row location public void DeleteRow() { // see if a table selected or insertion point inside a table HtmlTable table = null; HtmlTableRow row = null; HtmlTableCell cell = null; GetTableElement(out table, out row, out cell); // process according to table being defined if (table != null && row != null) { try { using (new UndoUnit(Document, "Table row delete")) { // find the existing row the user is on and perform the deletion int index = row.rowIndex; table.deleteRow(index); } editor._FireCursorMovedEvent(); } catch (Exception ex) { throw new HtmlEditorException("Unable to delete the selected Row", "TableDeleteRow", ex); } } else { throw new HtmlEditorException("Row not currently selected within the table", "TableDeleteRow"); } }
public void DeleteTable() { HtmlTable table = null; HtmlTableRow row = null; HtmlTableCell cell = null; GetTableElement(out table, out row, out cell); if (table != null) { try { using (new UndoUnit(Document, "Table delete")) { (table as HtmlDomNode).removeNode(true); } editor._FireCursorMovedEvent(); } catch (Exception ex) { throw new HtmlEditorException("Unable to delete the selected Row", "TableDeleteRow", ex); } } else { throw new HtmlEditorException("Row not currently selected within the table", "TableDeleteRow"); } }
public void DeleteColumn() { // see if a table selected or insertion point inside a table HtmlTable table = null; HtmlTableRow row = null; HtmlTableCell cell = null; GetTableElement(out table, out row, out cell); // process according to table being defined if (table != null && row != null && cell != null) { try { using (new UndoUnit(Document, "Table column delete")) { foreach (HtmlTableRow r in table.rows) { r.deleteCell(cell.cellIndex); } table.cols--; } editor._FireCursorMovedEvent(); } catch (Exception ex) { throw new HtmlEditorException("Unable to delete the selected Row", "TableDeleteRow", ex); } } else { throw new HtmlEditorException("Row not currently selected within the table", "TableDeleteRow"); } }
} //GetTableElement /// <summary> /// Get selected or parent table /// </summary> /// <returns>null if not found</returns> public HtmlTable GetTableElement() { // define the table and row elements and obtain there values HtmlTable table = null; HtmlTextRange range = SelectionHelper.GetTextRange(Document); // first see if the table element is selected table = SelectionHelper.GetFirstControl(Document) as HtmlTable; // if table not selected then parse up the selection tree if (table == null && range != null) { HtmlElement element = (HtmlElement)range.parentElement(); // parse up the tree until the table element is found while (element != null && table == null) { if (element is HtmlTable) { table = (HtmlTable)element; } element = (HtmlElement)element.parentElement; } } // return the defined table element return(table); }
} //GetTableProperties // Determine if the insertion point or selection is a table public bool InsideTable() { // see if a table selected or insertion point inside a table HtmlTable htmlTable = GetTableElement(); // process according to table being defined return(htmlTable != null); }
} //TableInsert // public function to modify a tables properties // ensure a table is currently selected or insertion point is within a table public bool TableModify(HtmlTableProperty tableProperties) { // define the Html Table element HtmlTable table = GetTableElement(); // if a table has been selected then process if (table != null) { ProcessTable(table, tableProperties); return(true); } else { return(false); } } //TableModify
// determine if the current selection is a table // return the table element private void GetTableElement(out HtmlTable table, out HtmlTableRow row, out HtmlTableCell cell) { table = null; row = null; cell = null; HtmlTextRange range = SelectionHelper.GetTextRange(Document); try { // first see if the table element is selected table = SelectionHelper.GetFirstControl(Document) as HtmlTable; // if table not selected then parse up the selection tree if (table == null && range != null) { HtmlElement element = (HtmlElement)range.parentElement(); // parse up the tree until the table element is found while (element != null && table == null) { if (element is HtmlTable) { table = (HtmlTable)element; } else if (element is HtmlTableRow) { row = (HtmlTableRow)element; } else if (element is HtmlTableCell) { cell = (HtmlTableCell)element; } element = (HtmlElement)element.parentElement; } } } catch (Exception) { // have unknown error so set return to null table = null; row = null; cell = null; } } //GetTableElement
public void ColumnMoveRight() { // see if a table selected or insertion point inside a table HtmlTable table = null; HtmlTableRow row = null; HtmlTableCell cell = null; GetTableElement(out table, out row, out cell); if (table != null && row != null && cell != null) { int index = cell.cellIndex; if (index >= row.cells.length - 1) { return; } try { using (new SelectionPreserver(GetMarkupRange())) { using (new UndoUnit(Document, "Table column move right")) { for (int i = 0; i < table.rows.length; i++) { HtmlTableRow r = table.rows.item(i); HtmlDomNode c1 = r.cells.item(index); HtmlDomNode c2 = r.cells.item(index + 1); c1.swapNode(c2); } } } } catch (Exception ex) { throw new HtmlEditorException("Unable to insert a new Row", "TableinsertRow", ex); } } else { throw new HtmlEditorException("Row not currently selected within the table", "TableInsertRow"); } }
// will insert a new row into the table // based on the current user row and insertion after private void InsertRow(bool below = true) { // see if a table selected or insertion point inside a table HtmlTable table = null; HtmlTableRow row = null; HtmlTableCell cell = null; GetTableElement(out table, out row, out cell); // process according to table being defined if (table != null && row != null) { try { using (new SelectionPreserver(GetMarkupRange())) { using (new UndoUnit(Document, "Table row insert")) { // find the existing row the user is on and perform the insertion int index = row.rowIndex + (below ? 1 : 0); HtmlTableRow insertedRow = table.insertRow(index) as HtmlTableRow; // add the new columns to the end of each row int numberCols = row.cells.length; for (int idxCol = 0; idxCol < numberCols; idxCol++) { insertedRow.insertCell(-1); } } } } catch (Exception ex) { throw new HtmlEditorException("Unable to insert a new Row", "TableinsertRow", ex); } } else { throw new HtmlEditorException("Row not currently selected within the table", "TableInsertRow"); } }
private void InsertColumn(bool right = true) { // see if a table selected or insertion point inside a table HtmlTable table = null; HtmlTableRow row = null; HtmlTableCell cell = null; GetTableElement(out table, out row, out cell); // process according to table being defined if (table != null && row != null && cell != null) { try { using (new UndoUnit(Document, "Table column insert")) { // find the existing row the user is on and perform the insertion int index = cell.cellIndex + (right ? 1 : 0); // add the new columns to the end of each row int numberRows = table.rows.length; for (int i = 0; i < numberRows; i++) { HtmlTableRow r = table.rows.item(i) as HtmlTableRow; r.insertCell(index); } table.cols++; } } catch (Exception ex) { throw new HtmlEditorException("Unable to insert a new Row", "TableinsertRow", ex); } } else { throw new HtmlEditorException("Row not currently selected within the table", "TableInsertRow"); } }
public List <CrmWebService.CompanyCmsData> GenerateCCDList(string html, out int totalCount) { List <CrmWebService.CompanyCmsData> ccdList = new List <CompanyCmsData>(); mshtml.HTMLDocumentClass doc = new mshtml.HTMLDocumentClass(); doc.designMode = "on"; doc.IHTMLDocument2_write(html); mshtml.IHTMLElement divcnt = doc.getElementById("cnt"); mshtml.IHTMLElementCollection childrens = (mshtml.IHTMLElementCollection)divcnt.children; mshtml.IHTMLTable table = (mshtml.IHTMLTable)childrens.item(2); for (int i = 1; i < table.rows.length; i++) { CompanyCmsData item = new CompanyCmsData(); mshtml.IHTMLTableRow row = (mshtml.IHTMLTableRow)table.rows.item(i); mshtml.IHTMLElement cell = (mshtml.IHTMLElement)row.cells.item(0); item.CmsId = int.Parse(cell.innerText.Trim()); cell = (mshtml.IHTMLElement)row.cells.item(1); item.CompanyName = string.IsNullOrEmpty(cell.innerText) ? "" : cell.innerText.Trim(); cell = (mshtml.IHTMLElement)row.cells.item(2); item.TTSStatusDesp = string.IsNullOrEmpty(cell.innerText) ? "" : cell.innerText.Trim(); cell = (mshtml.IHTMLElement)row.cells.item(3); item.ContactPhone = string.IsNullOrEmpty(cell.innerText) ? "" : cell.innerText.Trim(); cell = (mshtml.IHTMLElement)row.cells.item(5); item.SalesName = string.IsNullOrEmpty(cell.innerText) ? "" : cell.innerText.Trim(); ccdList.Add(item); } totalCount = 0; mshtml.IHTMLElementCollection eles = doc.getElementsByName("totalCount"); if (eles != null && eles.length > 0) { totalCount = int.Parse(((mshtml.IHTMLElement)eles.item(0)).getAttribute("value").ToString()); } return(ccdList); }
public void RowMoveDown() { // see if a table selected or insertion point inside a table HtmlTable table = null; HtmlTableRow row = null; HtmlTableCell cell = null; GetTableElement(out table, out row, out cell); if (table != null && row != null && cell != null) { if (row.rowIndex >= table.rows.length - 1) { return; } try { using (new SelectionPreserver(GetMarkupRange())) { using (new UndoUnit(Document, "Table row move down")) { HtmlTableRow rowBelow = table.rows.item(row.rowIndex + 1); (row as HtmlDomNode).swapNode(rowBelow as HtmlDomNode); } } } catch (Exception ex) { throw new HtmlEditorException("Unable to insert a new Row", "TableinsertRow", ex); } } else { throw new HtmlEditorException("Row not currently selected within the table", "TableInsertRow"); } }
/// <summary> /// Given an Html Table Element determines the table properties /// Returns the properties as an HtmlTableProperty class /// </summary> private HtmlTableProperty GetTableProperties(mshtmlTable table) { // define a set of base table properties HtmlTableProperty tableProperties = new HtmlTableProperty(true); // if user has selected a table extract those properties if (table != null) { try { // have a table so extract the properties mshtmlTableCaption caption = table.caption; // if have a caption persist the values if (caption != null) { tableProperties.CaptionText = ((mshtmlElement)table.caption).innerText; if (caption.align != null) tableProperties.CaptionAlignment = (HorizontalAlignOption)TryParseEnum(typeof(HorizontalAlignOption), caption.align, HorizontalAlignOption.Default); if (caption.vAlign != null) tableProperties.CaptionLocation = (VerticalAlignOption)TryParseEnum(typeof(VerticalAlignOption), caption.vAlign, VerticalAlignOption.Default); } // look at the table properties if (table.border != null) tableProperties.BorderSize = TryParseByte(table.border.ToString(), tableProperties.BorderSize); if (table.align != null) tableProperties.TableAlignment = (HorizontalAlignOption)TryParseEnum(typeof(HorizontalAlignOption), table.align, HorizontalAlignOption.Default); // define the table rows and columns int rows = Math.Min(table.rows.length, Byte.MaxValue); int cols = Math.Min(table.cols, Byte.MaxValue); if (cols == 0 && rows > 0) { // cols value not set to get the maxiumn number of cells in the rows foreach (mshtmlTableRow tableRow in table.rows) { cols = Math.Max(cols, (int)tableRow.cells.length); } } tableProperties.TableRows = (byte)Math.Min(rows, byte.MaxValue); tableProperties.TableColumns = (byte)Math.Min(cols, byte.MaxValue); // define the remaining table properties if (table.cellPadding != null) tableProperties.CellPadding = TryParseByte(table.cellPadding.ToString(), tableProperties.CellPadding); if (table.cellSpacing != null) tableProperties.CellSpacing = TryParseByte(table.cellSpacing.ToString(), tableProperties.CellSpacing); if (table.width != null) { string tableWidth = table.width.ToString(); if (tableWidth.TrimEnd(null).EndsWith("%")) { tableProperties.TableWidth = TryParseUshort(tableWidth.Remove(tableWidth.LastIndexOf("%"), 1), tableProperties.TableWidth); tableProperties.TableWidthMeasurement = MeasurementOption.Percent; } else { tableProperties.TableWidth = TryParseUshort(tableWidth, tableProperties.TableWidth); tableProperties.TableWidthMeasurement = MeasurementOption.Pixel; } } else { tableProperties.TableWidth = 0; tableProperties.TableWidthMeasurement = MeasurementOption.Pixel; } } catch (Exception ex) { // throw an exception indicating table structure change be determined throw new HtmlEditorException("Unable to determine Html Table properties.", "GetTableProperties", ex); } } // return the table properties return tableProperties; }
/// <summary> /// Method to determine if the current selection is a table /// If found will return the table element /// </summary> private void GetTableElement(out mshtmlTable table, out mshtmlTableRow row) { table = null; row = null; mshtmlTextRange range = GetTextRange(); try { // first see if the table element is selected table = GetFirstControl() as mshtmlTable; // if table not selected then parse up the selection tree if (table == null && range != null) { mshtmlElement element = (mshtmlElement)range.parentElement(); // parse up the tree until the table element is found while (element != null && table == null) { element = (mshtmlElement)element.parentElement; // extract the Table properties if (element is mshtmlTable) { table = (mshtmlTable)element; } // extract the Row properties if (element is mshtmlTableRow) { row = (mshtmlTableRow)element; } } } } catch (Exception) { // have unknown error so set return to null table = null; row = null; } }
} //ProcessTable /// <summary> /// Method to determine if the current selection is a table /// If found will return the table element /// </summary> private void GetTableElement(out mshtmlTable table, out mshtmlTableRow row) { row = null; mshtmlTextRange range = GetTextRange(); try { // first see if the table element is selected table = GetFirstControl() as mshtmlTable; // if table not selected then parse up the selection tree if (table.IsNull() && !range.IsNull()) { var element = range.parentElement(); // parse up the tree until the table element is found while (!element.IsNull() && table.IsNull()) { element = element.parentElement; // extract the Table properties var htmlTable = element as mshtmlTable; if (!htmlTable.IsNull()) { table = htmlTable; } // extract the Row properties var htmlTableRow = element as mshtmlTableRow; if (!htmlTableRow.IsNull()) { row = htmlTableRow; } } } } catch (Exception) { // have unknown error so set return to null table = null; row = null; } } //GetTableElement
public bool Get(ref mshtml.IHTMLTable table, bool tableCreated) { if (table == null) { return(false); } // define the table border, width, cell padding and spacing object bgColor, borderColor; base.Get(out bgColor, out borderColor); table.bgColor = bgColor; table.borderColor = borderColor; if (this.TableWidth > 0) { table.width = (this.TableWidthMeasurement == MeasurementOption.Pixel) ? string.Format("{0}", this.TableWidth) : string.Format("{0}%", this.TableWidth); } else { table.width = string.Empty; } if (this.TableAlignment != HorizontalAlignOption.Default) { table.align = this.TableAlignment.ToString().ToLower(); } else { table.align = string.Empty; } table.border = this.BorderSize; table.cellPadding = this.CellPadding.ToString(); table.cellSpacing = this.CellSpacing.ToString(); // define the given table caption and alignment string caption = this.CaptionText; mshtml.IHTMLTableCaption tableCaption = table.caption; if (caption != null && caption != string.Empty) { // ensure table caption correctly defined if (tableCaption == null) { tableCaption = table.createCaption(); } ((mshtml.IHTMLElement)tableCaption).innerText = caption; if (this.CaptionAlignment != HorizontalAlignOption.Default) { tableCaption.align = this.CaptionAlignment.ToString().ToLower(); } if (this.CaptionLocation != VerticalAlignOption.Default) { tableCaption.vAlign = this.CaptionLocation.ToString().ToLower(); } } else { // if no caption specified remove the existing one if (tableCaption != null) { // prior to deleting the caption the contents must be cleared ((mshtml.IHTMLElement)tableCaption).innerText = null; table.deleteCaption(); } } // determine the number of rows one has to insert int numberRows, numberCols; if (tableCreated) { numberRows = Math.Max((int)this.TableRows, 1); } else { numberRows = Math.Max((int)this.TableRows, 1) - (int)table.rows.length; } // layout the table structure in terms of rows and columns table.cols = (int)this.TableColumns; if (tableCreated) { // this section is an optimization based on creating a new table // the section below works but not as efficiently numberCols = Math.Max((int)this.TableColumns, 1); // insert the appropriate number of rows mshtml.IHTMLTableRow tableRow; for (int idxRow = 0; idxRow < numberRows; idxRow++) { tableRow = (mshtml.IHTMLTableRow)table.insertRow(-1); // add the new columns to the end of each row for (int idxCol = 0; idxCol < numberCols; idxCol++) { tableRow.insertCell(-1); } } } else { // if the number of rows is increasing insert the decrepency if (numberRows > 0) { // insert the appropriate number of rows for (int idxRow = 0; idxRow < numberRows; idxRow++) { table.insertRow(-1); } } else { // remove the extra rows from the table for (int idxRow = numberRows; idxRow < 0; idxRow++) { table.deleteRow(table.rows.length - 1); } } // have the rows constructed // now ensure the columns are correctly defined for each row mshtml.IHTMLElementCollection rows = table.rows; foreach (mshtml.IHTMLTableRow tableRow in rows) { numberCols = Math.Max((int)this.TableColumns, 1) - (int)tableRow.cells.length; if (numberCols > 0) { // add the new column to the end of each row for (int idxCol = 0; idxCol < numberCols; idxCol++) { tableRow.insertCell(-1); } } else { // reduce the number of cells in the given row // remove the extra rows from the table for (int idxCol = numberCols; idxCol < 0; idxCol++) { tableRow.deleteCell(tableRow.cells.length - 1); } } } } return(true); }
public bool Set(mshtml.IHTMLTable table) { if (table == null) { return(false); } try { base.Set(table.bgColor, table.borderColor); // have a table so extract the properties mshtml.IHTMLTableCaption caption = table.caption; // if have a caption persist the values if (caption != null) { this.CaptionText = ((mshtml.IHTMLElement)table.caption).innerText; if (caption.align != null) { this.CaptionAlignment = (HorizontalAlignOption)Utils.TryParseEnum(typeof(HorizontalAlignOption), caption.align, HorizontalAlignOption.Default); } if (caption.vAlign != null) { this.CaptionLocation = (VerticalAlignOption)Utils.TryParseEnum(typeof(VerticalAlignOption), caption.vAlign, VerticalAlignOption.Default); } } // look at the table properties if (table.border != null) { this.BorderSize = Utils.TryParseByte(table.border.ToString(), this.BorderSize); } if (table.align != null) { this.TableAlignment = (HorizontalAlignOption)Utils.TryParseEnum(typeof(HorizontalAlignOption), table.align, HorizontalAlignOption.Default); } // define the table rows and columns int rows = Math.Min(table.rows.length, Byte.MaxValue); int cols = Math.Min(table.cols, Byte.MaxValue); if (cols == 0 && rows > 0) { // cols value not set to get the maxiumn number of cells in the rows foreach (mshtml.IHTMLTableRow tableRow in table.rows) { cols = Math.Max(cols, (int)tableRow.cells.length); } } this.TableRows = (byte)Math.Min(rows, byte.MaxValue); this.TableColumns = (byte)Math.Min(cols, byte.MaxValue); // define the remaining table properties if (table.cellPadding != null) { this.CellPadding = Utils.TryParseByte(table.cellPadding.ToString(), this.CellPadding); } if (table.cellSpacing != null) { this.CellSpacing = Utils.TryParseByte(table.cellSpacing.ToString(), this.CellSpacing); } if (table.width != null) { string tableWidth = table.width.ToString(); if (tableWidth.TrimEnd(null).EndsWith("%")) { this.TableWidth = Utils.TryParseUshort(tableWidth.Remove(tableWidth.LastIndexOf("%"), 1), this.TableWidth); this.TableWidthMeasurement = MeasurementOption.Percent; } else { this.TableWidth = Utils.TryParseUshort(tableWidth, this.TableWidth); this.TableWidthMeasurement = MeasurementOption.Pixel; } } else { this.TableWidth = 0; this.TableWidthMeasurement = MeasurementOption.Pixel; } } catch (Exception ex) { // throw an exception indicating table structure change be determined throw new HtmlEditorException("Unable to determine Html Table properties.", "GetTableProperties", ex); } return(true); }
public HtmlTableProperty(mshtml.IHTMLTable table, bool htmlDefaults) : this(htmlDefaults) { Set(table); }
private static HtmlTableCell GetCell(HtmlTable table, int row, int col) { return(table.rows.item(row).cells.item(col) as HtmlTableCell); }
} //TableModify public void TableModify(HtmlTable table, HtmlTableProperty tableProperties) { ProcessTable(table, tableProperties); }
/// <summary> /// Method to insert a basic table /// Will honour the existing table if passed in /// </summary> private void ProcessTable(mshtmlTable table, HtmlTableProperty tableProperties) { try { // obtain a reference to the body node and indicate table present mshtmlDomNode bodyNode = (mshtmlDomNode)document.body; bool tableCreated = false; // ensure a table node has been defined to work with if (table == null) { // create the table and indicate it was created table = (mshtmlTable)document.createElement(TABLE_TAG); tableCreated = true; } // define the table border, width, cell padding and spacing table.border = tableProperties.BorderSize; if (tableProperties.TableWidth > 0) table.width = (tableProperties.TableWidthMeasurement == MeasurementOption.Pixel) ? string.Format("{0}", tableProperties.TableWidth) : string.Format("{0}%", tableProperties.TableWidth); else table.width = string.Empty; if (tableProperties.TableAlignment != HorizontalAlignOption.Default) table.align = tableProperties.TableAlignment.ToString().ToLower(); else table.align = string.Empty; table.cellPadding = tableProperties.CellPadding.ToString(); table.cellSpacing = tableProperties.CellSpacing.ToString(); // define the given table caption and alignment string caption = tableProperties.CaptionText; mshtmlTableCaption tableCaption = table.caption; if (caption != null && caption != string.Empty) { // ensure table caption correctly defined if (tableCaption == null) tableCaption = table.createCaption(); ((mshtmlElement)tableCaption).innerText = caption; if (tableProperties.CaptionAlignment != HorizontalAlignOption.Default) tableCaption.align = tableProperties.CaptionAlignment.ToString().ToLower(); if (tableProperties.CaptionLocation != VerticalAlignOption.Default) tableCaption.vAlign = tableProperties.CaptionLocation.ToString().ToLower(); } else { // if no caption specified remove the existing one if (tableCaption != null) { // prior to deleting the caption the contents must be cleared ((mshtmlElement)tableCaption).innerText = null; table.deleteCaption(); } } // determine the number of rows one has to insert int numberRows, numberCols; if (tableCreated) { numberRows = Math.Max((int)tableProperties.TableRows, 1); } else { numberRows = Math.Max((int)tableProperties.TableRows, 1) - (int)table.rows.length; } // layout the table structure in terms of rows and columns table.cols = (int)tableProperties.TableColumns; if (tableCreated) { // this section is an optimization based on creating a new table // the section below works but not as efficiently numberCols = Math.Max((int)tableProperties.TableColumns, 1); // insert the appropriate number of rows mshtmlTableRow tableRow; for (int idxRow = 0; idxRow < numberRows; idxRow++) { tableRow = (mshtmlTableRow)table.insertRow(-1); // add the new columns to the end of each row for (int idxCol = 0; idxCol < numberCols; idxCol++) { tableRow.insertCell(-1); } } } else { // if the number of rows is increasing insert the decrepency if (numberRows > 0) { // insert the appropriate number of rows for (int idxRow = 0; idxRow < numberRows; idxRow++) { table.insertRow(-1); } } else { // remove the extra rows from the table for (int idxRow = numberRows; idxRow < 0; idxRow++) { table.deleteRow(table.rows.length - 1); } } // have the rows constructed // now ensure the columns are correctly defined for each row mshtmlElementCollection rows = table.rows; foreach (mshtmlTableRow tableRow in rows) { numberCols = Math.Max((int)tableProperties.TableColumns, 1) - (int)tableRow.cells.length; if (numberCols > 0) { // add the new column to the end of each row for (int idxCol = 0; idxCol < numberCols; idxCol++) { tableRow.insertCell(-1); } } else { // reduce the number of cells in the given row // remove the extra rows from the table for (int idxCol = numberCols; idxCol < 0; idxCol++) { tableRow.deleteCell(tableRow.cells.length - 1); } } } } // if the table was created then it requires insertion into the DOM // otherwise property changes are sufficient if (tableCreated) { // table processing all complete so insert into the DOM mshtmlDomNode tableNode = (mshtmlDomNode)table; mshtmlElement tableElement = (mshtmlElement)table; mshtmlSelection selection = document.selection; mshtmlTextRange textRange = GetTextRange(); // final insert dependant on what user has selected if (textRange != null) { // text range selected so overwrite with a table try { string selectedText = textRange.text; if (selectedText != null) { // place selected text into first cell mshtmlTableRow tableRow = (mshtmlTableRow)table.rows.item(0, null); ((mshtmlElement)tableRow.cells.item(0, null)).innerText = selectedText; } textRange.pasteHTML(tableElement.outerHTML); } catch (Exception ex) { throw new HtmlEditorException("Invalid Text selection for the Insertion of a Table.", "ProcessTable", ex); } } else { mshtmlControlRange controlRange = GetAllControls(); if (controlRange != null) { // overwrite any controls the user has selected try { // clear the selection and insert the table // only valid if multiple selection is enabled for (int idx = 1; idx < controlRange.length; idx++) { controlRange.remove(idx); } controlRange.item(0).outerHTML = tableElement.outerHTML; // this should work with initial count set to zero // controlRange.add((mshtmlControlElement)table); } catch (Exception ex) { throw new HtmlEditorException("Cannot Delete all previously Controls selected.", "ProcessTable", ex); } } else { // insert the table at the end of the HTML bodyNode.appendChild(tableNode); } } } else { // table has been correctly defined as being the first selected item // need to remove other selected items mshtmlControlRange controlRange = GetAllControls(); if (controlRange != null) { // clear the controls selected other than than the first table // only valid if multiple selection is enabled for (int idx = 1; idx < controlRange.length; idx++) { controlRange.remove(idx); } } } } catch (Exception ex) { // throw an exception indicating table structure change error throw new HtmlEditorException("Unable to modify Html Table properties.", "ProcessTable", ex); } }
// function to insert a basic table // will honour the existing table if passed in private void ProcessTable(HtmlTable table, HtmlTableProperty tableProperties) { try { using (new UndoUnit(Document, "Table add/modify")) { // obtain a reference to the body node and indicate table present HtmlDomNode bodyNode = (HtmlDomNode)Document.body; bool tableCreated = false; MsHtmlWrap.MarkupRange targetMarkupRange = null; // ensure a table node has been defined to work with if (table == null) { // create the table and indicate it was created table = (HtmlTable)Document.createElement("TABLE"); tableCreated = true; //markup range for selecting first cell after table creation targetMarkupRange = GetMarkupRange(); } // define the table border, width, cell padding and spacing table.border = tableProperties.BorderSize; if (tableProperties.TableWidth > 0) { table.width = (tableProperties.TableWidthMeasurement == MeasurementOption.Pixel) ? string.Format("{0}", tableProperties.TableWidth) : string.Format("{0}%", tableProperties.TableWidth); } else { table.width = string.Empty; } if (tableProperties.TableAlignment != HorizontalAlignOption.Default) { table.align = tableProperties.TableAlignment.ToString().ToLower(); } else { table.align = string.Empty; } table.cellPadding = tableProperties.CellPadding.ToString(); table.cellSpacing = tableProperties.CellSpacing.ToString(); // define the given table caption and alignment string caption = tableProperties.CaptionText; HtmlTableCaption tableCaption = table.caption; if (caption != null && caption != string.Empty) { // ensure table caption correctly defined if (tableCaption == null) { tableCaption = table.createCaption(); } ((HtmlElement)tableCaption).innerText = caption; if (tableProperties.CaptionAlignment != HorizontalAlignOption.Default) { tableCaption.align = tableProperties.CaptionAlignment.ToString().ToLower(); } if (tableProperties.CaptionLocation != VerticalAlignOption.Default) { tableCaption.vAlign = tableProperties.CaptionLocation.ToString().ToLower(); } } else { // if no caption specified remove the existing one if (tableCaption != null) { // prior to deleting the caption the contents must be cleared ((HtmlElement)tableCaption).innerText = null; table.deleteCaption(); } } // determine the number of rows one has to insert int numberRows, numberCols; if (tableCreated) { numberRows = Math.Max((int)tableProperties.TableRows, 1); } else { numberRows = Math.Max((int)tableProperties.TableRows, 1) - (int)table.rows.length; } // layout the table structure in terms of rows and columns table.cols = (int)tableProperties.TableColumns; if (tableCreated) { // this section is an optimization based on creating a new table // the section below works but not as efficiently numberCols = Math.Max((int)tableProperties.TableColumns, 1); // insert the appropriate number of rows HtmlTableRow tableRow; for (int idxRow = 0; idxRow < numberRows; idxRow++) { tableRow = table.insertRow(-1) as HtmlTableRow; // add the new columns to the end of each row for (int idxCol = 0; idxCol < numberCols; idxCol++) { tableRow.insertCell(-1); } } } else { // if the number of rows is increasing insert the decrepency if (numberRows > 0) { // insert the appropriate number of rows for (int idxRow = 0; idxRow < numberRows; idxRow++) { table.insertRow(-1); } } else { // remove the extra rows from the table for (int idxRow = numberRows; idxRow < 0; idxRow++) { table.deleteRow(table.rows.length - 1); } } // have the rows constructed // now ensure the columns are correctly defined for each row HtmlElementCollection rows = table.rows; foreach (HtmlTableRow tableRow in rows) { numberCols = Math.Max((int)tableProperties.TableColumns, 1) - (int)tableRow.cells.length; if (numberCols > 0) { // add the new column to the end of each row for (int idxCol = 0; idxCol < numberCols; idxCol++) { tableRow.insertCell(-1); } } else { // reduce the number of cells in the given row // remove the extra rows from the table for (int idxCol = numberCols; idxCol < 0; idxCol++) { tableRow.deleteCell(tableRow.cells.length - 1); } } } } // if the table was created then it requires insertion into the DOM // otherwise property changes are sufficient if (tableCreated) { // table processing all complete so insert into the DOM HtmlDomNode tableNode = (HtmlDomNode)table; HtmlElement tableElement = (HtmlElement)table; HtmlSelection selection = Document.selection; HtmlTextRange textRange = SelectionHelper.GetTextRange(Document); // final insert dependant on what user has selected if (textRange != null) { // text range selected so overwrite with a table try { string selectedText = textRange.text; if (selectedText != null) { // place selected text into first cell HtmlTableRow tableRow = table.rows.item(0, null) as HtmlTableRow; (tableRow.cells.item(0, null) as HtmlElement).innerText = selectedText; } textRange.pasteHTML(tableElement.outerHTML); } catch (Exception ex) { throw new HtmlEditorException("Invalid Text selection for the Insertion of a Table.", "ProcessTable", ex); } } else { HtmlControlRange controlRange = SelectionHelper.GetAllControls(Document); if (controlRange != null) { // overwrite any controls the user has selected try { // clear the selection and insert the table // only valid if multiple selection is enabled for (int idx = 1; idx < controlRange.length; idx++) { controlRange.remove(idx); } controlRange.item(0).outerHTML = tableElement.outerHTML; // this should work with initial count set to zero // controlRange.add((HtmlControlElement)table); } catch (Exception ex) { throw new HtmlEditorException("Cannot Delete all previously Controls selected.", "ProcessTable", ex); } } else { // insert the table at the end of the HTML bodyNode.appendChild(tableNode); } } } else { // table has been correctly defined as being the first selected item // need to remove other selected items HtmlControlRange controlRange = SelectionHelper.GetAllControls(Document); if (controlRange != null) { // clear the controls selected other than than the first table // only valid if multiple selection is enabled for (int idx = 1; idx < controlRange.length; idx++) { controlRange.remove(idx); } } } //if table created, then focus the first cell if (tableCreated) { try { HtmlElement cell = targetMarkupRange.GetFirstElement(e => e is HtmlTableCell, true); if (cell != null) { SelectCell(cell as HtmlTableCell); } } catch (Exception e) { Log.Write(e); } } } } catch (Exception ex) { // throw an exception indicating table structure change error throw new HtmlEditorException("Unable to modify Html Table properties.", "ProcessTable", ex); } } //ProcessTable
/// <summary> /// Method to present to the user the table properties dialog /// Uses all the default properties for the table based on an insert operation /// </summary> private void ProcessTablePrompt(mshtmlTable table) { using (TablePropertyForm dialog = new TablePropertyForm()) { // define the base set of table properties HtmlTableProperty tableProperties = GetTableProperties(table); // set the dialog properties dialog.TableProperties = tableProperties; DefineDialogProperties(dialog); // based on the user interaction perform the neccessary action if (dialog.ShowDialog(this.ParentForm) == DialogResult.OK) { tableProperties = dialog.TableProperties; if (table == null) TableInsert(tableProperties); else ProcessTable(table, tableProperties); } } }
//実行 MAIN public void DoAll() { //SavePdf("aaaa.csv") Pub_Com = new Com("納期指定発注" + DateTime.Now.ToString("yyyyMMddHHmmss")); if (Pub_Com.file_list_Nouki.Count == 0) { ProBar = 100; return; } lv1 = System.Convert.ToDecimal(90 / Pub_Com.file_list_Nouki.Count); lv2 = lv1 / 15; //一回目 bool firsOpenKbn = true; object authHeader = "Authorization: Basic " + Convert.ToBase64String(System.Text.UnicodeEncoding.UTF8.GetBytes(string.Format("{0}:{1}", Pub_Com.user, Pub_Com.password))) + "\\r\\n"; //*** OnSiteパスワード入力画面 Ie.Navigate(Pub_Com.url, null, null, null, authHeader); Ie.Silent = true; Ie.Visible = IeVisible; ProBar = 5; //***ログイン DoStep1_Login(); //CSV ファイルs 取込 ProBar = 10; for (int fileIdx = 0; fileIdx <= Pub_Com.file_list_Nouki.Count - 1; fileIdx++) { string csvFileName = System.Convert.ToString(Pub_Com.file_list_Nouki[fileIdx].ToString().Trim()); string[] csvNameSplitor = csvFileName.Split('-'); string 事業所 = csvNameSplitor[0]; string 得意先 = csvNameSplitor[1]; string 店 = csvNameSplitor[2]; string 現場名 = csvNameSplitor[3]; string 備考 = csvNameSplitor[4]; string 日付連番 = csvNameSplitor[5]; //一回目ではなく 実行 if (firsOpenKbn == false) { Pub_Com.GetElementBy(ref Ie, "fraHead", "input", "value", "絞込検索").click(); Pub_Com.SleepAndWaitComplete(Ie); } firsOpenKbn = false; AddProBar(lv2); //1 Pub_Com.AddMsg("取込:" + Pub_Com.file_list_Nouki[fileIdx].ToString().Trim()); //見積検索 Pub_Com.AddMsg("見積検索"); DoStep1_PoupuSentaku(事業所, 得意先, 店, 現場名, 備考, 日付連番, csvFileName); Pub_Com.SleepAndWaitComplete(Ie); AddProBar(lv2); //2 //納期日設定 if (!DoStep2_Set()) { continue; } Pub_Com.SleepAndWaitComplete(Ie); Pub_Com.SleepAndWaitComplete(Ie); Pub_Com.SleepAndWaitComplete(Ie); //該当データがありません NEXT mshtml.HTMLWindow2 fraTmp = Pub_Com.GetFrameByName(ref Ie, "fraHyou"); if (fraTmp != null) { if (fraTmp.document.body.innerText.IndexOf("該当データがありません") >= 0) { continue; } } //CSVファイル内容取込 string[] csvDataLines = System.IO.File.ReadAllLines(Pub_Com.folder_Nouki + csvFileName); string code = ""; string nouki = ""; AddProBar(lv2); //3 mshtml.HTMLWindow2 fra = Pub_Com.GetFrameWait(ref Ie, "fraMitBody"); mshtml.HTMLDocument Doc = (mshtml.HTMLDocument)fra.document; mshtml.IHTMLElementCollection eles = Doc.getElementsByTagName("input"); //Radio 明細Key mshtml.IHTMLElementCollection cbEles = Doc.getElementsByName("strMeisaiKey"); //指定納期 mshtml.IHTMLElementCollection nouhinDateEles = Doc.getElementsByName("strSiteiNouhinDate"); int csvIdx = 0; int sameCdSuu = 0; int gamenSameCdSuu = 0; //CSV LINES for (int csvLinesIdx = 0; csvLinesIdx <= csvDataLines.Length - 1; csvLinesIdx++) { if (!string.IsNullOrEmpty(csvDataLines[csvLinesIdx].Trim())) { //コード 納期 code = System.Convert.ToString(csvDataLines[csvLinesIdx].Split(',')[1].Trim()); nouki = System.Convert.ToString((System.Convert.ToDateTime(csvDataLines[csvLinesIdx].Split(',')[2].Trim())).ToString("yyyy/MM/dd")); sameCdSuu = 0; gamenSameCdSuu = 0; for (csvIdx = 0; csvIdx <= csvLinesIdx; csvIdx++) { if (csvDataLines[csvIdx].Split(',')[1].Trim() == code) { sameCdSuu++; } } for (int i = 0; i <= cbEles.length - 1; i++) { mshtml.IHTMLElement cb = (mshtml.IHTMLElement)(cbEles.item(i)); mshtml.IHTMLTableRow tr = (mshtml.IHTMLTableRow)cb.parentElement.parentElement; mshtml.HTMLTableCell td = (mshtml.HTMLTableCell)(tr.cells.item(1)); mshtml.IHTMLTable table = (mshtml.IHTMLTable)cb.parentElement.parentElement.parentElement.parentElement; bool isHaveDate = false; if (td.innerText == code) { gamenSameCdSuu++; if (sameCdSuu == gamenSameCdSuu) { mshtml.IHTMLSelectElement sel = (mshtml.IHTMLSelectElement)(nouhinDateEles.item(i)); for (int j = 0; j <= sel.length - 1; j++) { mshtml.IHTMLOptionElement opEle = (mshtml.IHTMLOptionElement)(sel.item(j)); if (opEle.value.IndexOf(nouki) > 0) { opEle.selected = true; isHaveDate = true; break; } } } else { continue; } if (!isHaveDate) { MessageBox.Show("コード:[" + code + "] 納品希望日:[" + nouki + "]がありません"); return; } } } } } AddProBar(lv2); //4 Pub_Com.GetElementBy(ref Ie, "fraMitBody", "select", "name", "strBukkenKbn").setAttribute("value", "01"); Pub_Com.GetElementBy(ref Ie, "fraMitBody", "input", "value", "発 注").click(); Pub_Com.SleepAndWaitComplete(Ie); Pub_Com.SleepAndWaitComplete(Ie); Pub_Com.SleepAndWaitComplete(Ie); AddProBar(lv2); //5 Pub_Com.GetElementBy(ref Ie, "fraMitBody", "input", "value", "発注結果照会へ").click(); Pub_Com.SleepAndWaitComplete(Ie); AddProBar(lv2); //6 //PDF 印刷 if (insatu) { SHDocVw.InternetExplorerMedium childIe = default(SHDocVw.InternetExplorerMedium); int RebackKaisu = -1; Reback: RebackKaisu++; Com.Sleep5(1000); //前回印刷画面 Close ClosePrintPage(); Pub_Com.GetElementBy(ref Ie, "fraMitBody", "input", "value", "結果印刷").click(); int wait_print; wait_print = int.Parse(Com.GetAppSetting("wait_print")); AutoResetEvent myEvent = new AutoResetEvent(false); myEvent.WaitOne(wait_print * 1000); myEvent.Close(); try { //印刷画面取得する childIe = GetPrintPage(); } catch (Exception) { } Com.Sleep5(1000); //IE エラー判定する if (GetErrCon() == false) { Com.Sleep5(1000); if (RebackKaisu <= 1) { goto Reback; } else { if (MessageBox.Show("帳票Download エラーしました、終了ですか?", "Confirm Message", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { System.Environment.Exit(0); } } } string flName = Pub_Com.pdfPath + csvFileName; try { bool rtv = GetFcwInfo(childIe, flName); ClosePrintPage(); if (rtv == false) { if (RebackKaisu <= 1) { goto Reback; } else { if (MessageBox.Show("帳票Download エラーしました、終了ですか?", "Confirm Message", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { System.Environment.Exit(0); } } } } catch (Exception) { ClosePrintPage(); if (RebackKaisu <= 1) { goto Reback; } else { if (MessageBox.Show("帳票Download エラーしました、終了ですか?", "Confirm Message", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { System.Environment.Exit(0); } } } } AddProBar(lv2); //10 Pub_Com.AddMsg("移動CSV:" + csvFileName + "→" + Pub_Com.folder_Nouki_kanryou); Com.MoveFile(Pub_Com.folder_Nouki + csvFileName, Pub_Com.folder_Nouki_kanryou + csvFileName); AddProBar(lv2); //11 Pub_Com.GetElementBy(ref Ie, "fraMitMenu", "a", "innertext", "[見積一覧を再表示]").click(); Pub_Com.SleepAndWaitComplete(Ie); } ProBar = 100; }