Skip to content

Commit a0ef46e

Browse files
Merge pull request #458 from Karan-SF4772/main
ES-963148 : Add Sample for Add or Remove column in a table
2 parents 8e98a57 + 50d334c commit a0ef46e

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.13.35919.96 d17.13
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
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}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{CB9481DB-1AD2-4432-B868-5AABFF5E0092}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{CB9481DB-1AD2-4432-B868-5AABFF5E0092}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{CB9481DB-1AD2-4432-B868-5AABFF5E0092}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{CB9481DB-1AD2-4432-B868-5AABFF5E0092}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {F8A4CC44-1AC3-4919-AFA8-63AA0BBBAABA}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Add_or_remove_column_in_a_table</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Template.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\.gitkeep">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
24+
</Project>
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

Comments
 (0)