如何使用 Open XML SDK 获取 MS Word 总页数? [英] How to get MS Word total pages count using Open XML SDK?

查看:50
本文介绍了如何使用 Open XML SDK 获取 MS Word 总页数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码来获取页数,但它没有给出实际的页数(PFA).获得总页数的更好方法是什么?

I am using below code to get the page count but it is not giving actual page count(PFA). What is the better way to get the total pages count?

var pageCount = doc.ExtendedFilePropertiesPart.Properties.Pages.Text.Trim();

注意:我们不能在我的 Azure Web 应用服务中使用 Office 主互操作程序集

Note: We cannot use the Office Primary Interop Assemblies in my Azure web app service

提前致谢.

推荐答案

理论上,以下属性可以使用 Open XML SDK 从 Word Open XML 文件返回该信息:

In theory, the following property can return that information from the Word Open XML file, using the Open XML SDK:

int pageCount = (int) document.ExtendedFilePropertiesPart.Properties.Pages.Text;

然而,在实践中,这并不可靠.它可能有效,但也可能无效 - 这完全取决于 1) Word 在关闭文件之前设法将其保存在文件中,以及 2) 可能对关闭的文件进行了何种编辑.

In practice, however, this isn't reliable. It might work, but then again, it might not - it all depends on 1) What Word managed to save in the file before it was closed and 2) what kind of editing may have been done on the closed file.

获取页码或页数的唯一可靠方法是在 Word 应用程序界面中打开文档.页数和页数在编辑期间由 Word 动态计算.当文档关闭时,此信息是静态的,不一定是打开或打印文档时的信息.

The only sure way to get a page number or a page count is to open a document in the Word application interface. Page count and number of pages is calculated dynamically, during editing, by Word. When a document is closed, this information is static and not necessarily what it will be when the document is open or printed.

另见 https://github.com/OfficeDev/Open-XML-SDK/issues/22 用于确认.

这篇关于如何使用 Open XML SDK 获取 MS Word 总页数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆