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.

Wed Oct 04, 2017 9:49 am

I am interested in adding text wherever table is removed. Is this possible can you share code snippet for this.

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

Wed Oct 04, 2017 10:18 am

Hello,

Thanks for your inquiry. Here are the codes for your reference.
Code: Select all
 
Document doc = new Document();
doc.LoadFromFile("test.docx");
Section sec = doc.Sections[0];
Table table= sec.Tables[0] as Table;
int tableIndex = sec.Body.ChildObjects.IndexOf(table);
sec.Tables.Remove(table);
Paragraph para = new Paragraph(doc);
para.Text = "test";
sec.Body.ChildObjects.Insert(tableIndex, para);
doc.SaveToFile("result.docx",FileFormat.Docx);

Please contact us if you have any questions.
Sincerely,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Mon Oct 09, 2017 10:05 am

Hello,

Greetings from E-iceblue.
Did we resolve your issue?
Thanks in advance for your valuable feedback and time.

Sincerely,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Wed Nov 22, 2017 3:41 pm

Yes, issue is resolved. Is it possible to add Text on top of table and bottom of table when we convert this to TEXT?

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

Thu Nov 23, 2017 2:14 am

Hello,

Thanks for your feedback. Please refer to the below code snippet to accomplish your task.
Code: Select all
            Document doc = new Document();
            doc.LoadFromFile(fileName);
            Section sec = doc.Sections[0];
            Table table = sec.Tables[0] as Table;
            int tableIndex = sec.Body.ChildObjects.IndexOf(table);
            sec.Tables.Remove(table);

            Paragraph bottomPara = new Paragraph(doc);
            bottomPara.Text = "bottom text";
            sec.Body.ChildObjects.Insert(tableIndex, bottomPara);

            Paragraph para = new Paragraph(doc);
            para.Text = "table text";
            sec.Body.ChildObjects.Insert(tableIndex, para);

            Paragraph topPara = new Paragraph(doc);
            topPara.Text = "top text";
            sec.Body.ChildObjects.Insert(tableIndex, topPara);

            doc.SaveToFile("result.txt", Spire.Doc.FileFormat.Txt);


Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Thu Nov 23, 2017 1:41 pm

we do not want to remove table ,this code removing table
sec.Tables.Remove(table);


here again required to get that table text.
Paragraph para = new Paragraph(doc);
para.Text = "table text";
sec.Body.ChildObjects.Insert(tableIndex, para);

we would like to add text at the starting and end of table.
Could it be possible without removing table.

ParasXOR
 
Posts: 28
Joined: Wed Nov 22, 2017 6:13 am

Fri Nov 24, 2017 2:25 am

Hello,

Thanks for your feedback. Here is the code to add text before and after the table. Attached is the result screenshot. If this is not what you want, please share us your expected result. Then we will provide you with the demo accordingly.
Code: Select all
            Document doc = new Document();
            doc.LoadFromFile(fileName);
            Section sec = doc.Sections[0];
            Table table = sec.Tables[0] as Table;
            int tableIndex = sec.Body.ChildObjects.IndexOf(table);
            Paragraph bottomPara = new Paragraph(doc);
            bottomPara.Text = "bottom text";
            sec.Body.ChildObjects.Insert(tableIndex+1, bottomPara);

            Paragraph topPara = new Paragraph(doc);
            topPara.Text = "top text";
            sec.Body.ChildObjects.Insert(tableIndex, topPara);

            doc.SaveToFile("result.docx", Spire.Doc.FileFormat.Docx);
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Fri Nov 24, 2017 10:44 am

thanks for your help

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

Mon Nov 27, 2017 1:37 am

Hello,

Thanks for your reply. If there is any other question, please feel free to contact us.


Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Mon Dec 18, 2017 7:51 am

Please find attached doc file, i have observed others table getting removed except one table.While converting doc to text file.

Please find Appendix 1 table.
it is not getting removed.

ParasXOR
 
Posts: 28
Joined: Wed Nov 22, 2017 6:13 am

Mon Dec 18, 2017 9:48 am

Hello,

Thanks for your feedback. I find that the Appendix 1 table is in a text box. Please refer to the below code snippet to remove it.

Code: Select all
                    Document docn = new Document();
                    docn.LoadFromFile(@"gn112016e.docx", FileFormat.Docx);

                    foreach (Section section in docn.Sections)
                    {               
                        //remove table in textbox
                        foreach (Paragraph p in section.Body.Paragraphs)
                        {
                            foreach (DocumentObject obj in p.ChildObjects)
                            {
                                if (obj is TextBox)
                                {
                                     var texbox = obj as TextBox;
                                     foreach (Table table in texbox.Body.Tables)
                                     {
                                         texbox.Body.Tables.Remove(table);
                                     }                           
                                }
                            }
                        }

                        //remove table in body
                        foreach (Table table in section.Tables)
                        {
                            section.Tables.Remove(table);
                        }
                    }
                    docn.SaveToFile("result.docx", FileFormat.Docx);


Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Tue Dec 19, 2017 7:38 am

Hello,

Greeting from E-iceblue.
Did you try the solution I provided?
Your feedback will be greatly appreciated.

Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Tue Dec 19, 2017 7:51 am

I have tried and it works for me.

Thanks

ParasXOR
 
Posts: 28
Joined: Wed Nov 22, 2017 6:13 am

Tue Dec 19, 2017 8:16 am

Hello,

Thanks for your feedback. If you encounter any other question, please feel free to contact us.

Best regards,
Simon
E-iceblue support team
User avatar

Simon.yang
 
Posts: 620
Joined: Wed Jan 11, 2017 2:03 am

Return to Spire.Doc