【问题标题】:DOCX XML Does Not Represent Line Breaks Like Word Does?DOCX XML 不像 Word 那样表示换行符?
【发布时间】:2023-03-24 22:16:01
【问题描述】:

我正在使用 Open XML SDK 2.5 在我的控制台应用程序中读取 .docx 文件。

Word 显示文档的方式与使用 Open XML SDK 打开时文档在 XML 中的表示方式之间似乎存在一些差异。

这是我在 Word 中看到的示例,其中空格可见:



所以在我的应用程序中,我将此段落作为DocumentFormat.OpenXml.Wordprocessing.Paragraph 对象引用。浏览 Open XML 文档后,我清楚地知道在 XML 格式中没有“线”的表示。所以我能做的最好的就是拥有我的Paragraph,最接近一条线的是Run 对象。在此示例中,Paragraph 节点具有 6 个 Run 对象的集合。如果我在此示例中获得ParagraphInnerXml 属性,它的外观如下:

<w:pPr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:pStyle w:val=\"PlainText\" /><w:numPr><w:ilvl w:val=\"0\" /><w:numId w:val=\"17\" /></w:numPr><w:rPr><w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" /><w:b /></w:rPr></w:pPr><w:r w:rsidRPr=\"000558F8\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:rPr><w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" /></w:rPr><w:t>Should we use the term “Verify” instead of “Confirm”</w:t></w:r><w:r w:rsidRPr=\"000558F8\" w:rsidR=\"00F5335C\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:rPr><w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" /></w:rPr><w:t xml:space=\"preserve\"> as per work instruction</w:t></w:r><w:r w:rsidRPr=\"000558F8\" w:rsidR=\"00411638\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:rPr><w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" /></w:rPr><w:t>?</w:t></w:r><w:r w:rsidR=\"000558F8\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:rPr><w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" /></w:rPr><w:br /><w:t>Med</w:t></w:r><w:r w:rsidRPr=\"000558F8\" w:rsidR=\"003E76BD\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:rPr><w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" /><w:b /></w:rPr><w:br /><w:t xml:space=\"preserve\">JD: </w:t></w:r><w:r w:rsidRPr=\"000558F8\" w:rsidR=\"00A118AB\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:rPr><w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" /><w:b /></w:rPr><w:t>Done.</w:t></w:r>

我看到的只是段落属性节点和 6 个运行节点。如您所见,运行节点不等于线。从 Word 中查看我的示例,我看到该段落有 2 个回车符,我希望这由 3 个“行”表示。然而,在 XML 中,我得到了 6 次运行,这似乎与 3 行非常接近,但由于某种原因,有些行似乎是任意拆分的。

真正的问题是,我看不到任何解释运行节点的方式,我可以重建 Word 示例中的行结构。例如,没有任何迹象表明运行 1、2 和 3 一起构成了第 1 行。

我需要解析超过 300 个单词的文档,这些文档依赖于换行符进行格式化。我需要换行符,我怎样才能得到它们?这可以通过 Open XML SDK 实现吗?

提前致谢。

【问题讨论】:

    标签: xml ms-word openxml docx openxml-sdk


    【解决方案1】:

    您在 XML 中查找的元素是 Break 元素,即 &lt;w:br /&gt;

    根据文档,这个 XML:

    <w:r>
        <w:t>This is</w:t>
        <w:br/>
        <w:t xml:space="preserve"> a simple sentence.</w:t>
    </w:r>
    

    会产生

    这是
    一个简单的句子。

    我已经美化了您的 XML 并在此答案的末尾标记了 Breaks

    Runs 不用于确定行,而是用于包含具有相同属性的文本的逻辑块。例如,假设我有以下文本:

    测试ing

    请注意,ing 以粗体显示。在 OpenXML 中,这需要两次运行,一次用于 test,另一次用于 ing,因为它们具有不同的属性。 XML 应该是这样的:

    <w:r>
        <w:t>Test</w:t>
    </w:r>
    <w:r w:rsidRPr="004750BC">
        <w:rPr>
           <w:b />
        </w:rPr>
        <w:t>ing</w:t>
    </w:r>
    

    &lt;w:rPr&gt; 是运行属性,&lt;w:b /&gt; 表示粗体。

    突出显示中断的 XML:

    <w:pPr
        xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
        <w:pStyle w:val="PlainText" />
        <w:numPr>
            <w:ilvl w:val="0" />
            <w:numId w:val="17" />
        </w:numPr>
        <w:rPr>
            <w:rFonts w:ascii="Arial" w:hAnsi="Arial" />
            <w:b />
        </w:rPr>
    </w:pPr>
    <w:r w:rsidRPr="000558F8"
        xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
        <w:rPr>
            <w:rFonts w:ascii="Arial" w:hAnsi="Arial" />
        </w:rPr>
        <w:t>Should we use the term “Verify” instead of “Confirm”</w:t>
    </w:r>
    <w:r w:rsidRPr="000558F8" w:rsidR="00F5335C"
        xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
        <w:rPr>
            <w:rFonts w:ascii="Arial" w:hAnsi="Arial" />
        </w:rPr>
        <w:t xml:space="preserve"> as per work instruction</w:t>
    </w:r>
    <w:r w:rsidRPr="000558F8" w:rsidR="00411638"
        xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
        <w:rPr>
            <w:rFonts w:ascii="Arial" w:hAnsi="Arial" />
        </w:rPr>
        <w:t>?</w:t>
    </w:r>
    <w:r w:rsidR="000558F8"
        xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
        <w:rPr>
            <w:rFonts w:ascii="Arial" w:hAnsi="Arial" />
        </w:rPr>
        <w:br /> <!-- break here -->
        <w:t>Med</w:t>
    </w:r>
    <w:r w:rsidRPr="000558F8" w:rsidR="003E76BD"
        xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
        <w:rPr>
            <w:rFonts w:ascii="Arial" w:hAnsi="Arial" />
            <w:b />
        </w:rPr>
        <w:br />  <!-- break here -->
        <w:t xml:space="preserve">JD: </w:t>
    </w:r>
    <w:r w:rsidRPr="000558F8" w:rsidR="00A118AB"
        xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
        <w:rPr>
            <w:rFonts w:ascii="Arial" w:hAnsi="Arial" />
            <w:b />
        </w:rPr>
        <w:t>Done.</w:t>
    </w:r>
    

    【讨论】:

    • 完美!这正是我所需要的。对于任何尝试做同样事情的人,在我的实现中,我只是将Paragraph 的所有Run 子级及其InnerText 属性附加到单个字符串中,当Run 对象时添加一个换行符包含Break 类型的子对象。
    猜你喜欢
    • 2021-10-05
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    • 2017-09-23
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    相关资源
    最近更新 更多