我试图在现有Word文档上添加水印,但无法添加。有很多创建新文档的例子,然后根据需要进行编辑。但找不到任何教程来编辑预先存在的word文档。
下面是我的代码。提前谢谢。

<?php
require_once '../PHPWord.php';

// New Word Document
$PHPWord = new PHPWord();

// New portrait section
$section = $PHPWord->createSection();

// Create header
$header = $section->createHeader();

// Add a watermark to the header
$header->addWatermark('_earth.jpg', array('marginTop'=>200, 'marginLeft'=>55));

$section->addText("Hello World");

// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('Watermark.docx');
?>

最佳答案

你可以在这里找到一个例子:
https://github.com/PHPOffice/PHPWord/blob/develop/samples/Sample_11_ReadWord2007.php
对于您的问题,要打开文档,您需要替换行

$PHPWord = new PHPWord();

具有
$PHPWord = PhpWord\IOFactory::load('<file path of your MSWord document>'); // or \PhpOffice\PhpWord\IOFactory depending on your environment

09-30 23:49