How to remove empty tables from Word document?
Syncfusion® Essential® DocIO is a .NET Word library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can remove empty tables from a Word document using C#.
To achieve this, iterates through each table in the Word document and checks if it’s empty by examining its contents. If a table has no content, it is removed. This also works for tables nested inside other tables.
Steps to remove empty tables from a Word document:
- Create a new .NET Core console application project.
- Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your project from NuGet.org.
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion® license key in your application to use the components without a trial message.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIO;
- Use the following code example to remove empty tables from a Word document.
C#
// 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);
}
- Use the following code example to check whether a table is empty.
C#
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;
}
- Use the following code example to check whether the text body is empty.
C#
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;
}
- Use the following code example to check whether a paragraph is empty.
C#
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;
}
You can download a complete working sample to remove empty tables from a Word document from GitHub.
Take a moment to peruse the documentation where you can find basic Word document processing options along with features like mail merge, merge, split, and compare Word documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.
Conclusion
I hope you enjoyed learning about how to remove empty tables from a Word document in a .NET Core Word document.
You can refer to our ASP.NET Core DocIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our ASP.NET Core DocIO example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!