/** * Set Paragraph style * * @param string|array|\PhpOffice\PhpWord\Style\Paragraph $style * @return string|\PhpOffice\PhpWord\Style\Paragraph */ public function setParagraphStyle($style = null) { if (is_array($style)) { $this->paragraphStyle = new Paragraph(); $this->paragraphStyle->setArrayStyle($style); } elseif ($style instanceof Paragraph) { $this->paragraphStyle = $style; } else { $this->paragraphStyle = $style; } return $this->paragraphStyle; }
/** * Create new font style * * @param string $type Type of font * @param array $paragraphStyle Paragraph styles definition */ public function __construct($type = 'text', $paragraphStyle = null) { $this->type = $type; if ($paragraphStyle instanceof Paragraph) { $this->paragraphStyle = $paragraphStyle; } elseif (is_array($paragraphStyle)) { $this->paragraphStyle = new Paragraph(); $this->paragraphStyle->setArrayStyle($paragraphStyle); } else { $this->paragraphStyle = $paragraphStyle; } }
/** * Test line height exception by using nonnumeric value * * @expectedException \PhpOffice\PhpWord\Exception\InvalidStyleException */ public function testLineHeightException() { $object = new Paragraph(); $object->setLineHeight('a'); }
/** * Write automatic styles. * * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter * @return void */ private function writeTextStyles(XMLWriter $xmlWriter) { $styles = Style::getStyles(); $paragraphStyleCount = 0; if (count($styles) > 0) { foreach ($styles as $style) { if ($style->isAuto() === true) { $styleClass = str_replace('\\Style\\', '\\Writer\\ODText\\Style\\', get_class($style)); if (class_exists($styleClass)) { /** @var \PhpOffice\PhpWord\Writer\ODText\Style\AbstractStyle $styleWriter Type hint */ $styleWriter = new $styleClass($xmlWriter, $style); $styleWriter->write(); } if ($style instanceof Paragraph) { $paragraphStyleCount++; } } } if ($paragraphStyleCount == 0) { $style = new Paragraph(); $style->setStyleName('P1'); $style->setAuto(); $styleWriter = new ParagraphStyleWriter($xmlWriter, $style); $styleWriter->write(); } } }
/** * Write paragraph style * * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter * @param string $styleName * @param \PhpOffice\PhpWord\Style\Paragraph $style */ private function writeParagraphStyle(XMLWriter $xmlWriter, $styleName, ParagraphStyle $style) { $xmlWriter->startElement('w:style'); $xmlWriter->writeAttribute('w:type', 'paragraph'); $xmlWriter->writeAttribute('w:customStyle', '1'); $xmlWriter->writeAttribute('w:styleId', $styleName); $xmlWriter->startElement('w:name'); $xmlWriter->writeAttribute('w:val', $styleName); $xmlWriter->endElement(); // Parent style $basedOn = $style->getBasedOn(); $xmlWriter->writeElementIf(!is_null($basedOn), 'w:basedOn', 'w:val', $basedOn); // Next paragraph style $next = $style->getNext(); $xmlWriter->writeElementIf(!is_null($next), 'w:next', 'w:val', $next); // w:pPr $styleWriter = new ParagraphStyleWriter($xmlWriter, $style); $styleWriter->write(); $xmlWriter->endElement(); }
/** * Set style values and put it to static style collection * * @param string $styleName * @param Paragraph|Font|Table|Numbering $styleObject * @param array|null $styleValues */ private static function setStyleValues($styleName, $styleObject, $styleValues = null) { if (!array_key_exists($styleName, self::$styles)) { if (!is_null($styleValues) && is_array($styleValues)) { foreach ($styleValues as $key => $value) { $styleObject->setStyleValue($key, $value); } } $styleObject->setStyleName($styleName); $styleObject->setIndex(self::countStyles() + 1); // One based index self::$styles[$styleName] = $styleObject; } }
/** * Write automatic styles */ private function writeAutomaticStyles(XMLWriter $xmlWriter, PhpWord $phpWord) { $xmlWriter->startElement('office:automatic-styles'); // Font and paragraph $styles = Style::getStyles(); $paragraphStyleCount = 0; if (count($styles) > 0) { foreach ($styles as $styleName => $style) { if (preg_match('#^T[0-9]+$#', $styleName) != 0 || preg_match('#^P[0-9]+$#', $styleName) != 0) { $styleClass = str_replace('Style', 'Writer\\ODText\\Style', get_class($style)); if (class_exists($styleClass)) { $styleWriter = new $styleClass($xmlWriter, $style); $styleWriter->setIsAuto(true); $styleWriter->write(); } if ($style instanceof Paragraph) { $paragraphStyleCount++; } } } if ($paragraphStyleCount == 0) { $style = new Paragraph(); $style->setStyleName('P1'); $styleWriter = new \PhpOffice\PhpWord\Writer\ODText\Style\Paragraph($xmlWriter, $style); $styleWriter->setIsAuto(true); $styleWriter->write(); } } // Images $images = Media::getElements('section'); foreach ($images as $image) { if ($image['type'] == 'image') { $xmlWriter->startElement('style:style'); $xmlWriter->writeAttribute('style:name', 'fr' . $image['rID']); $xmlWriter->writeAttribute('style:family', 'graphic'); $xmlWriter->writeAttribute('style:parent-style-name', 'Graphics'); $xmlWriter->startElement('style:graphic-properties'); $xmlWriter->writeAttribute('style:vertical-pos', 'top'); $xmlWriter->writeAttribute('style:vertical-rel', 'baseline'); $xmlWriter->endElement(); $xmlWriter->endElement(); } } // Tables $sections = $phpWord->getSections(); $sectionCount = count($sections); if ($sectionCount > 0) { $sectionId = 0; foreach ($sections as $section) { $sectionId++; $elements = $section->getElements(); foreach ($elements as $element) { if ($elements instanceof Table) { $xmlWriter->startElement('style:style'); $xmlWriter->writeAttribute('style:name', $element->getElementId()); $xmlWriter->writeAttribute('style:family', 'table'); $xmlWriter->startElement('style:table-properties'); //$xmlWriter->writeAttribute('style:width', 'table'); $xmlWriter->writeAttribute('style:rel-width', 100); $xmlWriter->writeAttribute('table:align', 'center'); $xmlWriter->endElement(); $xmlWriter->endElement(); } } } } $xmlWriter->endElement(); // office:automatic-styles }