【问题标题】:PHPWord bold certain words on a line [closed]PHPWord在一行上加粗某些单词[关闭]
【发布时间】:2013-07-12 01:34:11
【问题描述】:

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

【问题讨论】:

    标签: php ms-word doc bold phpword


    【解决方案1】:

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

    $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);
        }
    }
    

    你可以调整它以获得你想要的,但是,基本上,通过使用上述方法,可以在同一行中获得不同的样式。

    【讨论】:

    • 感谢您的帮助!
    • 我做了一个快速函数来做你需要的你可以在这里找到它->pastebin.com/qDMcsivW
    • 有谁知道在使用 addListItem() 处理项目符号文本时是否也可以应用内联格式?
    猜你喜欢
    • 2017-06-12
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 2020-07-29
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    相关资源
    最近更新 更多