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.

Fri Mar 24, 2017 3:43 pm

We have a client that is unable to process Word tables correctly. To assist, we added code that is able to convert tables to text. However, when doing the conversion, we need to place the new text block exactly where the old table had been.

For example:
If the document had 3 lines like so:
Line 1
[Table consisting of 2 rows, 2 columns]
Line 3

We need to convert the Table between line 1 and line 3 so it would result in something like this:
Line 1
Line 2.1
Line 2.2
Line 3

The code we use to append the text looks like this:
// textBlock is text rendered from table
// index is where the original table was located
sec.Paragraphs[index].AppendText(textBlock);

We have been unable to find the correct index of the paragraph owner of the table. If we know the index of the owner, we could convert the table to text, insert the text at the correct location, end then delete the table.

Any help would be most appreciated.

Thank you.

rutzelt
 
Posts: 43
Joined: Mon Sep 22, 2014 4:59 pm

Mon Mar 27, 2017 7:13 am

Dear rutzelt,

Thanks for your inquiry and sorry for late reply as weekend.
Please note that the paragrah is not the owner of table in the case you mentioned, they are same level. So you only need to find the index of table in document. Here is sample code for your reference.
Code: Select all
            Table table = doc.Sections[0].Tables[0] as Table;
            int index =  doc.Sections[0].Body.ChildObjects.IndexOf(table);

If there is any question, please let me know.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Mon Mar 27, 2017 2:15 pm

Hi Betsy. The problem is finding the index of the table relative to the surrounding paragraphs. When we turn the table into text, we want to do it in the location where the table appears to be on the document.

For example:
Paragraph 1
Paragraph 2
Table 1
Paragraph 3
Table 2

In this scenario, we want the contents of Table 1 to appear between Paragraph 2 and 3. As you mention, table 1 would be Index 0. However, we need to convert it in such a way that it appears after Paragraph 2.

Is there a way to find out the location of a table relative to the Paragraphs that might surround it?

Thank you,
Tom

rutzelt
 
Posts: 43
Joined: Mon Sep 22, 2014 4:59 pm

Mon Mar 27, 2017 2:22 pm

Hi again. I figured it out with your assistance.

Thank you for your help.
-Tom

rutzelt
 
Posts: 43
Joined: Mon Sep 22, 2014 4:59 pm

Tue Mar 28, 2017 1:31 am

Dear Tom,

Thanks for your feedback.
Please feel free to contact us if you have any question. We will be happy to help you :) .

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Thu Feb 17, 2022 4:17 pm

Hi,

How the index of a table relative to the surrounding paragraphs can be found? Is there any way to distinguish between a paragraph and Table while parsing the word file.

For example:
Heading1
Text Paragraph 1
Text Paragraph 2
Table 1
Text Paragraph 3
Heading2
Table 2

nirajgiet36
 
Posts: 1
Joined: Thu Feb 17, 2022 4:09 pm

Fri Feb 18, 2022 6:38 am

Hello nirajgiet36,

Thank you for your inquiry.
According to your description, the paragraphs and tables in the Word file are on the same level, and the paragraph is not the owner of the table. Please refer to the code below to get the index of the heading, the table and the paragraph in the Word file.
Code: Select all
 Document doc = new Document();
 doc.LoadFromFile("test.docx");
 Section section = doc.Sections[0];

 for (int i = 0; i < section.Tables.Count;i++ )
 {
     //Get the index of the table
     int tableIndex = section.Body.ChildObjects.IndexOf(section.Tables[i]);
     Console.WriteLine("Table"+(i+1)+ ":index of document is " + tableIndex );
 }
 int h = 1;
 foreach (IParagraph paragraph in section.Paragraphs)
 {
     //Get the index of the paragraph
     int paIndex = section.Body.ChildObjects.IndexOf(paragraph);
     // Determine if the paragraph is a heading
     if (paragraph.StyleName.Contains("Heading"))
     {
         Console.WriteLine("Heading" +h +": index of document is "+paIndex);
         h++;
     }
     else
     {
         Console.WriteLine("Text Paragraph index of document is " + paIndex );
     }     
 }
 Console.ReadLine();
Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Doc