首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHPWord LineBreak in TextRun不工作

PHPWord LineBreak in TextRun不工作
EN

Stack Overflow用户
提问于 2016-01-28 15:30:58
回答 1查看 2K关注 0票数 1

我从一位较老的Stack Over Flow Question那里找到了建议,我在让它开始工作时遇到了困难。我添加了他们描述的方法,但是生成的word文件没有换行符(在解压缩之后,通过视觉和检查docx文件)。没有错误,所以这个方法似乎是在做一些事情,而不是我想要的。我之所以这样做,是因为我需要\n字符,而且我使用PHPRtfText确实让它工作得很好,但在兼容性方面存在问题,而且它作为Word2007文件似乎工作得更好,因此我相应地转换了代码。不过,也许我把它放在了错误的位置,但下面是修改后的函数:

/lib/PHPWord/Writer/Word2007/Base.php:

代码语言:javascript
复制
protected function _writeTextRun(PHPWord_Shared_XMLWriter $objWriter = null,    PHPWord_Section_TextRun $textrun)
{
  $elements = $textrun->getElements();
  $styleParagraph = $textrun->getParagraphStyle();

  $SpIsObject = ($styleParagraph instanceof PHPWord_Style_Paragraph) ? true : false;

  $objWriter->startElement('w:p');

  if($SpIsObject)
  {
     $this->_writeParagraphStyle($objWriter, $styleParagraph);
  }
  elseif(!$SpIsObject && !is_null($styleParagraph))
  {
     $objWriter->startElement('w:pPr');
        $objWriter->startElement('w:pStyle');
           $objWriter->writeAttribute('w:val', $styleParagraph);
        $objWriter->endElement();
     $objWriter->endElement();
  }

  if(count($elements) > 0)
  {
     foreach($elements as $element)
     {
        if($element instanceof PHPWord_Section_Text)
        {
           $this->_writeText($objWriter, $element, true);
        }
        elseif($element instanceof PHPWord_Section_Link)
        {
           $this->_writeLink($objWriter, $element, true);
        }
     }
  }
  elseif($element instanceof PHPWord_Section_TextBreak)
  {
     $objWriter->writeElement('w:br');
  }

  $objWriter->endElement();
}

我是通过以下方式引用它的:

test1.php:

代码语言:javascript
复制
$PHPWord = new PHPWord();;
$PHPWord->setDefaultFontName('Calibri');
$PHPWord->setDefaultFontSize(11);

$section = $PHPWord->createSection();
$textrun = $section->createTextRun();
$textrun->addText( "---------------");
$textrun->addTextBreak();
$textrun->addText( "$name", array('bold'=>true ) );

最后在/lib/PHPWord/Section/TextRun.php:

代码语言:javascript
复制
public function addTextBreak($count = 1)
{
    for($i=1; $i<=$count; $i++)
    {
        $this->_elementCollection[] = new PHPWord_Section_TextBreak();
    }
}

还有什么需要我帮忙的吗?我真的需要它来工作,不能再把这个类改变成其他的东西。

我对文本进行了很大的格式化,因此需要文本,但是我需要在每一行之后使用\n,而不是一个部分,因为它需要相同的段落,所以使用\n\n是不能接受的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-02 18:59:27

我当然认为我试过了,但也许我并没有把它保存在网络服务器上,但是把它移到更高的位置

代码语言:javascript
复制
if(count($elements) > 0) 
  {
     foreach($elements as $element) 
     {
        if($element instanceof PHPWord_Section_Text) 
        {
           $this->_writeText($objWriter, $element, true);
            } 
        elseif($element instanceof PHPWord_Section_Link) 
        {
           $this->_writeLink($objWriter, $element, true);
            }
        elseif($element instanceof PHPWord_Section_TextBreak)
        {        
           $objWriter->writeElement('w:br');
        }              
     }
  } 
  $objWriter->endElement();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35065575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档