From 617feb3240f76f65cacd48d5bb0d8b4bde8f46cc Mon Sep 17 00:00:00 2001 From: Gabriel Bull Date: Wed, 12 Feb 2014 11:48:45 -0500 Subject: [PATCH] Added section page numbering --- Classes/PHPWord/Section/Settings.php | 26 ++++++++ Classes/PHPWord/Writer/Word2007/Document.php | 26 ++++---- README.md | 59 +++++++++++++------ changelog.txt | 3 + test/PHPWord/Tests/ImageTest.php | 2 +- .../Tests/Section/PageNumberingTest.php | 26 ++++++++ 6 files changed, 113 insertions(+), 29 deletions(-) create mode 100755 test/PHPWord/Tests/Section/PageNumberingTest.php diff --git a/Classes/PHPWord/Section/Settings.php b/Classes/PHPWord/Section/Settings.php index e638ac6f..020a82e3 100755 --- a/Classes/PHPWord/Section/Settings.php +++ b/Classes/PHPWord/Section/Settings.php @@ -150,6 +150,14 @@ class PHPWord_Section_Settings */ private $_borderBottomColor; + + /** + * Page Numbering Start + * + * @var int + */ + private $pageNumberingStart; + /** * Create new Section Settings */ @@ -542,4 +550,22 @@ class PHPWord_Section_Settings { return $this->_borderBottomColor; } + + /** + * @param null|int $pageNumberingStart + * @return $this + */ + public function setPageNumberingStart($pageNumberingStart = null) + { + $this->pageNumberingStart = $pageNumberingStart; + return $this; + } + + /** + * @return null|int + */ + public function getPageNumberingStart() + { + return $this->pageNumberingStart; + } } diff --git a/Classes/PHPWord/Writer/Word2007/Document.php b/Classes/PHPWord/Writer/Word2007/Document.php index f4308eda..20d3d5ca 100755 --- a/Classes/PHPWord/Writer/Word2007/Document.php +++ b/Classes/PHPWord/Writer/Word2007/Document.php @@ -123,19 +123,19 @@ class PHPWord_Writer_Word2007_Document extends PHPWord_Writer_Word2007_Base private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section) { - $_settings = $section->getSettings(); + $settings = $section->getSettings(); $_headers = $section->getHeaders(); $_footer = $section->getFooter(); - $pgSzW = $_settings->getPageSizeW(); - $pgSzH = $_settings->getPageSizeH(); - $orientation = $_settings->getOrientation(); + $pgSzW = $settings->getPageSizeW(); + $pgSzH = $settings->getPageSizeH(); + $orientation = $settings->getOrientation(); - $marginTop = $_settings->getMarginTop(); - $marginLeft = $_settings->getMarginLeft(); - $marginRight = $_settings->getMarginRight(); - $marginBottom = $_settings->getMarginBottom(); + $marginTop = $settings->getMarginTop(); + $marginLeft = $settings->getMarginLeft(); + $marginRight = $settings->getMarginRight(); + $marginBottom = $settings->getMarginBottom(); - $borders = $_settings->getBorderSize(); + $borders = $settings->getBorderSize(); $objWriter->startElement('w:sectPr'); @@ -182,7 +182,7 @@ class PHPWord_Writer_Word2007_Document extends PHPWord_Writer_Word2007_Base if (!is_null($borders[0]) || !is_null($borders[1]) || !is_null($borders[2]) || !is_null($borders[3])) { - $borderColor = $_settings->getBorderColor(); + $borderColor = $settings->getBorderColor(); $objWriter->startElement('w:pgBorders'); $objWriter->writeAttribute('w:offsetFrom', 'page'); @@ -225,6 +225,12 @@ class PHPWord_Writer_Word2007_Document extends PHPWord_Writer_Word2007_Base $objWriter->endElement(); } + // Page numbering + if (null !== $settings->getPageNumberingStart()) { + $objWriter->startElement('w:pgNumType'); + $objWriter->writeAttribute('w:start', $section->getSettings()->getPageNumberingStart()); + $objWriter->endElement(); + } $objWriter->startElement('w:cols'); $objWriter->writeAttribute('w:space', '720'); diff --git a/README.md b/README.md index 21c15959..8fc7881d 100755 --- a/README.md +++ b/README.md @@ -1,19 +1,18 @@ -# PHPWord - OpenXML - Read, Write and Create Word documents in PHP +# PHPWord + +__OpenXML - Read, Write and Create Word documents in PHP.__ + +PHPWord is a library written in PHP that create word documents. -PHPWord is a library written in PHP that create word documents. No Windows operating system is needed for usage because the result are docx files (Office Open XML) that can be opened by all major office software. -## Want to contribute? -Fork us! +__Want to contribute?__ Fork us! ## Requirements * PHP version 5.3.0 or higher -## License -PHPWord is licensed under [LGPL (GNU LESSER GENERAL PUBLIC LICENSE)](https://github.com/PHPOffice/PHPWord/blob/master/license.md) - ## Installation 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``. } ``` -## Usage +## Documentation + +### Table of contents + +1. [Basic usage](#basic-usage) +2. [Sections](#sections) + * [Change Section Page Numbering](#sections-page-numbering) +3. [Images](#images) + + +#### Basic usage The following is a basic example of the PHPWord library. @@ -59,7 +68,21 @@ $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $objWriter->save('helloWorld.docx'); ``` -## Images + +#### Sections + + +##### Change Section Page Numbering + +You can change a section page numbering. + +```php +$section = $PHPWord->createSection(); +$section->getSettings()->setPageNumberingStart(1); +``` + + +#### Images You can add images easily using the following example. @@ -69,16 +92,16 @@ $section->addImage('mars.jpg'); ``` Images settings include: - * ``width`` width in pixels - * ``height`` height in pixels - * ``align`` image alignment, __left__, __right__ or __center__ - * ``marginTop`` top margin in inches, can be negative - * ``marginLeft`` left margin in inches, can be negative - * ``wrappingStyle`` can be inline, __square__, __tight__, __behind__, __infront__ +* ``width`` width in pixels +* ``height`` height in pixels +* ``align`` image alignment, _left_, _right_ or _center_ +* ``marginTop`` top margin in inches, can be negative +* ``marginLeft`` left margin in inches, can be negative +* ``wrappingStyle`` can be _inline_, _square_, _tight_, _behind_, _infront_ - To add an image with settings, consider the following example. +To add an image with settings, consider the following example. - ```php +```php $section->addImage( 'mars.jpg', array( @@ -86,7 +109,7 @@ $section->addImage( 'height' => 100, 'marginTop' => -1, 'marginLeft' => -1, - wrappingStyle => 'behind' + 'wrappingStyle' => 'behind' ) ); ``` diff --git a/changelog.txt b/changelog.txt index caeaf601..94eb1657 100755 --- a/changelog.txt +++ b/changelog.txt @@ -22,6 +22,9 @@ * @version ##VERSION##, ##DATE## ************************************************************************************** +Changes in branch for release 0.7.1 : +- Feature: (gabrielbull) - Word2007 : Support sections page numbering + Fixed in branch for release 0.7.0 : - 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 - Bugfix: (RomanSyroeshko) GH-34 - PHPWord_Shared_String.IsUTF8 returns FALSE for Cyrillic UTF-8 input diff --git a/test/PHPWord/Tests/ImageTest.php b/test/PHPWord/Tests/ImageTest.php index 81b24677..710af5ad 100755 --- a/test/PHPWord/Tests/ImageTest.php +++ b/test/PHPWord/Tests/ImageTest.php @@ -14,7 +14,7 @@ class ImageTest extends PHPUnit_Framework_TestCase public function testImageWrappingStyleBehind() { $PHPWord = new PHPWord(); - $section = $PHPWord->createSection(12240, 15840, 0, 0, 0, 0); + $section = $PHPWord->createSection(); $section->addImage( __DIR__ . '/_files/images/earth.jpg', diff --git a/test/PHPWord/Tests/Section/PageNumberingTest.php b/test/PHPWord/Tests/Section/PageNumberingTest.php new file mode 100755 index 00000000..2223973e --- /dev/null +++ b/test/PHPWord/Tests/Section/PageNumberingTest.php @@ -0,0 +1,26 @@ +createSection(); + $section->getSettings()->setPageNumberingStart(2); + + $doc = TestHelper::getDocument($PHPWord); + $element = $doc->getElement('/w:document/w:body/w:sectPr/w:pgNumType'); + + $this->assertEquals(2, $element->getAttribute('w:start')); + } +} \ No newline at end of file