PhpPresentation类改写支持设置行高

默认PhpPresentation类无法设置行高,没办法有需求只能自己参考国外资料修改,方法如下:

找到\PhpOffice\PhpPresentation\Shape\RichText\Paragraph.php文件,给类Paragraph添加如下属性:

/**
 * @var integer 100000 = 100%的行高
 */
private $lineSpacing = 100000;

给类Paragraph添加如下方法:

/**
 * @return int
 */
public function getLineSpacing()
{
    return $this->lineSpacing;
}

/**
 * @param int $lineSpacing
 * @return Paragraph
 */
public function setLineSpacing($lineSpacing)
{
    $this->lineSpacing = $lineSpacing;
    return $this;
}

找到PhpOffice\PhpPresentation\Writer\PowerPoint2007\Slide.php,给类Slide类的方法 private function writeParagraphs(XMLWriter $objWriter, $paragraphs)

// a:pPr
$objWriter->startElement('a:pPr');
$objWriter->writeAttribute('algn', $paragraph->getAlignment()->getHorizontal());
$objWriter->writeAttribute('fontAlgn', $paragraph->getAlignment()->getVertical());
$objWriter->writeAttribute('marL', CommonDrawing::pixelsToEmu($paragraph->getAlignment()->getMarginLeft()));
$objWriter->writeAttribute('marR', CommonDrawing::pixelsToEmu($paragraph->getAlignment()->getMarginRight()));
$objWriter->writeAttribute('indent', CommonDrawing::pixelsToEmu($paragraph->getAlignment()->getIndent()));
$objWriter->writeAttribute('lvl', $paragraph->getAlignment()->getLevel());
//添加如下代码
$objWriter->startElement('a:lnSpc');
$objWriter->startElement('a:spcPct');
$objWriter->writeAttribute('val', $paragraph->getLineSpacing());
$objWriter->endElement();
$objWriter->endElement();

111.png

$slide = $objPHPPowerPoint->createSlide();
$shape = $slide->createRichTextShape();
$shape->setHeight(600);
$shape->setWidth(550);
$shape->setOffsetX(86);
$shape->setOffsetY(178);
$shape->getActiveParagraph()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT)->setMarginLeft(0);
//设置行高
$shape->getActiveParagraph()->setLineSpacing(100000);
$textRun = $shape->createTextRun('Media Name: ');
$textRun->getFont()->setName("Locator Regular");
$textRun->getFont()->setSize(18);
$textRun->getFont()->setColor( new Color('FF000000') );

参考资料:http://officeopenxml.com/anatomyofOOXML-pptx.php

http://officeopenxml.com/drwSp-text-paraProps-margins.php

转载请注明本文固定链接
喜欢 ()or分享