|
| 1 | +using Syncfusion.DocIO.DLS; |
| 2 | +using Syncfusion.DocIO; |
| 3 | + |
| 4 | +namespace Add_or_remove_column_in_a_table |
| 5 | +{ |
| 6 | + class Program |
| 7 | + { |
| 8 | + static void Main(string[] args) |
| 9 | + { |
| 10 | + // Load the Word document |
| 11 | + using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx"))) |
| 12 | + { |
| 13 | + // Access the first table in the document |
| 14 | + WTable table = (WTable)document.Sections[0].Tables[0]; |
| 15 | + // Add a new column at index |
| 16 | + InsertColumn(table, 1); |
| 17 | + // Add a column at the last index |
| 18 | + AddColumn(table); |
| 19 | + // Remove a column at the index |
| 20 | + RemoveColumn(table, 3); |
| 21 | + // Save the modified document to a new file |
| 22 | + document.Save(Path.GetFullPath(@"../../../Output/Result.docx"), FormatType.Docx); |
| 23 | + } |
| 24 | + } |
| 25 | + /// <summary> |
| 26 | + /// Adds a new column at the last index in the table |
| 27 | + /// </summary> |
| 28 | + /// <param name="table">The table to modify</param> |
| 29 | + private static void AddColumn(WTable table) |
| 30 | + { |
| 31 | + // Loop through each row in the table |
| 32 | + for (int i = 0; i < table.Rows.Count; i++) |
| 33 | + { |
| 34 | + // Add a new cell to the current row (appends at the end) |
| 35 | + WTableCell cell = table.Rows[i].AddCell(); |
| 36 | + // Set the width of the new cell to match the first cell in the row |
| 37 | + cell.Width = table.Rows[i].Cells[0].Width; |
| 38 | + // Add a paragraph to the new cell and insert the text |
| 39 | + cell.AddParagraph().AppendText("Using Add API"); |
| 40 | + } |
| 41 | + } |
| 42 | + /// <summary> |
| 43 | + /// Adds a new column at the specified index in the table. |
| 44 | + /// </summary> |
| 45 | + /// <param name="table">The table to modify.</param> |
| 46 | + /// <param name="indexToAdd">The index at which to insert the new column.</param> |
| 47 | + private static void InsertColumn(WTable table, int indexToAdd) |
| 48 | + { |
| 49 | + // Loop through each row in the table |
| 50 | + for (int i = 0; i < table.Rows.Count; i++) |
| 51 | + { |
| 52 | + // Check if the index is within the valid range for the current row |
| 53 | + if (indexToAdd >= 0 && indexToAdd <= table.Rows[i].Cells.Count) |
| 54 | + { |
| 55 | + // Create a new cell. |
| 56 | + WTableCell newCell = new WTableCell(table.Document); |
| 57 | + // Insert the new cell at the specified index in the current row |
| 58 | + table.Rows[i].Cells.Insert(indexToAdd, newCell); |
| 59 | + // Add a paragraph to the new cell and insert the text |
| 60 | + newCell.AddParagraph().AppendText("Using Insert API"); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + /// <summary> |
| 65 | + /// Removes a column at the specified index from the table. |
| 66 | + /// </summary> |
| 67 | + /// <param name="table">The table to modify.</param> |
| 68 | + /// <param name="indexToRemove">The index of the column to remove.</param> |
| 69 | + private static void RemoveColumn(WTable table, int indexToRemove) |
| 70 | + { |
| 71 | + // Loop through each row in the table |
| 72 | + for (int i = 0; i < table.Rows.Count; i++) |
| 73 | + { |
| 74 | + // Check if the index is within the valid range for the current row |
| 75 | + if (indexToRemove >= 0 && indexToRemove < table.Rows[i].Cells.Count) |
| 76 | + { |
| 77 | + // Remove the cell at the specified index in the current row |
| 78 | + table.Rows[i].Cells.RemoveAt(indexToRemove); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments