Skip to content

Commit 617feb3

Browse files
committed
Added section page numbering
1 parent 3cf069f commit 617feb3

File tree

6 files changed

+113
-29
lines changed

6 files changed

+113
-29
lines changed

Classes/PHPWord/Section/Settings.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ class PHPWord_Section_Settings
150150
*/
151151
private $_borderBottomColor;
152152

153+
154+
/**
155+
* Page Numbering Start
156+
*
157+
* @var int
158+
*/
159+
private $pageNumberingStart;
160+
153161
/**
154162
* Create new Section Settings
155163
*/
@@ -542,4 +550,22 @@ public function getBorderBottomColor()
542550
{
543551
return $this->_borderBottomColor;
544552
}
553+
554+
/**
555+
* @param null|int $pageNumberingStart
556+
* @return $this
557+
*/
558+
public function setPageNumberingStart($pageNumberingStart = null)
559+
{
560+
$this->pageNumberingStart = $pageNumberingStart;
561+
return $this;
562+
}
563+
564+
/**
565+
* @return null|int
566+
*/
567+
public function getPageNumberingStart()
568+
{
569+
return $this->pageNumberingStart;
570+
}
545571
}

Classes/PHPWord/Writer/Word2007/Document.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,19 @@ private function _writeSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWo
123123

124124
private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section)
125125
{
126-
$_settings = $section->getSettings();
126+
$settings = $section->getSettings();
127127
$_headers = $section->getHeaders();
128128
$_footer = $section->getFooter();
129-
$pgSzW = $_settings->getPageSizeW();
130-
$pgSzH = $_settings->getPageSizeH();
131-
$orientation = $_settings->getOrientation();
129+
$pgSzW = $settings->getPageSizeW();
130+
$pgSzH = $settings->getPageSizeH();
131+
$orientation = $settings->getOrientation();
132132

133-
$marginTop = $_settings->getMarginTop();
134-
$marginLeft = $_settings->getMarginLeft();
135-
$marginRight = $_settings->getMarginRight();
136-
$marginBottom = $_settings->getMarginBottom();
133+
$marginTop = $settings->getMarginTop();
134+
$marginLeft = $settings->getMarginLeft();
135+
$marginRight = $settings->getMarginRight();
136+
$marginBottom = $settings->getMarginBottom();
137137

138-
$borders = $_settings->getBorderSize();
138+
$borders = $settings->getBorderSize();
139139

140140
$objWriter->startElement('w:sectPr');
141141

@@ -182,7 +182,7 @@ private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PH
182182

183183

184184
if (!is_null($borders[0]) || !is_null($borders[1]) || !is_null($borders[2]) || !is_null($borders[3])) {
185-
$borderColor = $_settings->getBorderColor();
185+
$borderColor = $settings->getBorderColor();
186186

187187
$objWriter->startElement('w:pgBorders');
188188
$objWriter->writeAttribute('w:offsetFrom', 'page');
@@ -225,6 +225,12 @@ private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PH
225225
$objWriter->endElement();
226226
}
227227

228+
// Page numbering
229+
if (null !== $settings->getPageNumberingStart()) {
230+
$objWriter->startElement('w:pgNumType');
231+
$objWriter->writeAttribute('w:start', $section->getSettings()->getPageNumberingStart());
232+
$objWriter->endElement();
233+
}
228234

229235
$objWriter->startElement('w:cols');
230236
$objWriter->writeAttribute('w:space', '720');

README.md

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
# PHPWord - OpenXML - Read, Write and Create Word documents in PHP
1+
# PHPWord
2+
3+
__OpenXML - Read, Write and Create Word documents in PHP.__
4+
5+
PHPWord is a library written in PHP that create word documents.
26

3-
PHPWord is a library written in PHP that create word documents.
47
No Windows operating system is needed for usage because the result are docx files (Office Open XML) that can be
58
opened by all major office software.
69

7-
## Want to contribute?
8-
Fork us!
10+
__Want to contribute?__ Fork us!
911

1012
## Requirements
1113

1214
* PHP version 5.3.0 or higher
1315

14-
## License
15-
PHPWord is licensed under [LGPL (GNU LESSER GENERAL PUBLIC LICENSE)](https://github.com/PHPOffice/PHPWord/blob/master/license.md)
16-
1716
## Installation
1817

1918
It is recommended that you install the PHPWord library [through composer](http://getcomposer.org/). To do so, add
@@ -27,7 +26,17 @@ the following lines to your ``composer.json``.
2726
}
2827
```
2928

30-
## Usage
29+
## Documentation
30+
31+
### Table of contents
32+
33+
1. [Basic usage](#basic-usage)
34+
2. [Sections](#sections)
35+
* [Change Section Page Numbering](#sections-page-numbering)
36+
3. [Images](#images)
37+
38+
<a name="basic-usage"></a>
39+
#### Basic usage
3140

3241
The following is a basic example of the PHPWord library.
3342

@@ -59,7 +68,21 @@ $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
5968
$objWriter->save('helloWorld.docx');
6069
```
6170

62-
## Images
71+
<a name="sections"></a>
72+
#### Sections
73+
74+
<a name="sections-page-numbering"></a>
75+
##### Change Section Page Numbering
76+
77+
You can change a section page numbering.
78+
79+
```php
80+
$section = $PHPWord->createSection();
81+
$section->getSettings()->setPageNumberingStart(1);
82+
```
83+
84+
<a name="images"></a>
85+
#### Images
6386

6487
You can add images easily using the following example.
6588

@@ -69,24 +92,24 @@ $section->addImage('mars.jpg');
6992
```
7093

7194
Images settings include:
72-
* ``width`` width in pixels
73-
* ``height`` height in pixels
74-
* ``align`` image alignment, __left__, __right__ or __center__
75-
* ``marginTop`` top margin in inches, can be negative
76-
* ``marginLeft`` left margin in inches, can be negative
77-
* ``wrappingStyle`` can be inline, __square__, __tight__, __behind__, __infront__
95+
* ``width`` width in pixels
96+
* ``height`` height in pixels
97+
* ``align`` image alignment, _left_, _right_ or _center_
98+
* ``marginTop`` top margin in inches, can be negative
99+
* ``marginLeft`` left margin in inches, can be negative
100+
* ``wrappingStyle`` can be _inline_, _square_, _tight_, _behind_, _infront_
78101

79-
To add an image with settings, consider the following example.
102+
To add an image with settings, consider the following example.
80103

81-
```php
104+
```php
82105
$section->addImage(
83106
'mars.jpg',
84107
array(
85108
'width' => 100,
86109
'height' => 100,
87110
'marginTop' => -1,
88111
'marginLeft' => -1,
89-
wrappingStyle => 'behind'
112+
'wrappingStyle' => 'behind'
90113
)
91114
);
92115
```

changelog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
* @version ##VERSION##, ##DATE##
2323
**************************************************************************************
2424

25+
Changes in branch for release 0.7.1 :
26+
- Feature: (gabrielbull) - Word2007 : Support sections page numbering
27+
2528
Fixed in branch for release 0.7.0 :
2629
- Bugfix: (RomanSyroeshko) GH-32 - "Warning: Invalid error type specified in ...\PHPWord.php on line 226" is thrown when the specified template file is not found
2730
- Bugfix: (RomanSyroeshko) GH-34 - PHPWord_Shared_String.IsUTF8 returns FALSE for Cyrillic UTF-8 input

test/PHPWord/Tests/ImageTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function tearDown()
1414
public function testImageWrappingStyleBehind()
1515
{
1616
$PHPWord = new PHPWord();
17-
$section = $PHPWord->createSection(12240, 15840, 0, 0, 0, 0);
17+
$section = $PHPWord->createSection();
1818

1919
$section->addImage(
2020
__DIR__ . '/_files/images/earth.jpg',
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
namespace PHPWord\Tests\Section;
3+
4+
use PHPUnit_Framework_TestCase;
5+
use PHPWord;
6+
use PHPWord\Tests\TestHelper;
7+
8+
class PageNumberingTest extends PHPUnit_Framework_TestCase
9+
{
10+
public function tearDown()
11+
{
12+
TestHelper::clear();
13+
}
14+
15+
public function testImageWrappingStyleBehind()
16+
{
17+
$PHPWord = new PHPWord();
18+
$section = $PHPWord->createSection();
19+
$section->getSettings()->setPageNumberingStart(2);
20+
21+
$doc = TestHelper::getDocument($PHPWord);
22+
$element = $doc->getElement('/w:document/w:body/w:sectPr/w:pgNumType');
23+
24+
$this->assertEquals(2, $element->getAttribute('w:start'));
25+
}
26+
}

0 commit comments

Comments
 (0)