php comワードセット編集可能領域(制限付き編集、一部編集可能、一部編集不可)

ここに画像の説明を挿入
テストドキュメントの内容は上図のとおりです。

 // 打开word 编辑
        $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);
        // wdAllowOnlyReading 3
        // https://docs.microsoft.com/en-us/office/vba/api/word.wdprotectiontype
        if($word->ActiveDocument->ProtectionType!=-1){
            $word->ActiveDocument->UnProtect('123456');
        }
        $Paragraphs = $word->ActiveDocument->Paragraphs;
        for ($i = 1; $i <= $Paragraphs->Count; $i++) {
            if($i==1||$i==3){ //给需要编辑的区域添加editor
                // -1 代表everyone
                $Paragraphs->Item($i)->Range->Editors->Add(-1);
            }
        }
        $word->ActiveDocument->Protect(3,false,'123456',false,true);
        $word->ActiveDocument->Save();
        $word->Quit();
        $word = null;

保護されたドキュメントの編集可能な部分を編集不可にする場合は、その領域のエディタを削除するだけで済みます。たとえば、上記のドキュメントを次のトスに通すと、ドキュメントのすべての部分が編集不能になります。

$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);

        $Paragraphs = $word->ActiveDocument->Paragraphs;
        for ($i = 1; $i <= $Paragraphs->Count; $i++) {
            $Editors = $Paragraphs->Item($i)->Range->Editors;
            if($Editors->Count>0){
                for($j=1;$j<=$Editors->Count;$j++){
                    $Editors->Item($j)->Delete();
                }
            }
        }

        $word->ActiveDocument->Save();
        $word->Quit();
        $word = null;

おすすめ

転載: blog.csdn.net/wang740209668/article/details/108604975