When using PHPWord can I format inline text when calling addListItem?

浪子不回头ぞ 提交于 2021-01-29 03:38:36

问题


I'm using PHPWord (https://github.com/PHPOffice/PHPWord/tree/develop) to generate a number of bulleted items appearing within a Word document. I am relying on addListItem() to add the bullet items to the document and that is working just fine.

The only problem I'm having is that I also need format some of the text appearing in the bulleted item with a bold word appearing first followed by non-bold text appearing afterwards; all within the same line.

For example:

  • bold word: more information about the bolded word all in one line

I can't figure out if that's a possibility using PHPWord. I know that I can create a textRun object via addTextRun() to create the formatting I want, but when I do that it's not added as a bullet in the list.

Is there a way to get the best of both worlds? Can I not only get the benefit of a textRun object and also have it appear as a bullet?


回答1:


yep, the thing you are searching for is the listItemRun. Directly from the samples 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));


来源:https://stackoverflow.com/questions/40271800/when-using-phpword-can-i-format-inline-text-when-calling-addlistitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!