Skip to content

Commit d1e16a6

Browse files
committed
GH-5 : Word2007 : Added support for page header & page footer height
1 parent 5a6cb08 commit d1e16a6

File tree

4 files changed

+111
-2
lines changed

4 files changed

+111
-2
lines changed

Classes/PHPWord/Section/Settings.php

+50
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,16 @@ class PHPWord_Section_Settings
158158
*/
159159
private $pageNumberingStart;
160160

161+
/**
162+
* @var int
163+
*/
164+
private $headerHeight;
165+
166+
/**
167+
* @var int
168+
*/
169+
private $footerHeight;
170+
161171
/**
162172
* Create new Section Settings
163173
*/
@@ -178,6 +188,8 @@ public function __construct()
178188
$this->_borderRightColor = null;
179189
$this->_borderBottomSize = null;
180190
$this->_borderBottomColor = null;
191+
$this->headerHeight = 720; // set default header and footer to 720 twips (.5 inches)
192+
$this->footerHeight = 720;
181193
}
182194

183195
/**
@@ -568,4 +580,42 @@ public function getPageNumberingStart()
568580
{
569581
return $this->pageNumberingStart;
570582
}
583+
584+
/**
585+
* Get Header Height
586+
*
587+
* @return int
588+
*/
589+
public function getHeaderHeight() {
590+
return $this->headerHeight;
591+
}
592+
593+
/**
594+
* Set Header Height
595+
*
596+
* @param int $pValue
597+
*/
598+
public function setHeaderHeight($pValue = '') {
599+
$this->headerHeight = $pValue;
600+
return $this;
601+
}
602+
603+
/**
604+
* Get Footer Height
605+
*
606+
* @return int
607+
*/
608+
public function getFooterHeight() {
609+
return $this->footerHeight;
610+
}
611+
612+
/**
613+
* Set Footer Height
614+
*
615+
* @param int $pValue
616+
*/
617+
public function setFooterHeight($pValue = '') {
618+
$this->footerHeight = $pValue;
619+
return $this;
620+
}
571621
}

Classes/PHPWord/Writer/Word2007/Document.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PH
135135
$marginRight = $settings->getMarginRight();
136136
$marginBottom = $settings->getMarginBottom();
137137

138+
$headerHeight = $settings->getHeaderHeight();
139+
$footerHeight = $settings->getFooterHeight();
140+
138141
$borders = $settings->getBorderSize();
139142

140143
$objWriter->startElement('w:sectPr');
@@ -175,8 +178,8 @@ private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PH
175178
$objWriter->writeAttribute('w:right', $marginRight);
176179
$objWriter->writeAttribute('w:bottom', $marginBottom);
177180
$objWriter->writeAttribute('w:left', $marginLeft);
178-
$objWriter->writeAttribute('w:header', '720');
179-
$objWriter->writeAttribute('w:footer', '720');
181+
$objWriter->writeAttribute('w:header', $headerHeight);
182+
$objWriter->writeAttribute('w:footer', $footerHeight);
180183
$objWriter->writeAttribute('w:gutter', '0');
181184
$objWriter->endElement();
182185

changelog.txt

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Changes in branch for release 0.7.1 :
2828
- Feature: (RomanSyroeshko) GH-56 GH-57 - Template : Permit to save a template generated as a file (PHPWord_Template::saveAs())
2929
- Feature: (gabrielbull) - Word2007 : Support sections page numbering
3030
- Feature: (gabrielbull) - Word2007 : Added support for line height
31+
- Feature: (JillElaine) GH-5 - Word2007 : Added support for page header & page footer height
3132
- QA: (Progi1984) - UnitTests
3233

3334
Changes in branch for release 0.7.0 :

samples/Sample_03_Sections.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
error_reporting(E_ALL);
4+
5+
if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
6+
define('EOL', PHP_EOL);
7+
} else {
8+
define('EOL', '<br />');
9+
}
10+
11+
require_once '../Classes/PHPWord.php';
12+
13+
// New Word Document
14+
echo date('H:i:s') , ' Create new PHPWord object' , EOL;
15+
$PHPWord = new PHPWord();
16+
17+
// New portrait section
18+
$section = $PHPWord->createSection(array('borderColor' => '00FF00', 'borderSize' => 12));
19+
$section->addText('I am placed on a default section.');
20+
21+
// New landscape section
22+
$section = $PHPWord->createSection(array('orientation' => 'landscape'));
23+
$section->addText('I am placed on a landscape section. Every page starting from this section will be landscape style.');
24+
$section->addPageBreak();
25+
$section->addPageBreak();
26+
27+
// New portrait section
28+
$section = $PHPWord->createSection(array('marginLeft' => 600, 'marginRight' => 600, 'marginTop' => 600, 'marginBottom' => 600));
29+
$section->addText('This section uses other margins.');
30+
31+
// New portrait section with Header & Footer
32+
$section = $PHPWord->createSection(array('marginLeft' => 200, 'marginRight' => 200, 'marginTop' => 200, 'marginBottom' => 200, 'headerHeight' => 50, 'footerHeight' => 50,));
33+
$section->addText('This section and we play with header/footer height.');
34+
$section->createHeader()->addText('Header');
35+
$section->createFooter()->addText('Footer');
36+
37+
// Save File
38+
echo date('H:i:s') , ' Write to Word2007 format' , EOL;
39+
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
40+
$objWriter->save(str_replace('.php', '.docx', __FILE__));
41+
42+
/*echo date('H:i:s') , ' Write to OpenDocumentText format' , EOL;
43+
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText');
44+
$objWriter->save(str_replace('.php', '.odt', __FILE__));
45+
46+
echo date('H:i:s') , ' Write to RTF format' , EOL;
47+
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF');
48+
$objWriter->save(str_replace('.php', '.rtf', __FILE__));*/
49+
50+
51+
// Echo memory peak usage
52+
echo date('H:i:s') , ' Peak memory usage: ' , (memory_get_peak_usage(true) / 1024 / 1024) , ' MB' , EOL;
53+
54+
// Echo done
55+
echo date('H:i:s') , ' Done writing file' , EOL;

0 commit comments

Comments
 (0)