PHPWord how to add text break / new line during text run

How to add a text gap or go to the next line / line during the text run? I tried just doing $section->addTextBreak(2); while in the text, but he just added breaks to the section after starting the text. I also tried $textrun->addTextBreak(2); but it gave me a fatal error. Any answers would be greatly appreciated.

+4
source share
5 answers

I am afraid that with the current version this will be impossible. I do not have a deep understanding of this library, but looking at the code, I found that the textRun class consists only of the addText and addLink .

But I also need this function, along with several others, so I'm going to write it myself and create a transfer request to include it in the next version (if any).

Basically, you can do this by changing the textRun class, adding the addLineBreak method (similar to the way in the section class), and then changing the Base.php class to create the corresponding elements in the final document.

In Docx xml, these linear brakes are similar to the html br tag, but the previous text should be closed and reopened after using break as follows:

 <w:r> <w:t>This is</w:t> <w:br/> <w:t xml:space="preserve"> a simple sentence.</w:t> </w:r> 

instead of just doing

 <w:r> <w:t>This is<w:br /> a simple sentence</w:t> </w:r> 

So in Base.php you need to change the behavior to create this block of code.

Hope this was helpful!

EDIT

I realized that implementing this is very simple. In textRun.php just add this method:

 /** * Add a TextBreak Element * * @param int $count */ public function addTextBreak($count = 1) { for($i=1; $i<=$count; $i++) { $this->_elementCollection[] = new PHPWord_Section_TextBreak(); } } 

and in Base.php in the Base.php method at the end of this method add this condition:

 elseif($element instanceof PHPWord_Section_TextBreak) { $objWriter->writeElement('w:br'); } 
+4
source

The question was asked 3 years ago, but I have the same problem, and I found a solution. Perhaps this may help new PHPWord users.

To add crlf to a Word document, a tag can help:

 $section->addText('Some text <w:br/> another text in the line '); 

I found a solution here: http://jeroen.is/phpword-line-breaks/

+5
source

Adding a new line to phpword bothered me, and I finnally found a solution by accident, so here it is: And it justifies the text.

 $PHPWord->addParagraphStyle('pJustify', array('align' => 'both', 'spaceBefore' => 0, 'spaceAfter' => 0, 'spacing' => 0)); //add this style then append it to text below $section->addText('something', 'textstyle', 'pJustify'); //the text behind this will be justified and will be in a new line, not in a new paragraph $section->addText('behind', 'textstyle', 'pJustify'); 

This will output:

something

per

+1
source

It's easy: just run the new textrun and before that add a textbreak to the section like this (tested with docx format):

 $textrun = $section->addTextRun(); $textrun->addText('blah-blah', 'p_bold'); $section->addTextBreak(2); $textrun = $section->addTextRun(); $textrun->addText('blah-blah-blah in new line ', 'p'); $textrun->addText('blah-blah', 'p_bold'); $textrun->addText('blah-blah', 'p'); $section->addTextBreak(2); $textrun = $section->addTextRun(); $textrun->addText('blahblah', 'p'); 
+1
source

For proper operation you need to add a text block:

 $string = strtr($string, [ "\n" => "</w:t>\n<w:br />\n<w:t xml:space=\"preserve\">" ]); 
0
source

Source: https://habr.com/ru/post/1491748/


All Articles