hi Team,
I got are very weird requirement from my business, they want to remove all tables excluding one with ZERO border.
can we do this? if yes can you share code snippet for same?
thanks in advance
Document doc = new Document();
doc.LoadFromFile("TableTest.docx");
foreach (Section s in doc.Sections)
{
//1.remove the table without border in the body
List<Table> NoBorderTbsinBody = new List<Table>();
TableCollection tbs = s.Tables;
foreach (Table tb in tbs)
{
tb.ApplyTableStyle();
if (tb.TableFormat.Borders.NoBorder)
{
NoBorderTbsinBody.Add(tb);
}
}
for (int i = 0; i < NoBorderTbsinBody.Count; i++)
{
s.Tables.Remove(NoBorderTbsinBody[i]);
}
//2. remove the table without border in the header part
List<Table> NoBorderTbsinHeader = new List<Table>();
foreach (DocumentObject hObj in s.HeadersFooters.Header.ChildObjects)
{
if (hObj is Table)
{
Table tb = hObj as Table;
tb.ApplyTableStyle();
if (tb.TableFormat.Borders.NoBorder)
{
NoBorderTbsinHeader.Add(tb);
}
}
}
for (int i = 0; i < NoBorderTbsinHeader.Count; i++)
{
s.HeadersFooters.Header.ChildObjects.Remove(NoBorderTbsinHeader[i]);
}
//3. remove the table without border in the footer part
List<Table> NoBorderTbsinFooter = new List<Table>();
foreach (DocumentObject fObj in s.HeadersFooters.Footer.ChildObjects)
{
if (fObj is Table)
{
Table tb = fObj as Table;
tb.ApplyTableStyle();
if (tb.TableFormat.Borders.NoBorder)
{
NoBorderTbsinFooter.Add(tb);
}
}
}
for (int i = 0; i < NoBorderTbsinFooter.Count; i++)
{
s.HeadersFooters.Footer.ChildObjects.Remove(NoBorderTbsinFooter[i]);
}
}
doc.SaveToFile("removeNoBorderTables.docx");