php com calls word to get the total number of pages of the document and the number of pages where the current content is located

$file = 'C:/Users/Administrator/Desktop/11.doc';
        $word = new \com("word.application",null,CP_UTF8) or die("Unable to instantiate Word");
        $word->Visible = 0;
        $word->Documents->Open($file,true,false);
        $docRange = $word->ActiveDocument->Content;
        //https://docs.microsoft.com/en-us/office/vba/api/word.range.information
        // 这个比较坑,information明明是个方法,却在文档的属性里
        //https://docs.microsoft.com/en-us/office/vba/api/word.wdinformation
        // wdFirstCharacterColumnNumber 9 开头列号
        // wdFirstCharacterLineNumber 10 开头行号
        echo $docRange->Information(4);//wdNumberOfPagesInDocument 页数
        echo '<br/>';
        $Paragraphs = $word->ActiveDocument->Paragraphs;
        for($i=1;$i<$Paragraphs->Count;$i++){
            // 该段落所在页码
            echo  $Paragraphs->Item($i)->Range->Information(3);
            echo '<br/>';
             // 该段落开头行号列号  
            echo  $Paragraphs->Item($i)->Range->Information(9);
            echo '&nbsp;&nbsp;&nbsp;&nbsp;';
            echo  $Paragraphs->Item($i)->Range->Information(10);
            echo '<br/><br/>';
        }
        $word->ActiveDocument->Save();
        $word->Quit();
        $word = null;

Guess you like

Origin blog.csdn.net/wang740209668/article/details/108715888