public static IHTMLTableRow AddTableRowsAfterRow( IHTMLTable table, int afterRowIndex, int count) { var columnCount = CountTableColumns(table); IHTMLTableRow lastRow = null; // Add rows. for (var j = 0; j < count; ++j) { var row = (IHTMLTableRow)table.insertRow(afterRowIndex); lastRow = row; // Add columns in the newly added row. while (row.cells.length < columnCount) { var cell = (IHTMLTableCell)row.insertCell(); var element = (IHTMLElement)cell; element.innerHTML = @" "; } } return(lastRow); }
public static IHTMLTableRow AddTableRowsAfterRow( IHTMLTable table, int afterRowIndex, int count) { var columnCount = CountTableColumns(table); IHTMLTableRow lastRow = null; // Add rows. for (var j = 0; j < count; ++j) { var row = (IHTMLTableRow)table.insertRow(afterRowIndex); lastRow = row; // Add columns in the newly added row. while (row.cells.length < columnCount) { var cell = (IHTMLTableCell)row.insertCell(); var element = (IHTMLElement)cell; element.innerHTML = @" "; } } return lastRow; }
/// <summary> /// Inserts a new table in the document /// </summary> /// <param name="id">The ID of the table</param> /// <param name="rows">The number of rows</param> /// <param name="cols">The number of columns</param> /// <param name="borderSize">The border size</param> /// <param name="borderColor">The border color</param> public void InsertTable(string id, int rows, int cols, int borderSize = 0, string borderColor = null) { IHTMLTxtRange range = this.document.selection.createRange() as IHTMLTxtRange; range.pasteHTML("<table id=\"" + id + "\" style=\"position: absolute;\"></table>"); IHTMLTable table = this.GetElementById(id) as IHTMLTable; if (table != null) { for (int i = 0; i < rows; i++) { table.insertRow(i - 1); } table.cellPadding = "10"; foreach (IHTMLTableRow row in table.rows) { for (int i = 0; i < cols; i++) { IHTMLTableCell insertedCell = row.insertCell(i - 1); (insertedCell as IHTMLElement).innerHTML = " "; } } if (borderSize >= 0) { (table as IHTMLElement).style.border = borderSize + "px solid " + borderColor; } else { throw new ArgumentOutOfRangeException("The border size should be a positive number"); } this.AddStyle(id + " td", "border: " + borderSize + "px solid " + borderColor); this.AddStyle(id, (table as IHTMLElement).style.cssText); StringBuilder strBuilder = new StringBuilder(this.HTMLContent); int startIndexToDelete = this.HTMLContent.IndexOf("style=\"", HTMLContent.LastIndexOf("<table")) + 7; int endIndexToDelete = this.HTMLContent.IndexOf("position:", startIndexToDelete); strBuilder.Remove(startIndexToDelete, endIndexToDelete - startIndexToDelete); this.HTMLContent = strBuilder.ToString(); this.MakeInsertedElementMovable(); } }
public void InsertRow(IHTMLTable table, int index, int numberofcells) { IHTMLTableRow row = table.insertRow(index) as IHTMLTableRow; if (row == null) { return; } CalculateCellWidths(numberofcells); for (int j = 0; j < numberofcells; j++) { if ((j + 1) == numberofcells) { Row_InsertCell(row, -1, m_lastcellwidth); } else { Row_InsertCell(row, -1, m_cellwidth); } } }
public void InsertRow(IHTMLTable table, int index, int numberofcells) { IHTMLTableRow row = table.insertRow(index) as IHTMLTableRow; if (row == null) return; CalculateCellWidths(numberofcells); for (int j = 0; j < numberofcells; j++) { if ((j + 1) == numberofcells) Row_InsertCell(row, -1, m_lastcellwidth); else Row_InsertCell(row, -1, m_cellwidth); } }
private void buttonOK_Click( object sender, EventArgs e) { if (IsNew) { var tmpHtml = string.Format( @"<table border=""{0}"" cellpadding=""{1}"" cellspacing=""{2}"" width=""90%"">", borderUpDown.Value, cellPaddingUpDown.Value, cellSpacingUpDown.Value) + Environment.NewLine; for (var row = 0; row < rowsUpDown.Value; ++row) { tmpHtml += @" <tr>" + Environment.NewLine; for (var column = 0; column < columnsUpDown.Value; ++column) { if (row == 0 && firstRowContainsHeadlineCheckBox.Checked) { tmpHtml += string.Format( @" <th{0}{1}></th>" + Environment.NewLine, calculateHorizontalAlignment(), calculateVerticalAlignment()); } else { tmpHtml += string.Format( @" <td{0}{1}></td>" + Environment.NewLine, calculateHorizontalAlignment(), calculateVerticalAlignment()); } } tmpHtml += @" </tr>" + Environment.NewLine; } tmpHtml += @"</table>" + Environment.NewLine; _html = tmpHtml; } else { // Modify existing. // Add rows. while (_table.rows.length < rowsUpDown.Value) { _table.insertRow(); } // Add columns. for (var i = 0; i < _table.rows.length; ++i) { var row = (IHTMLTableRow)_table.rows.item(i, i); while (row.cells.length < columnsUpDown.Value) { var cell = (IHTMLTableCell)row.insertCell(); var element = (IHTMLElement)cell; element.innerHTML = @" "; } } _table.border = ConvertHelper.ToInt32(borderUpDown.Value); _table.cellSpacing = ConvertHelper.ToInt32(cellSpacingUpDown.Value); _table.cellPadding = ConvertHelper.ToInt32(cellPaddingUpDown.Value); } // -- if (ExternalInformationProvider != null) { ExternalInformationProvider.SavePerUserPerWorkstationValue(StoreID + @"HtmlEditorTableNewDialog.RowCount", rowsUpDown.Value.ToString(CultureInfo.InvariantCulture)); ExternalInformationProvider.SavePerUserPerWorkstationValue(StoreID + @"HtmlEditorTableNewDialog.ColCount", columnsUpDown.Value.ToString(CultureInfo.InvariantCulture)); ExternalInformationProvider.SavePerUserPerWorkstationValue(StoreID + @"HtmlEditorTableNewDialog.Border", borderUpDown.Value.ToString(CultureInfo.InvariantCulture)); ExternalInformationProvider.SavePerUserPerWorkstationValue(StoreID + @"HtmlEditorTableNewDialog.CellSpacing", cellSpacingUpDown.Value.ToString(CultureInfo.InvariantCulture)); ExternalInformationProvider.SavePerUserPerWorkstationValue(StoreID + @"HtmlEditorTableNewDialog.CellPadding", cellPaddingUpDown.Value.ToString(CultureInfo.InvariantCulture)); ExternalInformationProvider.SavePerUserPerWorkstationValue(StoreID + @"HtmlEditorTableNewDialog.HorizontalAlignmentIndex", horizontalAlignmentComboBox.SelectedIndex.ToString(CultureInfo.InvariantCulture)); ExternalInformationProvider.SavePerUserPerWorkstationValue(StoreID + @"HtmlEditorTableNewDialog.VerticalAlignmentIndex", verticalAlignmentComboBox.SelectedIndex.ToString(CultureInfo.InvariantCulture)); } }
/// <summary> /// Method to insert a basic table /// Will honour the existing table if passed in /// </summary> private void ProcessTable(IHTMLTable table, HtmlTableProperty tableProperties) { try { // obtain a reference to the body node and indicate table present var document = ContentUtils.GetFocusedHtmlDocument(); var bodyNode = document?.body; bool tableCreated = false; // ensure a table node has been defined to work with if (table.IsNull()) { // create the table and indicate it was created table = (IHTMLTable)document.createElement("<table>"); 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; } table.align = tableProperties.TableAlignment != HorizontalAlignOption.Default ? tableProperties.TableAlignment.ToString().ToLower() : string.Empty; table.cellPadding = tableProperties.CellPadding.ToString(CultureInfo.InvariantCulture); table.cellSpacing = tableProperties.CellSpacing.ToString(CultureInfo.InvariantCulture); // define the given table caption and alignment string caption = tableProperties.CaptionText; IHTMLTableCaption tableCaption = table.caption; if (!caption.IsNullOrEmpty()) { // ensure table caption correctly defined if (tableCaption.IsNull()) { tableCaption = table.createCaption(); } ((IHTMLElement)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.IsNull()) { // prior to deleting the caption the contents must be cleared ((IHTMLElement)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) - table.rows.length; } // layout the table structure in terms of rows and columns table.cols = 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 IHTMLTableRow tableRow; for (int idxRow = 0; idxRow < numberRows; idxRow++) { tableRow = (IHTMLTableRow)table.insertRow(); // add the new columns to the end of each row for (int idxCol = 0; idxCol < numberCols; idxCol++) { tableRow.insertCell(); } } } 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(); } } 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 IHTMLElementCollection rows = table.rows; foreach (IHTMLTableRow tableRow in rows) { numberCols = Math.Max((int)tableProperties.TableColumns, 1) - 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(); } } 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 var tableNode = (IHTMLDOMNode)table; var tableElement = (IHTMLElement)table; IHTMLTxtRange textRange = ContentUtils.GetTextSelectionObject(); // final insert dependant on what user has selected if (!textRange.IsNull()) { // text range selected so overwrite with a table try { string selectedText = textRange.text; if (!selectedText.IsNull()) { // place selected text into first cell var tableRow = (IHTMLTableRow)table.rows.item(0, null); ((IHTMLElement)tableRow.cells.item(0, null)).innerText = selectedText; } textRange.pasteHTML(tableElement.outerHTML); } catch (RemotingException) { } catch (UnauthorizedAccessException) { } } else { IHTMLControlRange controlRange = ContentUtils.GetControlSelectionObject(); if (!controlRange.IsNull()) { // 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 (RemotingException) { } catch (UnauthorizedAccessException) { } } else { // insert the table at the end of the HTML ((IHTMLDOMNode)bodyNode).appendChild(tableNode); } } } else { // table has been correctly defined as being the first selected item // need to remove other selected items IHTMLControlRange controlRange = ContentUtils.GetControlSelectionObject(); if (!controlRange.IsNull()) { // 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 (RemotingException) { } catch (UnauthorizedAccessException) { } } //ProcessTable
//bordersize 2 or "2" public bool AppendTable(int colnum, int rownum, int bordersize, string alignment, int cellpadding, int cellspacing, string widthpercentage, int widthpixel, string backcolor, string bordercolor, string lightbordercolor, string darkbordercolor) { if (m_pDoc2 == null) { return(false); } IHTMLTable t = (IHTMLTable)m_pDoc2.createElement("table"); //set the cols t.cols = colnum; t.border = bordersize; if (!string.IsNullOrEmpty(alignment)) { t.align = alignment; //"center" } t.cellPadding = cellpadding; //1 t.cellSpacing = cellspacing; //2 if (!string.IsNullOrEmpty(widthpercentage)) { t.width = widthpercentage; //"50%"; } else if (widthpixel > 0) { t.width = widthpixel; //80; } if (!string.IsNullOrEmpty(backcolor)) { t.bgColor = backcolor; } if (!string.IsNullOrEmpty(bordercolor)) { t.borderColor = bordercolor; } if (!string.IsNullOrEmpty(lightbordercolor)) { t.borderColorLight = lightbordercolor; } if (!string.IsNullOrEmpty(darkbordercolor)) { t.borderColorDark = darkbordercolor; } //Insert rows and fill them with space int cells = colnum - 1; int rows = rownum - 1; CalculateCellWidths(colnum); for (int i = 0; i <= rows; i++) { IHTMLTableRow tr = (IHTMLTableRow)t.insertRow(-1); for (int j = 0; j <= cells; j++) { IHTMLElement c = tr.insertCell(-1) as IHTMLElement; if (c != null) { c.innerHTML = HtmlSpace; IHTMLTableCell tcell = c as IHTMLTableCell; if (tcell != null) { //set width so as user enters text //the cell width would not adjust if (j == cells) //last cell { tcell.width = m_lastcellwidth; } else { tcell.width = m_cellwidth; } } } } } //Append to body DOM collection IHTMLDOMNode nd = (IHTMLDOMNode)t; IHTMLDOMNode body = (IHTMLDOMNode)m_pDoc2.body; return(body.appendChild(nd) != null); }