【问题标题】:Suppress Blank Space between two line of Text in PHPWord抑制PHPWord中两行文本之间的空格
【发布时间】:2015-10-16 11:22:41
【问题描述】:

我正在尝试使用 PHPWord 创建 word 文件。我已经设置了一些字体样式和段落样式。即使没有给出文本分隔符,我也会在 line1 和 line2 之间得到 6 行 空格。有什么建议吗?

这是我的代码

    $this->word->addFontStyle('titleStyle', array('bold' => true, 'size' => 16, 'name'=>'Calibri'));
    $this->word->addFontStyle('lineStyle', array('align'=>'left','size' => 10, 'name'=>'Calibri'));
    $this->word->addParagraphStyle('pStyle', array('align' => 'center', 'spaceBefore' => 0, 'spaceAfter' => 0, 'spacing' => 0));

    $section = $this->word->createSection(array('breakType'=>'continuous'));

    // Simple text
    $section->addText('Text Document','titleStyle','pStyle');

    $section->addText('Line1 : ','lineStyle');      
    $section->addText('Line2 : ','lineStyle');      
    $section->addText('Line3 : ','lineStyle');          
    $section->addText('Line4 : ','lineStyle');          


    $filename='NewDoc.docx'; //save our document as this file name
    header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); //mime type
    header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
    header('Cache-Control: max-age=0'); //no cache

    $objWriter = PHPWord_IOFactory::createWriter($this->word, 'Word2007');
    $objWriter->save('php://output');

任何帮助将不胜感激。

下面是我正在生成的输出文件。

【问题讨论】:

    标签: codeigniter phpword


    【解决方案1】:

    您定义的段落样式不会应用于任何添加的文本。

    如果要将定义的段落样式应用于添加的文本,则需要将名称(或定义)添加为参数:

    $section->addText('Line 1 : '.$line1,'lineStyle', 'pStyle');
    

    或者,如果您更喜欢创建将在任何地方自动使用的全局段落样式:

    $phpWord->setDefaultParagraphStyle(array('align'=>'left','size' => 10, 'name'=>'Calibri'));
    

    【讨论】:

    • 您问题中的那段代码(当用一些常量字符串替换 $line1 等时)没有为我创建任何额外的空格或行。您的变量中是否有可能导致额外行的内容?
    • 好吧,现在我已经删除了所有变量并使用简单的常量字符串,就像你尝试过的那样。但我仍然遇到同样的问题。我已经更新了我的代码。可能是我使用的 PHPWord 版本与您不同。我从 gitHUb 下载了一个。
    • 是的,我使用的不是最新的 0.12.0 - 所以如果您使用的是 0.13.0 版本,可能会有一些差异......
    • 对不起,我的错。我没有使用 github,而是使用 codeplex PHPWord。 phpword.codeplex.com
    • 有什么方法可以让我获得您当前使用的源的链接。(GitHub)
    猜你喜欢
    • 2010-09-25
    • 1970-01-01
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多