【发布时间】:2013-07-17 00:31:48
【问题描述】:
如何在文本运行时添加文本中断或转到下一行/行?我试图在文本运行时只做$section->addTextBreak(2);,但它只是在文本运行后将中断添加到该部分。我也试过$textrun->addTextBreak(2);,但它给了我一个致命的错误。任何回复将不胜感激。
【问题讨论】:
如何在文本运行时添加文本中断或转到下一行/行?我试图在文本运行时只做$section->addTextBreak(2);,但它只是在文本运行后将中断添加到该部分。我也试过$textrun->addTextBreak(2);,但它给了我一个致命的错误。任何回复将不胜感激。
【问题讨论】:
试试这个:
str_replace("\n", '</w:t><w:br/><w:t xml:space="preserve">', $yourData );
或者这个
str_replace("\r\n", '</w:t><w:br/><w:t xml:space="preserve">', $yourData );
【讨论】:
这个问题是 3 年前提出的,但我遇到了同样的问题,我找到了解决方案。也许这可以帮助 PHPWord 的新用户。
要在 Word 文档中添加 crlf,该标签可以提供帮助:
$section->addText('Some text <w:br/> another text in the line ');
我在这里找到了解决方案:http://jeroen.is/phpword-line-breaks/
【讨论】:
\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(false); 并自己对所有文本进行 XML 转义。
$templateProcessor->setValue('contact_address', '1st Line<w:br />2nd Line');
你可以有一个文本并在你想要换行的地方添加\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);
}
【讨论】:
这很简单:只需启动一个新的 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');
【讨论】:
恐怕当前版本无法做到这一点。我对这个库没有深入的了解,但是通过查看代码发现textRun 类只包含addText 和addLink 方法。
但我还需要这个功能以及其他几个功能,所以我将自己编写它并创建一个拉取请求以将其包含在下一个版本中(如果有的话)。
基本上可以通过修改textRun 类,添加addLineBreak 方法(与section 类中的方法类似)然后修改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');
}
【讨论】:
在 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');
这将输出:
某事
后面
【讨论】:
我不知道是否包含来自已接受答案的 PR,或者它实际上在 2013 年就已经可能,但无论如何,自 2015 年以来,如果不是更早,在段落中插入换行符的正确方法在initial response to #553 on GitHub:
TextRun;TextRun 通过调用TextRun 的addTextBreak() 方法
本身。这里有一个函数可以处理包含文字换行符的字符串中的完整文本段落(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 中打开时,上面的输出是正确的。
【讨论】:
为了正确工作,您需要添加一个文本块:
$string = strtr($string, [
"\n" => "</w:t>\n<w:br />\n<w:t xml:space=\"preserve\">"
]);
【讨论】:
\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(false); 并自己对所有文本进行 XML 转义。
使用 PHP_EOL 进行换行,这是 PHPWord 中唯一的换行解决方案
【讨论】: