I'm a Japanese.So maybe my English is so bad, sorry.
I want to add "Track Revisions" to *.docx By PHP WORD. But I can't find how to do it.
1, Add some documents by PHP WORD. 2, Add some Track Revisions to the documents by PHP WORD. 3, Output the documents by docx file. 4, Open the file by Microsoft Word, and we can see the documents with Track Revisions.
I wrote this code, but i can't do.
<?php
require_once 'vendor/autoload.php';
$phpword = new PhpOffice\PhpWord\PhpWord();
$phpword->getSettings()->setTrackRevisions(true);
$section = $phpword->addSection();
$section->addText('some text');
// output
$objWriter = PhpOffice\PhpWord\IOFactory::createWriter($phpword, 'Word2007');
$objWriter->save('helloWorld.docx');
// ===========================================
// read file
$reader = PhpOffice\PhpWord\IOFactory::load("helloWorld.docx", 'Word2007');
$trackChangesView = new PhpOffice\PhpWord\ComplexType\TrackChangesView();
$section2 = $reader->addSection();
$trackChangesView->setComments('history');
$sugoiyatsu = $section2->addTextRun();
$sugoiyatsu->addText('some some text');
$writer = PhpOffice\PhpWord\IOFactory::createWriter($reader, 'Word2007');
$writer->save("sample.docx");
How can I do? If you know how to do this, please tell me how to do.
Thank you.
I found this manual, https://media.readthedocs.org/pdf/phpword/develop/phpword.pdf and page of 28.
They said that Track changes can be set on text elements. There are 2 ways to set the change information on an element. Either by
calling the setChangeInfo(), or by setting the TrackChange instance on the element with setTrackChange().
.
However, My IDE(IntelliJ) didn't found setChangeInfo
method and setTrackChange
method... X(
I found how do I this. It is impossible to add Track Revisions to docx using PHP Word v0.14.0.
(1)I must use develop branch's code.
composer require phpoffice/phpword:dev-develop
composer update
(2) Use this code
<?php
require_once 'vendor/autoload.php';
use PhpOffice\PhpWord\Element\TrackChange;
$phpword = new PhpOffice\PhpWord\PhpWord();
$section = $phpword->addSection();
$textRun = $section->addTextRun();
$text = $textRun->addText('I am TEXT');
$text->setChangeInfo(TrackChange::INSERTED, 'nnahito', time() - 1800);
Bibliography https://nnahito.com/articles/31