Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Nov 21, 2017 2:45 pm

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

yogeshmsharma
 
Posts: 17
Joined: Mon Sep 25, 2017 5:25 pm

Wed Nov 22, 2017 6:06 am

Hi,

Thanks for your inquiry.
Theoretically, this can be achieved. However, an error occurred when making the judgment on whether a table has any borders. I have delivered this issue to our dev team for a further investigation and fixing. Once there's any update, I will provide the code snippet accordingly.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Wed Nov 22, 2017 3:39 pm

thanks Jane,
will wait for your post with solution

yogeshmsharma
 
Posts: 17
Joined: Mon Sep 25, 2017 5:25 pm

Thu Nov 30, 2017 6:04 am

Hello,

Glad to inform that the issue has been resolved.
Please download the latest hotfix Spire.Doc Pack(hot fix) Version:6.0.63 and use the following code to remove the tables without any border in your document.
Code: Select all
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");

If there's still any issue, please come back to us and share your sample document.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Return to Spire.Doc