Skip to content

Commit 3f50666

Browse files
Added sample to split a table by columns in a Word document
1 parent 9f2d2d7 commit 3f50666

File tree

5 files changed

+134
-0
lines changed

5 files changed

+134
-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 16
4+
VisualStudioVersion = 16.0.31911.196
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Split-table-by-columns", "Split-table-by-columns\Split-table-by-columns.csproj", "{C17B90BC-F559-456B-B189-90B53FF6CDD4}"
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+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.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 = {EF357FC6-E9E5-4E3C-B932-43F727BE1DE4}
24+
EndGlobalSection
25+
EndGlobal
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System;
4+
using System.IO;
5+
6+
namespace Split_table_by_columns
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
13+
{
14+
//Open an existing Word document.
15+
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
16+
{
17+
//Access the first table in the Word document.
18+
WTable table = document.Sections[0].Tables[0] as WTable;
19+
//Get the index of the table within the document's text body.
20+
int index = table.OwnerTextBody.ChildEntities.IndexOf(table);
21+
//Determine the column index at which the table will be split.
22+
int columnIndex = (int)Math.Floor((double)table.Rows[0].Cells.Count / 2);
23+
//Split the table into two parts based on the calculated column index.
24+
WTable secondTable = SplitTableByColumns(table, columnIndex);
25+
26+
//Insert a heading above the first part of the table.
27+
WParagraph titleParaOne = new WParagraph(document);
28+
titleParaOne.AppendText("Part 1 of 2").CharacterFormat.Bold = true;
29+
table.OwnerTextBody.ChildEntities.Insert(index, titleParaOne);
30+
//Insert a heading above the second part of the table.
31+
WParagraph titleParaTwo = new WParagraph(document);
32+
titleParaTwo.AppendText("Part 2 of 2").CharacterFormat.Bold = true;
33+
table.OwnerTextBody.ChildEntities.Insert(index + 2, titleParaTwo);
34+
35+
//Insert the second part of the table into the Word document.
36+
table.OwnerTextBody.ChildEntities.Insert(index + 3, secondTable);
37+
//Create a file stream to save the modified Word document.
38+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
39+
{
40+
//Save the modified Word document to the file stream.
41+
document.Save(outputFileStream, FormatType.Docx);
42+
}
43+
}
44+
}
45+
}
46+
/// <summary>
47+
/// Split a table into two parts based on the specified column index.
48+
/// </summary>
49+
/// <param name="originalTable">The table to split.</param>
50+
/// <param name="columnsToSplit">The column index where the split occurs.</param>
51+
/// <returns>The table with columns after the split point.</returns>
52+
public static WTable SplitTableByColumns(WTable originalTable, int columnsToSplit)
53+
{
54+
//Clone the original table to create the second part of the table.
55+
WTable clonedTable = originalTable.Clone();
56+
//Remove cells from the cloned table to keep only the columns after the split point.
57+
foreach (WTableRow row in clonedTable.Rows)
58+
{
59+
//Check if the table has enough columns before attempting to remove.
60+
if (columnsToSplit >= 0 && columnsToSplit < row.Cells.Count)
61+
{
62+
//Remove cells from the first part of the table (columns before the split point).
63+
for (int i = columnsToSplit; i >= 0; i--)
64+
{
65+
row.Cells.RemoveAt(i);
66+
}
67+
}
68+
}
69+
//Remove cells from the original table to keep only the columns before the split point.
70+
foreach (WTableRow row in originalTable.Rows)
71+
{
72+
//Check if the table has enough columns before attempting to remove.
73+
if (columnsToSplit < row.Cells.Count)
74+
{
75+
//Remove cells from the second part of the table (columns after the split point).
76+
for (int i = row.Cells.Count - 1; i > columnsToSplit; i--)
77+
{
78+
row.Cells.RemoveAt(i);
79+
}
80+
}
81+
}
82+
//Return the second part of the table.
83+
return clonedTable;
84+
}
85+
}
86+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Split_table_by_columns</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="Data\Template.docx">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
<None Update="Output\.gitkeep">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
</ItemGroup>
21+
22+
</Project>

0 commit comments

Comments
 (0)