首页 > 解决方案 > PHPWORD页脚文本对齐问题

问题描述

我正在使用 phpword 生成一个 word 文件。我的页脚有问题。在页脚中,我有页码和文本机密文档。页码必须左对齐,机密文档必须右对齐。我尝试了下面的脚本,但文本和页码都只左对齐。

$phpword_object = new PHPWord();
$section = $phpword_object->createSection();

$footer = $section->createFooter();
$footer->addPreserveText('Page {PAGE} of {NUMPAGES}.',     
array(
    'align' => 'end',
    'positioning' => 'absolute'
));

$text = 'Confidential Document';

$footer->addPreserveText( $text,
    array(
        'align' => 'start',
        'positioning' => 'absolute'
    ));

标签: phpphpword

解决方案


就个人而言,我会用一个简单的右对齐标签来做到这一点。它更容易理解,无论是在代码形式中,还是在你面前的 Word 中打开 docx 文件后。

所以,假设你有一个 A4 页面,两边有 2.5 厘米的边距,给你一个 16 厘米宽的区域,所以把标签放在 16 厘米处,像这样(这段代码有效 - 我刚刚试过):

$phpWord->addParagraphStyle('footer_paragraph', array('tabs' => array(new \PhpOffice\PhpWord\Style\Tab('right', \PhpOffice\PhpWord\Shared\Converter::cmToTwip(16)))));

$footer = $section->addFooter();

$footer->addPreserveText('Page {PAGE} of {SECTIONPAGES}' . "\u{0009}Confidential Document", 'your_own_font_style', 'footer_paragraph');


推荐阅读