简体   繁体   English

PHPWord如何在文本运行中添加文本中断/换行

[英]PHPWord how to add text break / new line while in a text run

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);我试着只做$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);我也试过$textrun->addTextBreak(2); but it gave me a fatal error.但它给了我一个致命的错误。 Any responses would be greatly appreciated.任何答复将不胜感激。

The question was asked 3 years ago but I have the same problem and I found a solution.这个问题是 3 年前提出的,但我遇到了同样的问题,我找到了解决方案。 Maybe this can help new users of PHPWord.也许这可以帮助 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/phpword-line-breaks/

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类只包含addTextaddLink方法。

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.基本上可以通过修改textRun类,添加addLineBreak方法(与节类中的方法类似),然后修改类Base.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 中,那些 line 刹车类似于 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.因此,在base.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:并在Base.php_writeTextRun方法的末尾添加此条件:

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

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在后面

You can have a text and add \\n where ever you want to have line breaks, and do the rest like this:你可以有一个文本并在你想要换行的地方添加\\n ,然后像这样完成其余的工作:

$text = "foo\nbar\nfoobar";
$textlines = explode("\n", $text);

$textrun = $section->addTextRun();
$textrun->addText(array_shift($textlines));

foreach($textlines as $line) {
    $textrun->addTextBreak();
    // maybe twice if you want to seperate the text
    // $textrun->addTextBreak(2);
    $textrun->addText($line);
}

It's easy: just start a new textrun, and before it add textbreak to the section, like this (tested with docx format):这很简单:只需启动一个新的 textrun,然后在将 textbreak 添加到该部分之前,就像这样(使用 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');

I don't know if a PR from the accepted answer was incorporated or it was in fact already possible in 2013, but regardless, since 2015 if not earlier, the correct way to insert line breaks within a paragraph is indicated in the initial response to #553 on GitHub :我不知道是否合并了已接受答案中的 PR 或者它实际上在 2013 年已经可能,但无论如何,自 2015 年以来,如果不是更早,在段落中插入换行符的正确方法在对#553 在 GitHub 上

  • Create a TextRun for the paragraph;为段落创建一个TextRun
  • Add line breaks to the TextRun by calling the addTextBreak() method of the TextRun itself .添加换行符到TextRun通过调用addTextBreak()的方法TextRun本身

Here's a function that will take care of this for a complete paragraph of text in a string containing literal line-breaks (CR-LF [ "\\r\\n" ], LF [ "\\n" ] or CR [ "\\r" ]):这是一个函数,它将处理包含文字换行符的字符串中的完整文本段落(CR-LF [ "\\r\\n" ], LF [ "\\n" ] 或 CR [ "\\r" ]):

/**
 * @param \PhpOffice\PhpWord\Element\AbstractContainer $container
 *        E.g. a section or table cell.
 * @param string $text String with literal line breaks as CR-LF, LF or CR.
 * @param string|array|\PhpOffice\PhpWord\Style\Paragraph $paragraphStyle
 * @param string|array|\PhpOffice\PhpWord\Style\Font $fontStyle
 *
 * @return \PhpOffice\PhpWord\Element\TextRun
 */
function addTextWithLineBreaks(
  \PhpOffice\PhpWord\Element\AbstractContainer $container,
  $text,
  $fontStyle,
  $paragraphStyle
) {
  $textRun = $container->addTextRun($paragraphStyle);
  foreach (preg_split('/(\\r\\n?+|\\n)/',
    $text,
    -1,
    PREG_SPLIT_DELIM_CAPTURE
  ) as $i => $part) {
    if ($i & 1) {
      $textRun->addTextBreak(1, $fontStyle, $paragraphStyle);
    } else {
      $textRun->addText($part, $fontStyle, $paragraphStyle);
    }
  }
  return $textRun;
}

PHPWord (0.14.0) currently discards line breaks when reading Word2007 documents – hopefully that will be fixed before 1.0 – but the output from the above is correct when opened in Word. PHPWord (0.14.0) 当前在读取 Word2007 文档时会丢弃换行符——希望这会在 1.0 之前修复——但在 Word 中打开时,上述输出是正确的。

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\">"
]);

使用 PHP_EOL 换行它是 PHPWord 中换行的唯一解决方案

Try this one:试试这个:

    str_replace("\n", '</w:t><w:br/><w:t xml:space="preserve">', $yourData );

or this one或者这个

    str_replace("\r\n", '</w:t><w:br/><w:t xml:space="preserve">', $yourData );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM