Forum


Replies: 7   Views: 261
Can't pass 'pstyle' from one wordfragment to another

Posted by admin  · 09-08-2019 - 16:06

Hello,

If the first parameter of addText is an array:

$individualparts = array();
...
$individualparts[4] = $text1;
...
$wfMain->addText($individualparts);

The method gets the inline contents of the WordFragments, so paragraph tags are removed (pStyle are applied to paragraph tags). If you set a pStyle in the second parameter, then it applies to the whole paragraph and character styles (like caps) are inherited to child contents. MS Word doesn't allow applying custom paragraph styles to run-of-text contents (only to paragraph tags), you need to use character styles to keep them when using an array and addText. You can use the rStyle option to apply a character style.

This sample uses character styles:

$text = array();
$text[] =
    array(
        'text' => 'Text MyStyle',
        'rStyle' => 'MyStyle1'
);
$text[] =
    array(
        'text' => ' text MyStyle2.',
        'rStyle' => 'MyStyle2'
);

$docx->addText($text);

You can use importStyles and createCharacterStyle to import and generate character styles.

Regards.