0
votes

I have searched a lot on the internet and I could not find an answer to this. Is there a way to check where does the table belong. That is, which section does the table belong to.

I would prefer to do it using OpenXml, but if there is a solution using Interop Word object. I would take that too...

As a sample document.

screenshot of the word document

I would have posted the xml for the document as well, but post's limit is reached. To clarify, the example document contains two sections. First one contains two tables and the second contains one table.

1
What do you mean by "which section" - what information do you want? The index value? The page orientation? Something else? - Cindy Meister
@CindyMeister I want to check where does the table belong to ? I mean, in which section? You can insert section breaks to create multiple chapters in a document which are also called sections. - Naveed Butt
Yes, I understand all that. But exatly what information do you want? - Cindy Meister
I want to see if the width of the table is greater than the page size or not, which could be different in each section/chapter, so I want to know where is this table currently placed in the document (i.e., in which section/chapter) - Naveed Butt

1 Answers

0
votes

Working with the Open XML SDK is very similar to using an XML parser to locate nodes relative to the current node. You're probably familiar with using Descendants as that's used in many examples. You can also use Ancestors and various Sibling criteria for locating a specific node.

In this case, if you inspect the underlying WordOpenXML, you'll see that <w:tbl> is a sibling of <w:p>, and <w:sectPr> is a child of <w:p>. So you want a NextSibling<Paragraph> that has a child of type SectionProperties.

This is the case for all sections except the last (default section for the document). The last sectPr is a child of <w:body> (sibling of <w:p>) and will be the last element before the closing </w:body> tag.