【问题标题】:PHP Word - Add Track RevisionsPHP Word - 添加音轨修订
【发布时间】:2018-03-05 11:33:16
【问题描述】:

我是日本人。所以也许我的英语太差了,抱歉。

我想要

我想通过 PHP WORD 将“Track Revisions”添加到 *.docx。 但我找不到怎么做。

1、通过PHP WORD添加一些文档。 2、通过PHP WORD在文档中添加一些Track Revisions。 3、通过docx文件输出文档。 4、用Microsoft Word打开文件,可以看到带有Track Revisions的文件。

我的代码

我写了这段代码,但我做不到。

<?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");

我该怎么办? 如果你知道怎么做,请告诉我怎么做。

谢谢。

后记

我找到了这本手册,https://media.readthedocs.org/pdf/phpword/develop/phpword.pdf 和 28 页。

他们说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().

但是,我的 IDE(IntelliJ) 没有找到 setChangeInfo 方法和 setTrackChange 方法... X(

【问题讨论】:

  • 您是否遇到任何错误?阅读stackoverflow.com/questions/1053424/…
  • 感谢您的评论!但我没有错误......我添加了代码 ini_set('display_errors', 1); ini_set('display_startup_errors', 1);错误报告(E_ALL);但我从未显示过错误。
  • 文件是否正在生成?请详细了解正在发生的事情
  • 是的,文件已生成。该文件已正确导出,但我无法添加轨道修订。但是,如果我直接使用 MS Word 编辑输出文件,则会启用 Track Revisions。

标签: php phpword


【解决方案1】:

我发现我该怎么做。 使用 PHP Word v0.14.0 无法将 Track Revisions 添加到 docx。

(1)我必须使用develop分支的代码。

composer require phpoffice/phpword:dev-develop composer update

(2) 使用此代码

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

参考书目 https://nnahito.com/articles/31

【讨论】:

    猜你喜欢
    • 2017-03-21
    • 2015-02-17
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    相关资源
    最近更新 更多