c# - 如何通过页码访问OpenXML内容?

标签 c# xml openxml docx openxml-sdk

使用OpenXML,我可以按页码读取文档内容吗?

wordDocument.MainDocumentPart.Document.Body 给出完整文档的内容。

  public void OpenWordprocessingDocumentReadonly()
        {
            string filepath = @"C:\...\test.docx";
            // Open a WordprocessingDocument based on a filepath.
            using (WordprocessingDocument wordDocument =
                WordprocessingDocument.Open(filepath, false))
            {
                // Assign a reference to the existing document body.  
                Body body = wordDocument.MainDocumentPart.Document.Body;
                int pageCount = 0;
                if (wordDocument.ExtendedFilePropertiesPart.Properties.Pages.Text != null)
                {
                    pageCount = Convert.ToInt32(wordDocument.ExtendedFilePropertiesPart.Properties.Pages.Text);
                }
                for (int i = 1; i <= pageCount; i++)
                {
                    //Read the content by page number
                }
            }
        }

MSDN Reference

<小时/>

更新1:

看起来分页符设置如下

<w:p w:rsidR="003328B0" w:rsidRDefault="003328B0">
        <w:r>
            <w:br w:type="page" />
        </w:r>
    </w:p>

所以现在我需要使用上述检查来拆分 XML,并为每个 XML 获取 InnerTex,这将为我提供页面可视化文本。

现在的问题是如何通过上述检查拆分 XML?

<小时/>

更新2:

仅当有分页符时才设置分页符,但如果文本从一页 float 到其他页面,则不会设置分页符 XML 元素,因此它会返回到如何识别页面分隔符的相同挑战.

最佳答案

您无法仅在 OOXML 数据级别通过页码引用 OOXML 内容

  • 硬分页不是问题;可以计算硬分页符。
  • 软分页符就是问题所在。这些是根据计算得出的 换行和分页算法的实现 依赖;它不是 OOXML 数据固有的。没有什么 来数数。

w:lastRenderedPageBreak 怎么样,它是文档上次呈现时软分页符位置的记录? 不,w:lastRenderedPageBreak 一般来说也没有帮助,因为:

如果您愿意接受对 Word Automation 及其所有固有功能的依赖 licensing and server operation limitations ,然后您就有机会确定页面边界、页码、页数等。

否则,唯一真正的答案是超越基于页面的引用框架,该框架依赖于专有的、特定于实现的分页算法。

关于c# - 如何通过页码访问OpenXML内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47228220/

相关文章:

javascript - 如何使用 jQuery 转义或替换 xml 文件中的 "&"?

c# - 检查docx是否损坏

c# - 以编程方式查找消息框并生成按钮点击

c# - 返回 Dictionary<string, string> 的 LINQ 查询

c# - ASP.NET MVC2 访问控制 : How to do authorization dynamically?

c# - Entity Framework 6 : related entities auto added to parent entity despite lazy loading being turned off

xml - 使用 xml::simple - 无法获得特定格式的输出

java - 通过 Spring MVC 发送包含 XML 文档的 POJO

asp.net - 无法在 OpenXMl Cell 样式中使用多种字体

c# - Excel 到 SQL、C# 库