简体   繁体   English

无法使用 OpenXml 从 word 文档中获取页数

[英]Can't get the pages count from a word document with OpenXml

I use the OpenXML SDK v2.11.3 with .net core 3.1 and when I try to get the total pages count from a word document sometimes it returns a null value.我将 OpenXML SDK v2.11.3 与 .net 核心 3.1 一起使用,当我尝试从 word 文档获取总页数时,有时它会返回 null 值。

using (var document = WordprocessingDocument.Open(memoryStream, true))
{​​​​
    var pages = Convert.ToInt32(document.ExtendedFilePropertiesPart.Properties.Pages.Text);
}

System.NullReferenceException: 'Object reference not set to an instance of an object.' System.NullReferenceException:“对象引用未设置为 object 的实例。” DocumentFormat.OpenXml.ExtendedProperties.Properties.Pages.get returned null. DocumentFormat.OpenXml.ExtendedProperties.Properties.Pages.get 返回 null。

What is the best way to get the total pages count from a docx file?从 docx 文件中获取总页数的最佳方法是什么?

Since自从

  1. pagination is a dynamic property dependent upon rendering,分页是一个依赖于渲染的动态属性,
  2. any given DOCX file may or may not have ever been rendered, and任何给定的 DOCX 文件可能会也可能不会被渲染,并且
  3. OpenXML SDK does not render or perform calculations needed for rendering, OpenXML SDK 不渲染或执行渲染所需的计算,

you cannot necessarily obtain a page count from an arbitrary DOCX file.您不一定能从任意 DOCX 文件中获取页数。

For further details and some limited work-arounds, see How to access OpenXML content by page number?有关更多详细信息和一些有限的解决方法,请参阅如何按页码访问 OpenXML 内容?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM