我正在使用php单词库在现有的word文档中添加水印。
当标头为空时,我可以添加水印,但是当标头不为空时,我不能添加水印。
我有一个单词文档“ test_doc.docx”,其中的标题中写有一些文本,并且有3页,而3页的标题中有相同的文本。
当我下载文件时-
下面是我的代码-
$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);