Skip to content

ES-963148 : Add Sample for Add or Remove column in a table #458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Add_or_remove_column_in_a_table</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Data\Template.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -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);
}
}
/// <summary>
/// Adds a new column at the specified index in the table.
/// </summary>
/// <param name="table">The table to modify.</param>
/// <param name="indexToAdd">The index at which to insert the new column.</param>
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);
}
}
}
/// <summary>
/// Removes a column at the specified index from the table.
/// </summary>
/// <param name="table">The table to modify.</param>
/// <param name="indexToRemove">The index of the column to remove.</param>
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);
}
}
}
}
}