【问题标题】:PHPWord how to add text break / new line while in a text runPHPWord如何在文本运行时添加文本中断/换行
【发布时间】:2013-07-17 00:31:48
【问题描述】:

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

【问题讨论】:

    标签: php format line phpword


    【解决方案1】:

    试试这个:

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

    【讨论】:

      【解决方案2】:

      这个问题是 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 时,此解决方案也有效:$templateProcessor-&gt;setValue('contact_address', '1st Line&lt;w:br /&gt;2nd Line');
      【解决方案3】:

      你可以有一个文本并在你想要换行的地方添加\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);
      }
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        【解决方案5】:

        恐怕当前版本无法做到这一点。我对这个库没有深入的了解,但是通过查看代码发现textRun 类只包含addTextaddLink 方法。

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

        基本上可以通过修改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');
        }
        

        【讨论】:

        • 我对将代码放在 Base.php 中的确切位置有点困惑。在哪个大括号的末尾?最后有 2 个 if 测试。另外,在将此代码添加到库后如何添加中断?
        • 哦,没关系,我让它工作了。感谢您的帮助,您不知道您为我节省了多少时间!
        • @user2579723 很高兴它对您有所帮助!
        • 既然您似乎对此非常了解,您能否看看这个问题,看看您是否可以提供任何帮助stackoverflow.com/questions/17757477/phpword-xml-indent-a-line?谢谢!
        • @user2579723 抱歉回复晚了,您链接的问题现在已删除:(
        【解决方案6】:

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

        这将输出:

        某事

        后面

        【讨论】:

        • 这将创建两个段落,它们之间的间距设置为零。它不会创建一个包含换行符的段落。
        【解决方案7】:

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

        • 为段落创建TextRun
        • TextRun 通过调用TextRunaddTextBreak() 方法 本身

        这里有一个函数可以处理包含文字换行符的字符串中的完整文本段落(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 中打开时,上面的输出是正确的。

        【讨论】:

          【解决方案8】:

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

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

          【讨论】:

          • 这需要\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(false); 并自己对所有文本进行 XML 转义。
          【解决方案9】:

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

          【讨论】:

            猜你喜欢
            • 2016-02-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-05-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-08-22
            相关资源
            最近更新 更多