使用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
一般来说也没有帮助,因为:
- 根据定义,当内容具有以下内容时,
w:lastRenderedPageBreak
位置已过时 自上次打开后已被对其分页的程序更改 内容。 - 在 MS Word 的实现中,
w:lastRenderedPageBreak
已知在多种情况下都不可靠,包括
如果您愿意接受对 Word Automation 及其所有固有功能的依赖 licensing and server operation limitations ,然后您就有机会确定页面边界、页码、页数等。
否则,唯一真正的答案是超越基于页面的引用框架,该框架依赖于专有的、特定于实现的分页算法。
关于c# - 如何通过页码访问OpenXML内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47228220/