【问题标题】:When using PHPWord can I format inline text when calling addListItem?使用 PHPWord 时,我可以在调用 addListItem 时格式化内联文本吗?
【发布时间】:2017-03-09 09:09:48
【问题描述】:

我正在使用 PHPWord (https://github.com/PHPOffice/PHPWord/tree/develop) 生成出现在 Word 文档中的许多项目符号项目。我依靠 addListItem() 将项目符号项添加到文档中,并且效果很好。

我遇到的唯一问题是我需要格式化出现在项目符号项中的一些文本,首先出现一个粗体字,然后是非粗体文本;都在同一行。

例如:

  • 粗体字:更多关于粗体字的信息都在一行中

我不知道这是否有可能使用 PHPWord。我知道我可以通过 addTextRun() 创建一个 textRun 对象来创建我想要的格式,但是当我这样做时,它不会作为项目符号添加到列表中。

有没有办法两全其美?我能否不仅获得 textRun 对象的好处,而且还让它显示为项目符号?

【问题讨论】:

    标签: php ms-word phpword


    【解决方案1】:

    是的,您要搜索的是 listItemRun。直接来自样本https://github.com/PHPOffice/PHPWord/blob/develop/samples/Sample_14_ListItem.php

    $section->addText(htmlspecialchars('List with inline formatting.', ENT_COMPAT, 'UTF-8'));
    $listItemRun = $section->addListItemRun();
    $listItemRun->addText(htmlspecialchars('List item 1', ENT_COMPAT, 'UTF-8'));
    $listItemRun->addText(htmlspecialchars(' in bold', ENT_COMPAT, 'UTF-8'), array('bold' => true));
    $listItemRun = $section->addListItemRun();
    $listItemRun->addText(htmlspecialchars('List item 2', ENT_COMPAT, 'UTF-8'));
    $listItemRun->addText(htmlspecialchars(' in italic', ENT_COMPAT, 'UTF-8'), array('italic' => true));
    

    【讨论】:

    • 太棒了。正是我想要的。谢谢。
    猜你喜欢
    • 2018-01-01
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 2017-03-18
    • 1970-01-01
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    相关资源
    最近更新 更多