PHPword,获取总页数

时间:2023-02-09 15:56:34

I'm using PHPword to create a docx file. I need a method/function that can get the total number of pages created.

我正在使用PHPword来创建docx文件。我需要一个方法/函数来获取创建的总页数。

I know PHPword does support pages numbering which is easily done using $footer->addPreserveText('Page {PAGE} of {NUMPAGES}.');

我知道PHPword确实支持使用$ footer-> addPreserveText({NUMPAGES}的'Page {PAGE}')轻松完成的页码编号。

once i got the total number of pages, i can create extra pages if the document has less the 20 pages.

一旦我获得了总页数,如果文档少于20页,我可以创建额外的页面。

2 个解决方案

#1


I also asked this question. While there isn't a built-in per se, you can get the info by opening the ZIP file manually and looking at the app.xml file within

我也问了这个问题。虽然本身没有内置功能,但您可以通过手动打开ZIP文件并查看内部的app.xml文件来获取信息。

Check out my answer and code sample here: https://*.com/a/40228208/6247265

在这里查看我的答案和代码示例:https://*.com/a/40228208/6247265

And here's the relevant code:

这是相关的代码:

 // Include PHPWord and other stuff before here
  function getPages() {
    $zip = new \PhpOffice\PhpWord\Shared\ZipArchive();
    $zip->open("/path/to/your/document.docx");
    preg_match("/\<Pages>(.*)\<\/Pages\>/", $zip->getFromName("docProps/app.xml"), $var);
    return $var[0];
  }

There's a catch to this: the page numbers will be wrong. I got around this by exec()ing to winword.exe using the /m switch to run the ToolsWordCountRecount macro. So something like this:

有一个问题:页码是错误的。我使用/ m开关通过exec()转到winword.exe来运行ToolsWordCountRecount宏。所以像这样:

c:\path\to\winword.exe /mToolsWordCountRecount /mFileSave /mFileCloseOrExit yourfile.docx

c:\ path \ to \ winword.exe / mToolsWordCountRecount / mFileSave / mFileCloseOrExit yourfile.docx

This forces Word to recount the pages, saves the document then quits. Then you can run getPages to correctly count the number of pages in a document.

这会强制Word重新计算页面,保存文档然后退出。然后,您可以运行getPages以正确计算文档中的页数。

#2


The {PAGE} and {NUMPAGES} are specific word field codes that are processed by Word instead than phpword, i.e. phpword doesn't actually know the page number values, it just passes these field codes as they are into the resulting word document.

{PAGE}和{NUMPAGES}是由Word而不是phpword处理的特定字段字段代码,即phpword实际上不知道页码值,它只是将这些字段代码传递给生成的word文档。

As an answer to the original question, I don't think that you can get the number of pages information from phpword.

作为对原始问题的回答,我认为您不能从phpword获取页面信息的数量。

more information about the word field values

有关单词字段值的更多信息

#1


I also asked this question. While there isn't a built-in per se, you can get the info by opening the ZIP file manually and looking at the app.xml file within

我也问了这个问题。虽然本身没有内置功能,但您可以通过手动打开ZIP文件并查看内部的app.xml文件来获取信息。

Check out my answer and code sample here: https://*.com/a/40228208/6247265

在这里查看我的答案和代码示例:https://*.com/a/40228208/6247265

And here's the relevant code:

这是相关的代码:

 // Include PHPWord and other stuff before here
  function getPages() {
    $zip = new \PhpOffice\PhpWord\Shared\ZipArchive();
    $zip->open("/path/to/your/document.docx");
    preg_match("/\<Pages>(.*)\<\/Pages\>/", $zip->getFromName("docProps/app.xml"), $var);
    return $var[0];
  }

There's a catch to this: the page numbers will be wrong. I got around this by exec()ing to winword.exe using the /m switch to run the ToolsWordCountRecount macro. So something like this:

有一个问题:页码是错误的。我使用/ m开关通过exec()转到winword.exe来运行ToolsWordCountRecount宏。所以像这样:

c:\path\to\winword.exe /mToolsWordCountRecount /mFileSave /mFileCloseOrExit yourfile.docx

c:\ path \ to \ winword.exe / mToolsWordCountRecount / mFileSave / mFileCloseOrExit yourfile.docx

This forces Word to recount the pages, saves the document then quits. Then you can run getPages to correctly count the number of pages in a document.

这会强制Word重新计算页面,保存文档然后退出。然后,您可以运行getPages以正确计算文档中的页数。

#2


The {PAGE} and {NUMPAGES} are specific word field codes that are processed by Word instead than phpword, i.e. phpword doesn't actually know the page number values, it just passes these field codes as they are into the resulting word document.

{PAGE}和{NUMPAGES}是由Word而不是phpword处理的特定字段字段代码,即phpword实际上不知道页码值,它只是将这些字段代码传递给生成的word文档。

As an answer to the original question, I don't think that you can get the number of pages information from phpword.

作为对原始问题的回答,我认为您不能从phpword获取页面信息的数量。

more information about the word field values

有关单词字段值的更多信息