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

问题描述

我正在使用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);

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)