【问题标题】:PhpWord add footer to templatePhpWord 在模板中添加页脚
【发布时间】:2020-10-13 10:20:27
【问题描述】:

我想在我创建的文档中添加页脚,我如何使用 phpword 来做到这一点?

我的模板处理代码:

$template = new \PhpOffice\PhpWord\TemplateProcessor("my-template.docx");

$table = new \PhpOffice\PhpWord\Element\Table();

$table->addRow();
$table->addCell()->addText("test");

$template->setComplexBlock('table_var', $table);

$template->saveAs("new-file.docx");

【问题讨论】:

    标签: php phpword


    【解决方案1】:

    another post 中所述,首先保存您的模板,然后再次打开结果,如:

    $file = \PhpOffice\PhpWord\IOFactory::load("new-file.docx");
    
    // Get Sections from the imported document...
    $sections = $file->getSections();
    $section = $sections[0];
    

    然后,如果您的模板没有任何页脚,请添加如下:

    $footer1 = $section->createFooter();
    $footer1->addImage('images/footer.png');
    

    或者,您可以编辑现有的页脚,例如:

    // Note that the first index is 1 here (not 0).
    $footers = $section->getFooters();
    $footer1 = $footers[1];
    
    // And first index is 0 for elements normally.
    $elements = $footer1->getElements();
    $element1 = $elements[0];
    
    // For example manipulating simple text information ($element1 is instance of Text object)
    $element1->setText("My new text, old content: " . $element1->getText());
    

    【讨论】:

    • $template->getSections();未定义。
    • 对不起,编辑了再试一次(在你把打开方式改成上面提到的那个之后)。
    • 我想用 TemplateProcessor 添加页脚,因为 IOFactory::load 会转储现有文档上的所有格式。
    • 找不到 API,但已编辑为解决方法。
    • 目前有一个打开的issue on GitHub,所以要么使用我的解决方法,要么先保存模板,然后使用 IOFactory 打开结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    • 2018-03-02
    • 2013-01-11
    • 2019-07-29
    相关资源
    最近更新 更多