如何使用C#和Open XML SDK按节拆分Word文档?

3

我希望能够使用C#和Open XML SDK编程地按节拆分Word文档。我们已经通过段落将Word文档拆分了,现在我们想对每个节执行相同的操作。请有相关知识的人告诉我如何解决这个问题。


2
你好,你能展示一下你用来按段落分割的代码吗? - Davide Piras
1
如果您已经知道如何按段落拆分,那么按部分拆分有什么问题呢? - Samuel Neff
1个回答

10

了解应用各个部分的方法有些古怪。与其将段落包含在各个部分中,这样我们可以轻松识别,各个部分实际上适用于它们之前找到的所有内容。

在段落的段落属性中查找SectionProperties元素,这些元素定义了节的分隔符。当您找到一个SectionProperties定义时,最后一个SectionProperties定义和此新定义之间的所有内容都被归为一节。 例如,考虑以下示例:

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

还要记住最后的SectionProperties不是在段落中,而是在Body标签内的根级别。很遗憾,据我所知SDK没有提供快捷方法来计算一个段落属于哪个章节。从这里开始,你应该能够得到一个快速计算章节的系统。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接