This project is read-only.

Calling addText() method gives a line break at the end of string

Apr 28, 2010 at 2:23 PM

Hi,

   I was using addText() method and for my condition, I am putting different styles on different strings in a single sentence. So, when I call addText() method for each individual phrase with different styles, it gives me the text in a new line. I want them all to be in a single line. Following is the example of code I am writing:

What I want:

Hello All!

I am putting these two Strings in two different addText() method.

$section->addText("Hello", array("color" => "red"));

$section->addText("All!", array("color" => "green"));

And what I am getting:

Hello

All!

How to disable these new line settings in addText() method?

 

 

Thanks,

Swarnendu

 

Apr 28, 2010 at 7:14 PM
Hello Swarnendu,

thanks for the feature request, I've added this into the feature
requests list.
The main problem is, that the docx writer always creates a new paragraph
for each text element.

For the moment, there is no workaround. sorry. But I think this feature
will be implemented in one the next releases we're planning at the moment.

best regards

Matthias


> From: swarnendude
>
> Hi,
>
> I was using addText() method and for my condition, I am putting
> different styles on different strings in a single sentence. So, when I
> call addText() method for each individual phrase with different
> styles, it gives me the text in a new line. I want them all to be in a
> single line. Following is the example of code I am writing:
>
> *What I want:*
>
> Hello All!
>
> I am putting these two Strings in two different addText() method.
>
> $section->addText("Hello", array("color" => "red"));
>
> $section->addText("All!", array("color" => "green"));
>
> *And what I am getting:*
>
> Hello
>
> All!
>
> How to disable these new line settings in addText() method?
>
>
>
>
>
> Thanks,
>
> Swarnendu
>
>
>
> Read the full discussion online
> <http://phpword.codeplex.com/Thread/View.aspx?ThreadId=210852&ANCHOR#Post436515>.
>
> To add a post to this discussion, reply to this email
> ([email removed]
> <mailto:[email removed]?subject=%5Bphpword:210852%5D>)
>
> To start a new discussion for this project, email
> [email removed] <mailto:[email removed]>
>
> You are receiving this email because you subscribed to this discussion
> on CodePlex. You can unsubscribe or change your settings
> <https://phpword.codeplex.com/subscriptions/thread/project/edit> on
> codePlex.com.
>
> Please note: Images and attachments will be removed from emails. Any
> posts to this discussion will also be available online at codeplex.com
>
May 4, 2010 at 5:56 AM
Edited May 4, 2010 at 6:04 AM

I have the same problem...
For now you can do this...
In the file PHPWord/Writer/Word2007/Base.php replace the line $text = htmlspecialchars($textrun->getText()); for $text = $textrun->getText(); and now when you insert text and you want that some words appears in other format you must write de xml code for that. For example when I want write "Hello World" I did that in this way

 

$BOLD='</w:t></w:r><w:r w:rsidRPr="00997AA1"> <w:rPr><w:b/></w:rPr><w:t xml:space="preserve"> %s 
</w:t></w:r><w:r w:rsidRPr="00997AA1"> <w:rPr></w:rPr><w:t xml:space="preserve">';
$string=sprintf($BOLD,'World');
$section->addText('Hello ' .$string);

 

 

Is similar for color, italic or whatever that you want, only you must use the xml code that you needed. if you notice, First I closed the tags of the previous text (w:t and w:r), then I wrote in bold, and finally I re-opened the same tags.

This is not the best solution because has a lots of problem but for now is better than nothing...

Greetings... 

May 4, 2010 at 7:38 AM

If you've used text runs in PHPExcel, I'm looking to code up the same mechanism for PHPWord. Something like:

$objTextRun = $section->createTextRun('Frankie Says"');
$objTextRun->addText("Hello ", array("color" => "green"));
$objTextRun->addText("World", array("color" => "red"));
$objTextRun->addText('"');
$section->createTextRun($objTextRun);
May 4, 2010 at 1:28 PM
Edited May 4, 2010 at 1:29 PM

Mark, this is exactly the way I would prefer to integrate.

For the usage I would prefer a little edited way:

$objTextRun = $section->createTextRun('Frankie Says"');
$objTextRun->addText("Hello ", array("color" => "green"));
$objTextRun->addText("World", array("color" => "red"));
$objTextRun->addText('"');
$section->addText($objTextRun);

The addText function could take a TextRun object or like before a text and optionally a font style:

 

public function addText($text, $style = null) {
	if($text instanceof PHPWord_Section_TextRun) {
		$this->_elementCollection[] = $text;
	} else {
		$givenText = utf8_encode($text);
		$text = new PHPWord_Section_Text($givenText, $style);
		$this->_elementCollection[] = $text;
	}
        return $text;
}

greetings

raw

 

 

May 4, 2010 at 10:40 PM
On 04/05/2010 13:28, raw_venture_ad wrote:
>
> For the usage I would prefer a little edited way:
>
> The addText function could take a TextRun object or like before a text
> and optionally a font style:
>
>
Not a problem, I can work with that.
I'll treat this as my first piece of work for PHPWord.

---
Mark Baker
Jul 24, 2010 at 3:04 PM

Fixed in PHPWord 0.6.2 Beta. See current release.

Apr 14, 2012 at 11:14 PM

Hi r@raw_venture_ad,

I also have the same problem, Pls can u help me out to fix this in PHPWord 0.6.2 Beta and i am using the latest release.

Thanks in Advance,

 

-vathsan

Apr 15, 2012 at 12:05 AM

Hi,

Got my problem solved.. Thanks..

$textrun = $section->createTextRun();

$textrun->addText('I am bold', array('bold'=>true));

$textrun->addText('I am italic, array('italic'=>true));

$textrun->addText('I am colored, array('color'=>'AACC00'));

Thanks to the excellent PHPWord Documentation.

Jul 25, 2012 at 11:50 AM

Hi.

This problem appears when file is oppened with Open Office.

What could be done to solve it ?

 

Thanks.

Dec 19, 2013 at 2:20 PM
as I can insert a newline or linebreak in this texRun?

like that:

Fecha de nacimiento: 11/07/1982
DNI: 1 07988921D
Nacionalidad: Española
$objTextRun = $section->createTextRun('');
$objTextRun->addText(utf8_decode('Fecha de nacimiento: '), 'strongStyle', 'txt_Style');
$objTextRun->addText(utf8_decode($Fecha_Nacimiento), 'txtStyle', 'txt_Style');
// INSERT LINEBREAK HERE
$objTextRun->addText(utf8_decode('DNI: '.$Tipo_Documento.' '.$Documento), 'txtStyle', 'txt_Style');
// INSERT LINEBREAK HERE
$objTextRun->addText(utf8_decode('Nacionalidad: '.$Nacionalidad), 'txtStyle', 'txt_Style');
$section->addText($objTextRun);
$section->addTextBreak(2);
with PHPWord 0.6.2!
Oct 8, 2014 at 8:41 AM
My workaround is to use a template that has all the formats I need.

$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate('Template.docx');
$document->setValue('XXXX', 'Sun');
$document->save('doc.docx');