3

I'm a Japanese.So maybe my English is so bad, sorry.

I Want

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.

My Code

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.

Postscript

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(

  • Are you getting any errors? Give https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display a read – IsThisJavascript Mar 05 '18 at 11:34
  • Thank you for your comment! But I have no errors.... I add the code, ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); But I have never shown errors. – beginner programmer Mar 05 '18 at 11:38
  • Is the file being generated? Please go into more detail about what is happening – IsThisJavascript Mar 05 '18 at 11:51
  • Yes, file is been generated. The file is exported correctly, but I can not add the Track Revisions. However, if I edit the output file directly with MS Word, the Track Revisions is enabled. – beginner programmer Mar 06 '18 at 01:11

1 Answers1

1

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