This project is read-only.

ListItem with text in differents formats

Aug 11, 2010 at 4:16 PM

I was trying to do the same thing that textrun does, have a paragraph with text in differents formats in a listitem, but i couldn't do it.And reviewing the code i did some changes to do that..

first call the method in this way


$list=$section->addListItem(utf8_decode('Normal ', 0);
$list->addText('Bold',array('bold' => 'Bold'));


until here no changes are necessary.....

Now in PHPWord -> Section -> ListItem.php, I modified the constructor and added one attribute (elementCollection) and two methods (getElements and addText)



private $_elementCollection;
public function addText($text = null, $styleFont = null) 
   $givenText = utf8_encode($text); 
   $text = new PHPWord_Section_Text($givenText, $styleFont); 
   $this->_elementCollection[] = $text; 
   return $text; 
public function getElements() 
   return $this->_elementCollection; 
public function __construct($text, $depth = 0, $styleFont = null, $styleList = null, $styleParagraph = null) 
   $this->_style = new PHPWord_Style_ListItem(); 
   $this->_textObject = new PHPWord_Section_Text($text, $styleFont, $styleParagraph);                
   $this->_elementCollection[] = $this->_textObject;                
   $this->_depth = $depth;     
   if(!is_null($styleList) && is_array($styleList)) 
      foreach($styleList as $key => $value) 
         if(substr($key, 0, 1) != '_'
            $key = '_'.$key; 
         $this->_style->setStyleValue($key, $value); 

and finally in PHPWord -> Writer -> Word2007 -> Document.php I modified the method writeListItem replacing 


$this->_writeText($objWriter, $textObject, true);

for this

$elements = $listItem->getElements(); 
if(count($elements) > 0) 
   foreach($elements as $element) 
      if($element instanceof PHPWord_Section_Text) 
         $this->_writeText($objWriter, $element, true); 
      elseif($element instanceof PHPWord_Section_Link) 
         $this->_writeLink($objWriter, $element, true); 

and done. Now you can add text with different formats in the same line of the list.



Aug 11, 2010 at 4:57 PM

Really awesome!

I will integrate your code in the next release.