How can I add a text break or go to the next line/row while in a text run? I tried to just do $section->addTextBreak(2);
while in the text run but it just added the breaks to the section after the text run. I also tried $textrun->addTextBreak(2);
but it gave me a fatal error. Any responses would be greatly appreciated.
如何在文本运行时添加文本中断或进入下一行/行?我试着做$section->addTextBreak(2);虽然在文本中运行,但它只是在文本运行后添加了中断。我也试过$ textrun - > addTextBreak(2);但它给了我一个致命的错误。如有任何答复,将不胜感激。
5 个解决方案
#1
4
I'm afraid that this will not be possible with current version. I don't have deep understanding of this library, but from looking at the code, I found out that the textRun
class consist only of addText
and addLink
methods.
我担心现在的版本是不可能的。我对这个库没有很深的理解,但是从代码的角度来看,我发现textRun类只包含addText和addLink方法。
But I also need this feature along with several others, so I'm going to write it myself and create a pull request to get it included in the next release (if there will be any).
但是我也需要这个特性和其他一些特性,所以我将自己编写它,并创建一个pull请求,以将它包含在下一个版本中(如果有的话)。
Basically it can be done by modifying the textRun
class, adding an addLineBreak
method (similar way as it is in the section class) and then modify class Base.php
to create proper elements in final document.
基本上可以通过修改textRun类来完成,添加一个addLineBreak方法(类似于节类中的方法),然后修改类库。php在最终文档中创建适当的元素。
In Docx xml, those line brakes are similar to the html br
tag, but previous text must be closed and reopened after using break like this:
在Docx xml中,这些行刹车类似于html br标记,但是在使用了这样的break之后,前面的文本必须关闭并重新打开:
<w:r>
<w:t>This is</w:t>
<w:br/>
<w:t xml:space="preserve"> a simple sentence.</w:t>
</w:r>
instead of simply doing
而不是简单地做
<w:r>
<w:t>This is<w:br /> a simple sentence</w:t>
</w:r>
So in base.php
, you'll need to edit behavior to create this block of code.
所以在基地。php,您需要编辑行为来创建这段代码。
Hope this was useful!
希望这是有用的!
EDIT
编辑
I have figured out that implementing this is very simple. In textRun.php
just add this method:
我已经发现实现这一点非常简单。在textRun。php只是添加了这个方法:
/**
* 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 _writeTextRun
method at the end of this method add this condition:
在基地。在此方法末尾的_writeTextRun方法中,php添加了以下条件:
elseif($element instanceof PHPWord_Section_TextBreak) {
$objWriter->writeElement('w:br');
}
#2
5
The question was asked 3 years ago but I have the same problem and I found a solution. Maybe this can help new users of PHPWord.
这个问题是3年前提出的,但我有同样的问题,我找到了解决办法。也许这可以帮助新用户的PHPWord。
To add a crlf in Word document the tag can help :
在Word文档中添加crlf可以帮助:
$section->addText('Some text <w:br/> another text in the line ');
I found the solution here : http://jeroen.is/phpword-line-breaks/
我在这里找到了解决方案:http://jeroen.is/phpwordline -breaks/。
#3
1
Adding a newline in phpword has bothered me, and I finnaly found solution, by accident, so here it is: And this justifies the text.
在phpword中添加一个换行已经困扰了我,我找到了解决方案,这是偶然的,这就是:这证明了文本的正确性。
$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
的东西
behind
后面
#4
1
It's easy: just start a new textrun, and before it add textbreak to the section, like this (tested with docx format):
这很简单:只需启动一个新的textrun,然后在它将textbreak添加到section之前,就像这样(用docx格式进行测试):
$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');
#5
0
For correct work, you need to add a text block:
对于正确的工作,您需要添加一个文本块:
$string = strtr($string, [
"\n" => "</w:t>\n<w:br />\n<w:t xml:space=\"preserve\">"
]);
#1
4
I'm afraid that this will not be possible with current version. I don't have deep understanding of this library, but from looking at the code, I found out that the textRun
class consist only of addText
and addLink
methods.
我担心现在的版本是不可能的。我对这个库没有很深的理解,但是从代码的角度来看,我发现textRun类只包含addText和addLink方法。
But I also need this feature along with several others, so I'm going to write it myself and create a pull request to get it included in the next release (if there will be any).
但是我也需要这个特性和其他一些特性,所以我将自己编写它,并创建一个pull请求,以将它包含在下一个版本中(如果有的话)。
Basically it can be done by modifying the textRun
class, adding an addLineBreak
method (similar way as it is in the section class) and then modify class Base.php
to create proper elements in final document.
基本上可以通过修改textRun类来完成,添加一个addLineBreak方法(类似于节类中的方法),然后修改类库。php在最终文档中创建适当的元素。
In Docx xml, those line brakes are similar to the html br
tag, but previous text must be closed and reopened after using break like this:
在Docx xml中,这些行刹车类似于html br标记,但是在使用了这样的break之后,前面的文本必须关闭并重新打开:
<w:r>
<w:t>This is</w:t>
<w:br/>
<w:t xml:space="preserve"> a simple sentence.</w:t>
</w:r>
instead of simply doing
而不是简单地做
<w:r>
<w:t>This is<w:br /> a simple sentence</w:t>
</w:r>
So in base.php
, you'll need to edit behavior to create this block of code.
所以在基地。php,您需要编辑行为来创建这段代码。
Hope this was useful!
希望这是有用的!
EDIT
编辑
I have figured out that implementing this is very simple. In textRun.php
just add this method:
我已经发现实现这一点非常简单。在textRun。php只是添加了这个方法:
/**
* 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 _writeTextRun
method at the end of this method add this condition:
在基地。在此方法末尾的_writeTextRun方法中,php添加了以下条件:
elseif($element instanceof PHPWord_Section_TextBreak) {
$objWriter->writeElement('w:br');
}
#2
5
The question was asked 3 years ago but I have the same problem and I found a solution. Maybe this can help new users of PHPWord.
这个问题是3年前提出的,但我有同样的问题,我找到了解决办法。也许这可以帮助新用户的PHPWord。
To add a crlf in Word document the tag can help :
在Word文档中添加crlf可以帮助:
$section->addText('Some text <w:br/> another text in the line ');
I found the solution here : http://jeroen.is/phpword-line-breaks/
我在这里找到了解决方案:http://jeroen.is/phpwordline -breaks/。
#3
1
Adding a newline in phpword has bothered me, and I finnaly found solution, by accident, so here it is: And this justifies the text.
在phpword中添加一个换行已经困扰了我,我找到了解决方案,这是偶然的,这就是:这证明了文本的正确性。
$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
的东西
behind
后面
#4
1
It's easy: just start a new textrun, and before it add textbreak to the section, like this (tested with docx format):
这很简单:只需启动一个新的textrun,然后在它将textbreak添加到section之前,就像这样(用docx格式进行测试):
$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');
#5
0
For correct work, you need to add a text block:
对于正确的工作,您需要添加一个文本块:
$string = strtr($string, [
"\n" => "</w:t>\n<w:br />\n<w:t xml:space=\"preserve\">"
]);