简体   繁体   中英

How to split a Word document by section using C# and the Open XML SDK?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM