Search code examples
phpphpword

Alternate bold and regular text in phpWord footer


I can alternate plain and bold text in the body of a document using textrun within a section.

$textrun->addText(' Short address here ');
$textrun->addText(' T ', $boldFontStyleName);
$textrun->addText(' ++353 1 5552999. ');

But I can't get the same effect where I need it, in the FOOTER. Text is added with $footer->addText which doesn't allow inline additions of new text.

I've tried everything I know, including concatenating and assigning $textrun->addText outputs to a new $variable, which I add just once to the footer via $footer->addText($variable); No luck.

My current code is as follows, and I'd be really grateful if someone could tweak it to work. Or is it just that footers in phpWord don't support this level of formatting?

// footer
$footer = $section->addFooter();

// define bold style
$boldFontStyleName = 'BoldText';
$phpWord->addFontStyle($boldFontStyleName, array('bold' => true));

// add text
$footer->addText(' Short address here ');
$footer->addText(' T ', $boldFontStyleName);
$footer->addText(' ++353 1 5552999. ');

Solution

  • you can use textrun similarly in footer as you would in section (at least in phpword version 0.13.0):

    // footer
    $footer = $section->addFooter();
    $textrun = $footer->addTextRun();
    
    // define bold style
    $boldFontStyleName = 'BoldText';
    $phpWord->addFontStyle($boldFontStyleName, array('bold' => true));
    
    // add text
    $textrun->addText(' Short address here ');
    $textrun->addText(' T ', $boldFontStyleName);
    $textrun->addText(' ++353 1 5552999. ');