I want to split a Word document by section programatically using C# and the Open XML SDK. We already we split the Word document by paragraph. Now we want to do the same operation for each section. Please anybody having knowledge in this area kindly let me know to resolve this problem.
1 Answers
Knowing where the sections are applied is a little quirky. Rather than wrapping paragraphs within sections, which would make it easy for us to identify, sections instead apply to all the content found before them.
Look for SectionProperties elements within the ParagraphProperties of a paragraph, these are what define Section breaks. When you find a SectionProperties definition, all content between the last SectionProperties definition and this new definition is grouped together as a section. For example consider the following:
Paragraph1 // Section 1
Paragraph2 // Section 1
SectionProperties (Section 1) // Defines what section 1 is like
Paragraph3 // Section 2
Paragraph4 // Section 2
SectionProperties (Section 2) // Defines what section 2 is like
Paragraph5 // Section 3
Final SectionProperties // Defines what Section 3 is like.
// This final definition exists within the Body tag itself.
// Other SectionProperties exist under Paragraph Properties
Also remember that the last SectionProperties doesn't live within a paragraph, it sits at the root level inside the Body tag. Unfortunately as far as I know the SDK doesn't provide shortcuts for calculating which section a Paragraph belongs to. From here you should be able to get a quick system for calculating sections.