diff --git a/Tables/Add-or-remove-column-in-a-table/.NET/Add-or-remove-column-in-a-table.sln b/Tables/Add-or-remove-column-in-a-table/.NET/Add-or-remove-column-in-a-table.sln new file mode 100644 index 000000000..9a32762fc --- /dev/null +++ b/Tables/Add-or-remove-column-in-a-table/.NET/Add-or-remove-column-in-a-table.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.13.35919.96 d17.13 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Add-or-remove-column-in-a-table", "Add-or-remove-column-in-a-table\Add-or-remove-column-in-a-table.csproj", "{CB9481DB-1AD2-4432-B868-5AABFF5E0092}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {CB9481DB-1AD2-4432-B868-5AABFF5E0092}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CB9481DB-1AD2-4432-B868-5AABFF5E0092}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CB9481DB-1AD2-4432-B868-5AABFF5E0092}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CB9481DB-1AD2-4432-B868-5AABFF5E0092}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F8A4CC44-1AC3-4919-AFA8-63AA0BBBAABA} + EndGlobalSection +EndGlobal diff --git a/Tables/Add-or-remove-column-in-a-table/.NET/Add-or-remove-column-in-a-table/Add-or-remove-column-in-a-table.csproj b/Tables/Add-or-remove-column-in-a-table/.NET/Add-or-remove-column-in-a-table/Add-or-remove-column-in-a-table.csproj new file mode 100644 index 000000000..333864edb --- /dev/null +++ b/Tables/Add-or-remove-column-in-a-table/.NET/Add-or-remove-column-in-a-table/Add-or-remove-column-in-a-table.csproj @@ -0,0 +1,24 @@ + + + + Exe + net8.0 + Add_or_remove_column_in_a_table + enable + enable + + + + + + + + + Always + + + Always + + + + diff --git a/Tables/Add-or-remove-column-in-a-table/.NET/Add-or-remove-column-in-a-table/Data/Template.docx b/Tables/Add-or-remove-column-in-a-table/.NET/Add-or-remove-column-in-a-table/Data/Template.docx new file mode 100644 index 000000000..ae467b98f Binary files /dev/null and b/Tables/Add-or-remove-column-in-a-table/.NET/Add-or-remove-column-in-a-table/Data/Template.docx differ diff --git a/Tables/Add-or-remove-column-in-a-table/.NET/Add-or-remove-column-in-a-table/Output/.gitkeep b/Tables/Add-or-remove-column-in-a-table/.NET/Add-or-remove-column-in-a-table/Output/.gitkeep new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/Tables/Add-or-remove-column-in-a-table/.NET/Add-or-remove-column-in-a-table/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Tables/Add-or-remove-column-in-a-table/.NET/Add-or-remove-column-in-a-table/Program.cs b/Tables/Add-or-remove-column-in-a-table/.NET/Add-or-remove-column-in-a-table/Program.cs new file mode 100644 index 000000000..7f6297275 --- /dev/null +++ b/Tables/Add-or-remove-column-in-a-table/.NET/Add-or-remove-column-in-a-table/Program.cs @@ -0,0 +1,62 @@ +using Syncfusion.DocIO.DLS; +using Syncfusion.DocIO; + +namespace Add_or_remove_column_in_a_table +{ + class Program + { + static void Main(string[] args) + { + // Load the Word document + using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx"))) + { + // Access the first table in the document + WTable table = (WTable)document.Sections[0].Tables[0]; + // Add a new column at index + AddColumn(table, 2); + // Remove a column at the index + RemoveColumn(table, 1); + // Save the modified document to a new file + document.Save(Path.GetFullPath(@"Output/Result.docx"), FormatType.Docx); + } + } + /// + /// Adds a new column at the specified index in the table. + /// + /// The table to modify. + /// The index at which to insert the new column. + private static void AddColumn(WTable table, int indexToAdd) + { + // Loop through each row in the table + for (int i = 0; i < table.Rows.Count; i++) + { + // Check if the index is within the valid range for the current row + if (indexToAdd >= 0 && indexToAdd <= table.Rows[i].Cells.Count) + { + // Create a new cell. + WTableCell newCell = new WTableCell(table.Document); + // Insert the new cell at the specified index in the current row + table.Rows[i].Cells.Insert(indexToAdd, newCell); + } + } + } + /// + /// Removes a column at the specified index from the table. + /// + /// The table to modify. + /// The index of the column to remove. + private static void RemoveColumn(WTable table, int indexToRemove) + { + // Loop through each row in the table + for (int i = 0; i < table.Rows.Count; i++) + { + // Check if the index is within the valid range for the current row + if (indexToRemove >= 0 && indexToRemove < table.Rows[i].Cells.Count) + { + // Remove the cell at the specified index in the current row + table.Rows[i].Cells.RemoveAt(indexToRemove); + } + } + } + } +}