【问题标题】:How to split a Word document by section using C# and the Open XML SDK?如何使用 C# 和 Open XML SDK 按部分拆分 Word 文档?
【发布时间】:2012-04-18 21:47:20
【问题描述】:

我想使用 C# 和 Open XML SDK 以编程方式按部分拆分 Word 文档。我们已经按段落拆分了 Word 文档。现在我们要对每个部分执行相同的操作。请任何有这方面知识的人告诉我以解决这个问题。

【问题讨论】:

  • 你好,你可以显示你用来分段的代码吗?
  • 如果你已经知道怎么分段了,那分段分段会不会很麻烦呢?

标签: c# openxml openxml-sdk


【解决方案1】:

知道这些部分的应用位置有点古怪。与其将段落包裹在各节中,这样我们更容易识别,而是将各节应用于在它们之前找到的所有内容。

在段落的 ParagraphProperties 中查找 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 没有提供计算段落所属部分的快捷方式。从这里您应该能够获得一个快速计算截面的系统。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多