diff --git a/Tables/Remove-empty-table-from-document/.NET/Remove-empty-table-from-document.sln b/Tables/Remove-empty-table-from-document/.NET/Remove-empty-table-from-document.sln new file mode 100644 index 000000000..d775e1c36 --- /dev/null +++ b/Tables/Remove-empty-table-from-document/.NET/Remove-empty-table-from-document.sln @@ -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 diff --git a/Tables/Remove-empty-table-from-document/.NET/Remove-empty-table-from-document/Data/Template.docx b/Tables/Remove-empty-table-from-document/.NET/Remove-empty-table-from-document/Data/Template.docx new file mode 100644 index 000000000..ed6ec3b6c Binary files /dev/null and b/Tables/Remove-empty-table-from-document/.NET/Remove-empty-table-from-document/Data/Template.docx differ diff --git a/Tables/Remove-empty-table-from-document/.NET/Remove-empty-table-from-document/Output/.gitkeep b/Tables/Remove-empty-table-from-document/.NET/Remove-empty-table-from-document/Output/.gitkeep new file mode 100644 index 000000000..5f282702b --- /dev/null +++ b/Tables/Remove-empty-table-from-document/.NET/Remove-empty-table-from-document/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Tables/Remove-empty-table-from-document/.NET/Remove-empty-table-from-document/Program.cs b/Tables/Remove-empty-table-from-document/.NET/Remove-empty-table-from-document/Program.cs new file mode 100644 index 000000000..6af618746 --- /dev/null +++ b/Tables/Remove-empty-table-from-document/.NET/Remove-empty-table-from-document/Program.cs @@ -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); +} + +/// +/// Checks whether table is empty +/// +/// +/// True if table is empty, Otherwise False. +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; +} +/// +/// Checks whether text body is empty +/// +/// +/// +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; +} +/// +/// Checks whether paragraph is empty +/// +/// +/// +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; +} diff --git a/Tables/Remove-empty-table-from-document/.NET/Remove-empty-table-from-document/Remove-empty-table-from-document.csproj b/Tables/Remove-empty-table-from-document/.NET/Remove-empty-table-from-document/Remove-empty-table-from-document.csproj new file mode 100644 index 000000000..6eac660f4 --- /dev/null +++ b/Tables/Remove-empty-table-from-document/.NET/Remove-empty-table-from-document/Remove-empty-table-from-document.csproj @@ -0,0 +1,24 @@ + + + + Exe + net8.0 + Remove_empty_table_from_document + enable + enable + + + + + + + + + Always + + + Always + + + +