无法将水印添加到现有的Word文档中,但是当使用PhpWord的标题为空时,它可以正常工作

时间:2020-10-09 15:51:13

标签: php ms-word phpword phpoffice

我正在使用php单词库在现有的word文档中添加水印。

当标头为空时,我可以添加水印,但是当标头不为空时,我不能添加水印。

我有一个单词文档“ test_doc.docx”,其中的标题中写有一些文本,并且有3页,而3页的标题中有相同​​的文本。

当我下载文件时-

  1. 我看不到水印。
  2. 在第一页上,我的标题文本丢失了,但在其他两页中却可以看到标题文本。

下面是我的代码-

$filename = "test_doc.docx";

$file_path = APPPATH."templates/test_doc.docx";


$phpWord = \PhpOffice\PhpWord\IOFactory::load($file_path);

$phpWord->getCompatibility()->setOoxmlVersion(14);
$phpWord->getCompatibility()->setOoxmlVersion(15);

$sections = $phpWord->getSections();
$section = $sections[0];
// get all headers
$headers = $section->getHeaders();

if (empty($headers)) {
 // inside when header is empty
 $header = $section->addHeader();
} else {
 // inside when header is not empty
 $header = $headers[1];
}

$header->addWatermark(IMG.'help.png', array('marginTop' => 100, 'marginLeft' => 20));

$phpWord->save($filename, 'Word2007', true);

0 个答案:

没有答案
相关问题