Skip to content
Merged
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,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35527.113 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5B0540C8-1A4D-4BB4-A0B9-10028D140E00}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
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,122 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;


// Load the Word document
using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx")))
{
// Iterate through all sections in the document
foreach (WSection section in document.Sections)
{
// Iterate through tables in reverse order to safely remove them if needed
for (int i = section.Tables.Count - 1; i >= 0; i--)
{
WTable table = (WTable)section.Tables[i];

#region RemoveNestedTableFirst
// Iterate through rows of the table
foreach (WTableRow row in table.Rows)
{
// Iterate through cells of the row
foreach (WTableCell cell in row.Cells)
{
// Iterate through child entities in reverse order for safe removal
for (int j = cell.ChildEntities.Count - 1; j >= 0; j--)
{
Entity entity = cell.ChildEntities[j];

// Check if entity is a table and if it's completely empty
if (entity.EntityType == EntityType.Table && IsTableCompletelyEmpty(entity as WTable))
{
cell.ChildEntities.Remove(entity); // Remove empty nested table
}
}
}
}
#endregion

// If the entire table is empty, remove it from the section
if (IsTableCompletelyEmpty(table))
{
section.Body.ChildEntities.Remove(table);
}
}
}
// Save the modified document
document.Save(Path.GetFullPath(@"Output/Result.docx"), FormatType.Docx);
}

/// <summary>
/// Checks whether table is empty
/// </summary>
/// <param name="table"></param>
/// <returns>True if table is empty, Otherwise False.</returns>
static bool IsTableCompletelyEmpty(WTable table)
{
for (int i = 0; i < table.Rows.Count; i++)
{
WTableRow row = table.Rows[i];

for (int j = 0; j < row.Cells.Count; j++)
{
WTableCell cell = row.Cells[j];
// If any cell contains content, the table is not empty
if (!IsTextBodyEmpty(cell))
return false;
}
}
return true;
}
/// <summary>
/// Checks whether text body is empty
/// </summary>
/// <param name="textBody"></param>
/// <returns></returns>
static bool IsTextBodyEmpty(WTextBody textBody)
{
for (int i = textBody.ChildEntities.Count - 1; i >= 0; i--)
{
Entity entity = textBody.ChildEntities[i];

switch (entity.EntityType)
{
case EntityType.Paragraph:
if (!IsParagraphEmpty(entity as WParagraph))
return false;
break;
case EntityType.BlockContentControl:
if (!IsTextBodyEmpty((entity as BlockContentControl).TextBody))
return false;
break;
}
}
return true;
}
/// <summary>
/// Checks whether paragraph is empty
/// </summary>
/// <param name="textBody"></param>
/// <returns></returns>
static bool IsParagraphEmpty(WParagraph paragraph)
{
for (int i = 0; i < paragraph.ChildEntities.Count; i++)
{
Entity entity = paragraph.ChildEntities[i];
switch (entity.EntityType)
{
case EntityType.TextRange:
WTextRange textRange = entity as WTextRange;
if (!string.IsNullOrEmpty(textRange.Text))
return false;
break;
case EntityType.BookmarkStart:
case EntityType.BookmarkEnd:
case EntityType.EditableRangeStart:
case EntityType.EditableRangeEnd:
break;
default:
return false;
}
}
return true;
}
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>Remove_empty_table_from_document</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>
Loading