【问题标题】:How to add watermark on existing Word Document using PHPWord?如何使用 PHPWord 在现有 Word 文档上添加水印?
【发布时间】:2017-07-27 06:56:27
【问题描述】:

我正在尝试在现有的 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');
?>

【问题讨论】:

    标签: php core doc phpword


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      您可以使用 PHPOffice 电子表格库

      示例工作代码如下

      <?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');
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-03
        • 1970-01-01
        • 2018-06-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多