diff --git a/Classes/PHPWord/Section/Settings.php b/Classes/PHPWord/Section/Settings.php index 020a82e374..fce8d7b1c8 100755 --- a/Classes/PHPWord/Section/Settings.php +++ b/Classes/PHPWord/Section/Settings.php @@ -158,6 +158,16 @@ class PHPWord_Section_Settings */ private $pageNumberingStart; + /** + * @var int + */ + private $headerHeight; + + /** + * @var int + */ + private $footerHeight; + /** * Create new Section Settings */ @@ -178,6 +188,8 @@ public function __construct() $this->_borderRightColor = null; $this->_borderBottomSize = null; $this->_borderBottomColor = null; + $this->headerHeight = 720; // set default header and footer to 720 twips (.5 inches) + $this->footerHeight = 720; } /** @@ -568,4 +580,42 @@ public function getPageNumberingStart() { return $this->pageNumberingStart; } + + /** + * Get Header Height + * + * @return int + */ + public function getHeaderHeight() { + return $this->headerHeight; + } + + /** + * Set Header Height + * + * @param int $pValue + */ + public function setHeaderHeight($pValue = '') { + $this->headerHeight = $pValue; + return $this; + } + + /** + * Get Footer Height + * + * @return int + */ + public function getFooterHeight() { + return $this->footerHeight; + } + + /** + * Set Footer Height + * + * @param int $pValue + */ + public function setFooterHeight($pValue = '') { + $this->footerHeight = $pValue; + return $this; + } } diff --git a/Classes/PHPWord/Section/TextRun.php b/Classes/PHPWord/Section/TextRun.php index a9104341d3..44473e63df 100755 --- a/Classes/PHPWord/Section/TextRun.php +++ b/Classes/PHPWord/Section/TextRun.php @@ -109,6 +109,27 @@ public function addLink($linkSrc, $linkName = null, $styleFont = null) return $link; } + /** + * Add a Image Element + * + * @param string $imageSrc + * @param mixed $styleFont + * @return PHPWord_Section_Image + */ + public function addImage($imageSrc, $style = null) { + $image = new PHPWord_Section_Image($imageSrc, $style); + + if (!is_null($image->getSource())) { + $rID = PHPWord_Media::addSectionMediaElement($imageSrc, 'image'); + $image->setRelationId($rID); + + $this->_elementCollection[] = $image; + return $image; + } else { + trigger_error('Source does not exist or unsupported image type.'); + } + } + /** * Get TextRun content * diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index 3d46444979..a90126d990 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -106,6 +106,8 @@ protected function _writeTextRun(PHPWord_Shared_XMLWriter $objWriter = null, PHP $this->_writeText($objWriter, $element, true); } elseif ($element instanceof PHPWord_Section_Link) { $this->_writeLink($objWriter, $element, true); + } elseif ($element instanceof PHPWord_Section_Image) { + $this->_writeImage($objWriter, $element, true); } } } @@ -627,7 +629,7 @@ protected function _writeCellStyle(PHPWord_Shared_XMLWriter $objWriter = null, P * @param \PHPWord_Shared_XMLWriter $objWriter * @param \PHPWord_Section_Image $image */ - protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Image $image) + protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Image $image, $withoutP = false) { $rId = $image->getRelationId(); @@ -639,14 +641,16 @@ protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo $marginLeft = $style->getMarginLeft(); $wrappingStyle = $style->getWrappingStyle(); - $objWriter->startElement('w:p'); + if (!$withoutP) { + $objWriter->startElement('w:p'); - if (!is_null($align)) { - $objWriter->startElement('w:pPr'); - $objWriter->startElement('w:jc'); - $objWriter->writeAttribute('w:val', $align); - $objWriter->endElement(); - $objWriter->endElement(); + if (!is_null($align)) { + $objWriter->startElement('w:pPr'); + $objWriter->startElement('w:jc'); + $objWriter->writeAttribute('w:val', $align); + $objWriter->endElement(); + $objWriter->endElement(); + } } $objWriter->startElement('w:r'); @@ -697,7 +701,9 @@ protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo $objWriter->endElement(); - $objWriter->endElement(); + if (!$withoutP) { + $objWriter->endElement(); // w:p + } } protected function _writeWatermark(PHPWord_Shared_XMLWriter $objWriter = null, $image) diff --git a/Classes/PHPWord/Writer/Word2007/Document.php b/Classes/PHPWord/Writer/Word2007/Document.php index 41718f6e64..8722e53ba8 100755 --- a/Classes/PHPWord/Writer/Word2007/Document.php +++ b/Classes/PHPWord/Writer/Word2007/Document.php @@ -135,6 +135,9 @@ private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PH $marginRight = $settings->getMarginRight(); $marginBottom = $settings->getMarginBottom(); + $headerHeight = $settings->getHeaderHeight(); + $footerHeight = $settings->getFooterHeight(); + $borders = $settings->getBorderSize(); $objWriter->startElement('w:sectPr'); @@ -175,8 +178,8 @@ private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PH $objWriter->writeAttribute('w:right', $marginRight); $objWriter->writeAttribute('w:bottom', $marginBottom); $objWriter->writeAttribute('w:left', $marginLeft); - $objWriter->writeAttribute('w:header', '720'); - $objWriter->writeAttribute('w:footer', '720'); + $objWriter->writeAttribute('w:header', $headerHeight); + $objWriter->writeAttribute('w:footer', $footerHeight); $objWriter->writeAttribute('w:gutter', '0'); $objWriter->endElement(); diff --git a/changelog.txt b/changelog.txt index 7b5375b465..bf07c48a8a 100755 --- a/changelog.txt +++ b/changelog.txt @@ -28,6 +28,7 @@ Changes in branch for release 0.7.1 : - Feature: (RomanSyroeshko) GH-56 GH-57 - Template : Permit to save a template generated as a file (PHPWord_Template::saveAs()) - Feature: (gabrielbull) - Word2007 : Support sections page numbering - Feature: (gabrielbull) - Word2007 : Added support for line height +- Feature: (JillElaine) GH-5 - Word2007 : Added support for page header & page footer height - QA: (Progi1984) - UnitTests Changes in branch for release 0.7.0 : diff --git a/samples/Sample_03_Sections.php b/samples/Sample_03_Sections.php new file mode 100755 index 0000000000..d2231dfa6b --- /dev/null +++ b/samples/Sample_03_Sections.php @@ -0,0 +1,55 @@ +'); +} + +require_once '../Classes/PHPWord.php'; + +// New Word Document +echo date('H:i:s') , ' Create new PHPWord object' , EOL; +$PHPWord = new PHPWord(); + +// New portrait section +$section = $PHPWord->createSection(array('borderColor' => '00FF00', 'borderSize' => 12)); +$section->addText('I am placed on a default section.'); + +// New landscape section +$section = $PHPWord->createSection(array('orientation' => 'landscape')); +$section->addText('I am placed on a landscape section. Every page starting from this section will be landscape style.'); +$section->addPageBreak(); +$section->addPageBreak(); + +// New portrait section +$section = $PHPWord->createSection(array('marginLeft' => 600, 'marginRight' => 600, 'marginTop' => 600, 'marginBottom' => 600)); +$section->addText('This section uses other margins.'); + +// New portrait section with Header & Footer +$section = $PHPWord->createSection(array('marginLeft' => 200, 'marginRight' => 200, 'marginTop' => 200, 'marginBottom' => 200, 'headerHeight' => 50, 'footerHeight' => 50,)); +$section->addText('This section and we play with header/footer height.'); +$section->createHeader()->addText('Header'); +$section->createFooter()->addText('Footer'); + +// Save File +echo date('H:i:s') , ' Write to Word2007 format' , EOL; +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); +$objWriter->save(str_replace('.php', '.docx', __FILE__)); + +/*echo date('H:i:s') , ' Write to OpenDocumentText format' , EOL; +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText'); +$objWriter->save(str_replace('.php', '.odt', __FILE__)); + +echo date('H:i:s') , ' Write to RTF format' , EOL; +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF'); +$objWriter->save(str_replace('.php', '.rtf', __FILE__));*/ + + +// Echo memory peak usage +echo date('H:i:s') , ' Peak memory usage: ' , (memory_get_peak_usage(true) / 1024 / 1024) , ' MB' , EOL; + +// Echo done +echo date('H:i:s') , ' Done writing file' , EOL; diff --git a/samples/Sample_04_Textrun.php b/samples/Sample_04_Textrun.php new file mode 100644 index 0000000000..80ad2d3c87 --- /dev/null +++ b/samples/Sample_04_Textrun.php @@ -0,0 +1,61 @@ +'); +} + +require_once '../Classes/PHPWord.php'; + +// New Word Document +echo date('H:i:s') , ' Create new PHPWord object' , EOL; +$PHPWord = new PHPWord(); + + +// Ads styles +$PHPWord->addParagraphStyle('pStyle', array('spacing'=>100)); +$PHPWord->addFontStyle('BoldText', array('bold'=>true)); +$PHPWord->addFontStyle('ColoredText', array('color'=>'FF8080')); +$PHPWord->addLinkStyle('NLink', array('color'=>'0000FF', 'underline'=>PHPWord_Style_Font::UNDERLINE_SINGLE)); + +// New portrait section +$section = $PHPWord->createSection(); + +// Add text run +$textrun = $section->createTextRun('pStyle'); + +$textrun->addText('Each textrun can contain native text, link elements or an image.'); +$textrun->addText(' No break is placed after adding an element.', 'BoldText'); +$textrun->addText(' All elements are placed inside a paragraph with the optionally given p-Style.', 'ColoredText'); +$textrun->addText(' Sample Link: '); +$textrun->addLink('http://www.google.com', null, 'NLink'); +$textrun->addText(' Sample Image: '); +$textrun->addImage('old/_earth.jpg', array('width'=>18, 'height'=>18)); +$textrun->addText(' Here is some more text. '); + +// Save File +echo date('H:i:s') , ' Write to Word2007 format' , EOL; +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); +$objWriter->save(str_replace('.php', '.docx', __FILE__)); + +/* Text Run is not currently supported for ODText +echo date('H:i:s') , ' Write to OpenDocumentText format' , EOL; +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText'); +$objWriter->save(str_replace('.php', '.odt', __FILE__)); +*/ + +/* Text Run is not currently supported for RTF +echo date('H:i:s') , ' Write to RTF format' , EOL; +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF'); +$objWriter->save(str_replace('.php', '.rtf', __FILE__)); +*/ + +// Echo memory peak usage +echo date('H:i:s') , ' Peak memory usage: ' , (memory_get_peak_usage(true) / 1024 / 1024) , ' MB' , EOL; + +// Echo done +echo date('H:i:s') , ' Done writing file' , EOL; \ No newline at end of file