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

问题描述 投票:5回答:8

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

php format line phpword
8个回答
6
投票

我担心目前的版本无法做到这一点。我对这个库没有深刻的理解,但是通过查看代码,我发现textRun类只包含addTextaddLink方法。

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

基本上它可以通过修改textRun类,添加addLineBreak方法(类似于在类类中的方式)然后修改类Base.php以在最终文档中创建适当的元素来完成。

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

而不是简单地做

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

因此,在base.php中,您需要编辑行为以创建此代码块。

希望这很有用!

编辑

我已经发现实现这一点非常简单。在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();
    }
}

并在Base.php中的_writeTextRun方法结束时添加此条件:

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

8
投票

问题在3年前被问到,但我遇到了同样的问题,我找到了解决方案。也许这可以帮助PHPWord的新用户。

要在Word文档中添加crlf,标记可以提供帮助:

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

我在这里找到了解决方案:http://jeroen.is/phpword-line-breaks/


1
投票

在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');

这将输出:

某物

背后


1
投票

我不知道接受的答案中的PR是否已被合并,或者事实上它在2013年是否已经可行,但无论如何,自2015年以来,如果不是更早,在initial response to #553 on GitHub中指出在段落中插入换行符的正确方法:

  • 为段落创建一个TextRun;
  • 通过调用TextRun本身的addTextBreak()方法向TextRun添加换行符。

这是一个函数,它将处理包含文字换行符(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)当前在读取Word2007文档时丢弃换行符 - 希望在1.0之前修复 - 但是在Word中打开时,上面的输出是正确的。


0
投票

这很简单:只需启动一个新的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');

0
投票

为了正确工作,您需要添加一个文本块:

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

0
投票

你可以有一个文本并添加\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);
}

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.