/** * Read meta.xml. * * @param \PhpOffice\PhpWord\PhpWord $phpWord * @return void * @todo Process property type */ public function read(PhpWord $phpWord) { $xmlReader = new XMLReader(); $xmlReader->getDomFromZip($this->docFile, $this->xmlFile); $docProps = $phpWord->getDocInfo(); $metaNode = $xmlReader->getElement('office:meta'); // Standard properties $properties = array('title' => 'dc:title', 'subject' => 'dc:subject', 'description' => 'dc:description', 'keywords' => 'meta:keyword', 'creator' => 'meta:initial-creator', 'lastModifiedBy' => 'dc:creator'); foreach ($properties as $property => $path) { $method = "set{$property}"; $propertyNode = $xmlReader->getElement($path, $metaNode); if ($propertyNode !== null && method_exists($docProps, $method)) { $docProps->{$method}($propertyNode->nodeValue); } } // Custom properties $propertyNodes = $xmlReader->getElements('meta:user-defined', $metaNode); foreach ($propertyNodes as $propertyNode) { $property = $xmlReader->getAttribute('meta:name', $propertyNode); // Set category, company, and manager property if (in_array($property, array('Category', 'Company', 'Manager'))) { $method = "set{$property}"; $docProps->{$method}($propertyNode->nodeValue); // Set other custom properties } else { $docProps->setCustomProperty($property, $propertyNode->nodeValue); } } }
/** * Read custom document properties. * * @param \PhpOffice\PhpWord\PhpWord $phpWord * @return void */ public function read(PhpWord $phpWord) { $xmlReader = new XMLReader(); $xmlReader->getDomFromZip($this->docFile, $this->xmlFile); $docProps = $phpWord->getDocInfo(); $nodes = $xmlReader->getElements('*'); if ($nodes->length > 0) { foreach ($nodes as $node) { $propertyName = $xmlReader->getAttribute('name', $node); $attributeNode = $xmlReader->getElement('*', $node); $attributeType = $attributeNode->nodeName; $attributeValue = $attributeNode->nodeValue; $attributeValue = DocInfo::convertProperty($attributeValue, $attributeType); $attributeType = DocInfo::convertPropertyType($attributeType); $docProps->setCustomProperty($propertyName, $attributeValue, $attributeType); } } }
/** * Read Paragraph Item * @param RichText $oShape * @param \DOMElement $oNodeParent */ protected function readParagraphItem(Paragraph $oParagraph, \DOMElement $oNodeParent) { if ($this->oXMLReader->elementExists('text:line-break', $oNodeParent)) { $oParagraph->createBreak(); } else { $oTextRun = $oParagraph->createTextRun(); if ($oNodeParent->hasAttribute('text:style-name')) { $keyStyle = $oNodeParent->getAttribute('text:style-name'); if (isset($this->arrayStyles[$keyStyle])) { $oTextRun->setFont($this->arrayStyles[$keyStyle]['font']); } } if ($oTextRunLink = $this->oXMLReader->getElement('text:a', $oNodeParent)) { $oTextRun->setText($oTextRunLink->nodeValue); if ($oTextRunLink->hasAttribute('xlink:href')) { $oTextRun->getHyperlink()->setUrl($oTextRunLink->getAttribute('xlink:href')); } } else { $oTextRun->setText($oNodeParent->nodeValue); } } }
protected function loadShapeRichText(XMLReader $document, \DOMElement $node, $baseFile) { // Core $oShape = $this->oPhpPresentation->getActiveSlide()->createRichTextShape(); $oShape->setParagraphs(array()); // Variables $fileRels = 'ppt/slides/_rels/' . $baseFile . '.rels'; $oElement = $document->getElement('p:spPr/a:xfrm', $node); if ($oElement && $oElement->hasAttribute('rot')) { $oShape->setRotation(CommonDrawing::angleToDegrees($oElement->getAttribute('rot'))); } $oElement = $document->getElement('p:spPr/a:xfrm/a:off', $node); if ($oElement) { if ($oElement->hasAttribute('x')) { $oShape->setOffsetX(CommonDrawing::emuToPixels($oElement->getAttribute('x'))); } if ($oElement->hasAttribute('y')) { $oShape->setOffsetY(CommonDrawing::emuToPixels($oElement->getAttribute('y'))); } } $oElement = $document->getElement('p:spPr/a:xfrm/a:ext', $node); if ($oElement) { if ($oElement->hasAttribute('cx')) { $oShape->setWidth(CommonDrawing::emuToPixels($oElement->getAttribute('cx'))); } if ($oElement->hasAttribute('cy')) { $oShape->setHeight(CommonDrawing::emuToPixels($oElement->getAttribute('cy'))); } } $arrayElements = $document->getElements('p:txBody/a:p', $node); foreach ($arrayElements as $oElement) { // Core $oParagraph = $oShape->createParagraph(); $oParagraph->setRichTextElements(array()); $oSubElement = $document->getElement('a:pPr', $oElement); if ($oSubElement) { if ($oSubElement->hasAttribute('algn')) { $oParagraph->getAlignment()->setHorizontal($oSubElement->getAttribute('algn')); } if ($oSubElement->hasAttribute('fontAlgn')) { $oParagraph->getAlignment()->setVertical($oSubElement->getAttribute('fontAlgn')); } if ($oSubElement->hasAttribute('marL')) { $oParagraph->getAlignment()->setMarginLeft(CommonDrawing::emuToPixels($oSubElement->getAttribute('marL'))); } if ($oSubElement->hasAttribute('marR')) { $oParagraph->getAlignment()->setMarginRight(CommonDrawing::emuToPixels($oSubElement->getAttribute('marR'))); } if ($oSubElement->hasAttribute('indent')) { $oParagraph->getAlignment()->setIndent(CommonDrawing::emuToPixels($oSubElement->getAttribute('indent'))); } if ($oSubElement->hasAttribute('lvl')) { $oParagraph->getAlignment()->setLevel($oSubElement->getAttribute('lvl')); } $oElementBuFont = $document->getElement('a:buFont', $oSubElement); $oParagraph->getBulletStyle()->setBulletType(Bullet::TYPE_NONE); if ($oElementBuFont) { if ($oElementBuFont->hasAttribute('typeface')) { $oParagraph->getBulletStyle()->setBulletFont($oElementBuFont->getAttribute('typeface')); } } $oElementBuChar = $document->getElement('a:buChar', $oSubElement); if ($oElementBuChar) { $oParagraph->getBulletStyle()->setBulletType(Bullet::TYPE_BULLET); if ($oElementBuChar->hasAttribute('char')) { $oParagraph->getBulletStyle()->setBulletChar($oElementBuChar->getAttribute('char')); } } /*$oElementBuAutoNum = $document->getElement('a:buAutoNum', $oSubElement); if ($oElementBuAutoNum) { $oParagraph->getBulletStyle()->setBulletType(Bullet::TYPE_NUMERIC); if ($oElementBuAutoNum->hasAttribute('type')) { $oParagraph->getBulletStyle()->setBulletNumericStyle($oElementBuAutoNum->getAttribute('type')); } if ($oElementBuAutoNum->hasAttribute('startAt') && $oElementBuAutoNum->getAttribute('startAt') != 1) { $oParagraph->getBulletStyle()->setBulletNumericStartAt($oElementBuAutoNum->getAttribute('startAt')); } }*/ } $arraySubElements = $document->getElements('(a:r|a:br)', $oElement); foreach ($arraySubElements as $oSubElement) { if ($oSubElement->tagName == 'a:br') { $oParagraph->createBreak(); } if ($oSubElement->tagName == 'a:r') { $oElementrPr = $document->getElement('a:rPr', $oSubElement); if (is_object($oElementrPr)) { $oText = $oParagraph->createTextRun(); if ($oElementrPr->hasAttribute('b')) { $oText->getFont()->setBold($oElementrPr->getAttribute('b') == 'true' ? true : false); } if ($oElementrPr->hasAttribute('i')) { $oText->getFont()->setItalic($oElementrPr->getAttribute('i') == 'true' ? true : false); } if ($oElementrPr->hasAttribute('strike')) { $oText->getFont()->setStrikethrough($oElementrPr->getAttribute('strike') == 'noStrike' ? false : true); } if ($oElementrPr->hasAttribute('sz')) { $oText->getFont()->setSize((int) ($oElementrPr->getAttribute('sz') / 100)); } if ($oElementrPr->hasAttribute('u')) { $oText->getFont()->setUnderline($oElementrPr->getAttribute('u')); } // Color $oElementSrgbClr = $document->getElement('a:solidFill/a:srgbClr', $oElementrPr); if (is_object($oElementSrgbClr) && $oElementSrgbClr->hasAttribute('val')) { $oColor = new Color(); $oColor->setRGB($oElementSrgbClr->getAttribute('val')); $oText->getFont()->setColor($oColor); } // Hyperlink $oElementHlinkClick = $document->getElement('a:hlinkClick', $oElementrPr); if (is_object($oElementHlinkClick)) { if ($oElementHlinkClick->hasAttribute('tooltip')) { $oText->getHyperlink()->setTooltip($oElementHlinkClick->getAttribute('tooltip')); } if ($oElementHlinkClick->hasAttribute('r:id') && isset($this->arrayRels[$fileRels][$oElementHlinkClick->getAttribute('r:id')])) { $oText->getHyperlink()->setUrl($this->arrayRels[$fileRels][$oElementHlinkClick->getAttribute('r:id')]); } } //} else { // $oText = $oParagraph->createText(); } $oSubSubElement = $document->getElement('a:t', $oSubElement); $oText->setText($oSubSubElement->nodeValue); } } } if (count($oShape->getParagraphs()) > 0) { $oShape->setActiveParagraph(0); } }
/** * Read style definition * * @param \PhpOffice\Common\XMLReader $xmlReader * @param \DOMElement $parentNode * @param array $styleDefs * @ignoreScrutinizerPatch * @return array */ protected function readStyleDefs(XMLReader $xmlReader, \DOMElement $parentNode = null, $styleDefs = array()) { $styles = array(); foreach ($styleDefs as $styleProp => $styleVal) { @(list($method, $element, $attribute, $expected) = $styleVal); if ($xmlReader->elementExists($element, $parentNode)) { $node = $xmlReader->getElement($element, $parentNode); // Use w:val as default if no attribute assigned $attribute = $attribute === null ? 'w:val' : $attribute; $attributeValue = $xmlReader->getAttribute($attribute, $node); $styleValue = $this->readStyleDef($method, $attributeValue, $expected); if ($styleValue !== null) { $styles[$styleProp] = $styleValue; } } } return $styles; }