Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions Classes/PHPWord/Section/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,16 @@ class PHPWord_Section_Settings
*/
private $pageNumberingStart;

/**
* @var int
*/
private $headerHeight;

/**
* @var int
*/
private $footerHeight;

/**
* Create new Section Settings
*/
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}
}
21 changes: 21 additions & 0 deletions Classes/PHPWord/Section/TextRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
24 changes: 15 additions & 9 deletions Classes/PHPWord/Writer/Word2007/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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();

Expand All @@ -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');
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions Classes/PHPWord/Writer/Word2007/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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();

Expand Down
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand Down
55 changes: 55 additions & 0 deletions samples/Sample_03_Sections.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

error_reporting(E_ALL);

if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
define('EOL', PHP_EOL);
} else {
define('EOL', '<br />');
}

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;
61 changes: 61 additions & 0 deletions samples/Sample_04_Textrun.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

error_reporting(E_ALL);

if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
define('EOL', PHP_EOL);
}
else {
define('EOL', '<br />');
}

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;