PHPWord如何在文本运行中添加文本中断/新行 [英] PHPWord how to add text break / new line while in a text run

查看:1966
本文介绍了PHPWord如何在文本运行中添加文本中断/新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在文本运行中添加文本中断或转到下一行/行?我尝试在文本运行时只是执行 $ section-> addTextBreak(2); ,但它只是在文本运行之后将断点添加到节中。我也试过 $ textrun-> addTextBreak(2); 但是它给了我一个致命的错误。任何反应将不胜感激。

解决方案

恐怕这是不可能与当前版本。我对这个库没有深入的了解,但是通过查看代码,我发现 textRun 类只包含 addText addLink 方法。

但是我也需要这个功能以及其他几个,所以我我会自己写,并创建一个拉请求,让它包含在下一个版本(如果有的话)。

基本上可以通过修改在 textRun 类中添加一个 addLineBreak 方法(类似于section类中的方法),然后修改class Base.php 在最终文档中创建合适的元素。

在Docx xml中,这些行制动器与html类似 br 标记,但之前的文本必须在使用中断之后关闭并重新打开: C $ C>< W:R>
< w:t>这是< / w:t>
< w:br />
< w:t xml:space =preserve>简单的句子。< / w:t>
< / w:r>

而不是简单的做

pre > < W:R>
< w:t>这是< w:br />一个简单的句子< / w:t>
< / w:r>





c> base.php ,您需要编辑行为来创建这个代码块。



希望这有用!



编辑



我发现实现这个非常简单。在 textRun.php 中加入这个方法:

  / ** 
*添加一个TextBreak元素
*
* @param int $ count
* /
public function addTextBreak($ count = 1){
for($ i = 1; $ i <= $ count; $ i ++){
$ this-> _elementCollection [] = new PHPWord_Section_TextBreak();




$ b $ Base.php 在这个方法结尾的 _writeTextRun 方法中添加这个条件:

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


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.

解决方案

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.

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).

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.

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:

<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.

Hope this was useful!

EDIT

I have figured out 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 _writeTextRun method at the end of this method add this condition:

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

这篇关于PHPWord如何在文本运行中添加文本中断/新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆