Skip to content

Commit a7db20e

Browse files
Added the sample Remove-empty-table-from-document
1 parent 410b6a3 commit a7db20e

File tree

5 files changed

+169
-0
lines changed

5 files changed

+169
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35527.113 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Remove-empty-table-from-document", "Remove-empty-table-from-document\Remove-empty-table-from-document.csproj", "{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}"
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+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
5+
// Load the Word document
6+
using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx")))
7+
{
8+
// Iterate through all sections in the document
9+
foreach (WSection section in document.Sections)
10+
{
11+
// Iterate through tables in reverse order to safely remove them if needed
12+
for (int i = section.Tables.Count - 1; i >= 0; i--)
13+
{
14+
WTable table = (WTable)section.Tables[i];
15+
16+
#region RemoveNestedTableFirst
17+
// Iterate through rows of the table
18+
foreach (WTableRow row in table.Rows)
19+
{
20+
// Iterate through cells of the row
21+
foreach (WTableCell cell in row.Cells)
22+
{
23+
// Iterate through child entities in reverse order for safe removal
24+
for (int j = cell.ChildEntities.Count - 1; j >= 0; j--)
25+
{
26+
Entity entity = cell.ChildEntities[j];
27+
28+
// Check if entity is a table and if it's completely empty
29+
if (entity.EntityType == EntityType.Table && IsTableCompletelyEmpty(entity as WTable))
30+
{
31+
cell.ChildEntities.Remove(entity); // Remove empty nested table
32+
}
33+
}
34+
}
35+
}
36+
#endregion
37+
38+
// If the entire table is empty, remove it from the section
39+
if (IsTableCompletelyEmpty(table))
40+
{
41+
section.Body.ChildEntities.Remove(table);
42+
}
43+
}
44+
}
45+
// Save the modified document
46+
document.Save(Path.GetFullPath(@"Output/Result.docx"), FormatType.Docx);
47+
}
48+
49+
/// <summary>
50+
/// Checks whether table is empty
51+
/// </summary>
52+
/// <param name="table"></param>
53+
/// <returns>True if table is empty, Otherwise False.</returns>
54+
static bool IsTableCompletelyEmpty(WTable table)
55+
{
56+
for (int i = 0; i < table.Rows.Count; i++)
57+
{
58+
WTableRow row = table.Rows[i];
59+
60+
for (int j = 0; j < row.Cells.Count; j++)
61+
{
62+
WTableCell cell = row.Cells[j];
63+
// If any cell contains content, the table is not empty
64+
if (!IsTextBodyEmpty(cell))
65+
return false;
66+
}
67+
}
68+
return true;
69+
}
70+
/// <summary>
71+
/// Checks whether text body is empty
72+
/// </summary>
73+
/// <param name="textBody"></param>
74+
/// <returns></returns>
75+
static bool IsTextBodyEmpty(WTextBody textBody)
76+
{
77+
for (int i = textBody.ChildEntities.Count - 1; i >= 0; i--)
78+
{
79+
Entity entity = textBody.ChildEntities[i];
80+
81+
switch (entity.EntityType)
82+
{
83+
case EntityType.Paragraph:
84+
if (!IsParagraphEmpty(entity as WParagraph))
85+
return false;
86+
break;
87+
case EntityType.BlockContentControl:
88+
if (!IsTextBodyEmpty((entity as BlockContentControl).TextBody))
89+
return false;
90+
break;
91+
}
92+
}
93+
return true;
94+
}
95+
/// <summary>
96+
/// Checks whether paragraph is empty
97+
/// </summary>
98+
/// <param name="textBody"></param>
99+
/// <returns></returns>
100+
static bool IsParagraphEmpty(WParagraph paragraph)
101+
{
102+
for (int i = 0; i < paragraph.ChildEntities.Count; i++)
103+
{
104+
Entity entity = paragraph.ChildEntities[i];
105+
switch (entity.EntityType)
106+
{
107+
case EntityType.TextRange:
108+
WTextRange textRange = entity as WTextRange;
109+
if (!string.IsNullOrEmpty(textRange.Text))
110+
return false;
111+
break;
112+
case EntityType.BookmarkStart:
113+
case EntityType.BookmarkEnd:
114+
case EntityType.EditableRangeStart:
115+
case EntityType.EditableRangeEnd:
116+
break;
117+
default:
118+
return false;
119+
}
120+
}
121+
return true;
122+
}
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>Remove_empty_table_from_document</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>

0 commit comments

Comments
 (0)