Art Geigel
Art Geigel

Reputation: 2002

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

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:

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?

Upvotes: 1

Views: 3064

Answers (1)

ejuhjav
ejuhjav

Reputation: 2710

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

Upvotes: 3

Related Questions