PHPWord在一行上加粗某些单词

use*_*723 6 php ms-word doc bold phpword

我想知道是否有办法在一条线上加粗某些单词.例如,如果我想要一行粗线上的每个第三个单词,我该怎么做.我目前正在使用addText,但这需要整行粗体或不粗体.任何回复将不胜感激.

sin*_*ake 5

您将需要使用createTextRun()方法.我试过Text.php文件Examples夹中的文件,这里是与您的问题相关的代码:

$textrun = $section->createTextRun();
$sentence='I am sentence, and every third word will be bold. This is bold.';
$word_arr=explode(' ', $sentence);

$styleFont = array('bold'=>true, 'size'=>16, 'name'=>'Calibri');
$styleFont2 = array('bold'=>false, 'size'=>16, 'name'=>'Calibri');

$c = 0;
for($i = 0; $i < count($word_arr); $i++) 
{
    $c++;
    if($c % 3 == 0) 
    {
        $textrun->addText($word_arr[$i].' ', $styleFont);
    }
    else 
    {
        $textrun->addText($word_arr[$i].' ', $styleFont2);
    }
}
Run Code Online (Sandbox Code Playgroud)

你可以调整它以获得你想要的东西,但是,基本上,通过使用所提到的方法,可以在同一行中获得不同的样式.