我正在尝试在我现有的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');
?>
答案 0 :(得分:1)
你可以在这里找到一个例子: 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
答案 1 :(得分:0)
您可以使用PHPOffice spreadspheet库
Sampl eworkingcode在下面
<?php
require_once("vendor/autoload.php");
// Your word file as input / existing file
$name = basename(__FILE__, '.php');
$name="sample";
$source = __DIR__ . "/{$name}.docx";
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source);
// Get the initial Section of header and add watermark image to it
$section = $phpWord->getSection(0);
$header = $section->createHeader();
$header->addWatermark('watermark_sample.png', array('marginTop' => 0, 'marginLeft' => 0));
// write the output file
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');