Inserting multi-line text in table cell

Mar 9, 2011 at 7:33 PM

Hello Guys,

I was trying to create table cell with multi-line text in it. I have noticed that while creating document.xml file, PHPword would strip the new line characters '\n' from the text.

So, I have modified _writeText function in Word2007/Base.php file and added multi line text support. Changes I made are in bold:

I am not sure if this is a perfect solution but it works for me. I hope someone will find it useful.


class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart {
    protected function _writeText(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Text $text, $withoutP = false) {
        $styleFont = $text->getFontStyle();
        $SfIsObject = ($styleFont instanceof PHPWord_Style_Font) ? true : false;
        $strText = htmlspecialchars($text->getText());
        // create array of newlines

        // cycle through the array and create new paragraph per new line
        for ($iii = 0; $iii < count($str_tmp); $iii++) {

        if(!$withoutP) {
            $styleParagraph = $text->getParagraphStyle();
            $SpIsObject = ($styleParagraph instanceof PHPWord_Style_Paragraph) ? true : false;
            if($SpIsObject) {
                $this->_writeParagraphStyle($objWriter, $styleParagraph);
            } elseif(!$SpIsObject && !is_null($styleParagraph)) {
                        $objWriter->writeAttribute('w:val', $styleParagraph);

        $strText = $str_tmp[$iii];   

        $strText = PHPWord_Shared_String::ControlCharacterPHP2OOXML($strText); // this is where new line characters are striped.
            if($SfIsObject) {
                $this->_writeTextStyle($objWriter, $styleFont);
            } elseif(!$SfIsObject && !is_null($styleFont)) {
                        $objWriter->writeAttribute('w:val', $styleFont);
                $objWriter->writeAttribute('xml:space', 'preserve'); // needed because of drawing spaces before and after text

        $objWriter->endElement(); // w:r
        if(!$withoutP) {
            $objWriter->endElement(); // w:p
    } // end of for $iii




Nov 3, 2011 at 2:16 PM
Edited Nov 3, 2011 at 2:29 PM

Dear Lenny


For _weeks_ I've been looking for this solution and finally I tracked down this post.

THANK YOU a million times over - my exporting-visit-activities-to-word application for Clinical Research Nurses now WORKS SEAMLESSLY.

Am off to celebrate...


Dec 13, 2011 at 6:41 PM

Would you help me to get the same functionality in TextRun?

My goal looks like this ("MyBoldTitle" is hardcoded and the text after it comes from an Input-Field.):

"MyBoldTitle: This is a text on the same line with MyBoldTitle and now
there is a single line break, not a new paragraph. And now
there is another one.
MyBoldTitle2: This is a text on the same line with MyBoldTitle and now
there is a single line break, not a new paragraph. And now
there is another one."

If I use addText() for title and text seperately I get a linebreak after "MyBoldTitle:" too.
--> I use the hack above to get the linebreaks inside the text after "MyBoldTitle:".

If I use createTextRun() of course I get Title and Text on the same line. Unfortunately it kills all linebreaks in the text.

Mar 1, 2013 at 2:35 PM
I hope this answer fills your needs! I have looked for this for a few hours during development and I couldn't find it. I had to bet a co-worker 20$ that he couldn't make a new line happen in a cell without editing the actual PHPWord library. I am now out 20$ and have this code, so I thought I would share to keep the money in your pocket.
Here it is :
// Create table element
$table = $section->addTable($tableStyle);

// Add a row as normal.

//  The important thing to note here is that you when you create the cell 
// that you want to have the multi-line on, is that you don't add text to it.
$cell = $table->addRow($cellWidth, $cellStyle);

// Add as many text to the cell as you want lines.  
$cell->addText("Line 2", $fontStyle, $paragraphStyle);
$cell->addText("Line 2", $fontStyle, $paragraphStyle);

/** continue with rest of table declaration */
In the end you will have an example like sailcomp posted above.

Good luck!
Jun 6, 2013 at 6:33 PM
Edited Jun 6, 2013 at 6:33 PM
basically same solution here:

This is taking advantage of the fact that ->addText adds a newline. (which some people don't want anyway, but in this situation, it's used to achieve the desired result)
  $positionTitleArray = explode("\n", $positionTitle);
  foreach ($positionTitleArray as $positionLine) {
    $cell->addText($positionLine, null, $tableSpace);