Skip to content

Commit 6e17274

Browse files
authored
Merge pull request #1569 from troosan/page_vertical_align
implement support for section vAlign
2 parents 5b0c8a3 + e3020c0 commit 6e17274

File tree

11 files changed

+131
-3
lines changed

11 files changed

+131
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ v0.17.0 (?? ??? 2019)
77
----------------------
88
### Added
99
- Add RightToLeft table presentation. @troosan #1550
10+
- Add support for page vertical alignment. @troosan #672 #1569
1011

1112
### Fixed
1213

docs/styles.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Available Section style options:
3232
See ``\PhpOffice\PhpWord\Style\Section::ORIENTATION_...`` class constants for possible values
3333
- ``pageSizeH``. Page height in *twip*. Implicitly defined by ``orientation`` option. Any changes are discouraged.
3434
- ``pageSizeW``. Page width in *twip*. Implicitly defined by ``orientation`` option. Any changes are discouraged.
35+
- ``vAlign``. Vertical Page Alignment
36+
See ``\PhpOffice\PhpWord\SimpleType\VerticalJc`` for possible values
3537

3638
.. _font-style:
3739

samples/Sample_03_Sections.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
use PhpOffice\PhpWord\SimpleType\VerticalJc;
3+
24
include_once 'Sample_Header.php';
35

46
// New Word Document
@@ -21,6 +23,12 @@
2123
);
2224
$section->addText('This section uses other margins with folio papersize.');
2325

26+
// The text of this section is vertically centered
27+
$section = $phpWord->addSection(
28+
array('vAlign' => VerticalJc::CENTER)
29+
);
30+
$section->addText('This section is vertically centered.');
31+
2432
// New portrait section with Header & Footer
2533
$section = $phpWord->addSection(
2634
array(

src/PhpWord/Reader/Word2007/Document.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ private function readSectionStyle(XMLReader $xmlReader, \DOMElement $domNode)
106106
{
107107
$styleDefs = array(
108108
'breakType' => array(self::READ_VALUE, 'w:type'),
109+
'vAlign' => array(self::READ_VALUE, 'w:vAlign'),
109110
'pageSizeW' => array(self::READ_VALUE, 'w:pgSz', 'w:w'),
110111
'pageSizeH' => array(self::READ_VALUE, 'w:pgSz', 'w:h'),
111112
'orientation' => array(self::READ_VALUE, 'w:pgSz', 'w:orient'),

src/PhpWord/SimpleType/VerticalJc.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @see https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2018 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\SimpleType;
19+
20+
use PhpOffice\PhpWord\Shared\AbstractEnum;
21+
22+
/**
23+
* Vertical Alignment Type.
24+
*
25+
* Introduced in ISO/IEC-29500:2008.
26+
*
27+
* @see http://www.datypic.com/sc/ooxml/t-w_ST_VerticalJc.html
28+
* @since 0.17.0
29+
*/
30+
final class VerticalJc extends AbstractEnum
31+
{
32+
const TOP = 'top';
33+
const CENTER = 'center';
34+
const BOTH = 'both';
35+
const BOTTOM = 'bottom';
36+
}

src/PhpWord/Style/Cell.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace PhpOffice\PhpWord\Style;
1919

2020
use PhpOffice\PhpWord\SimpleType\TblWidth;
21+
use PhpOffice\PhpWord\SimpleType\VerticalJc;
2122

2223
/**
2324
* Table cell style
@@ -28,10 +29,20 @@ class Cell extends Border
2829
* Vertical alignment constants
2930
*
3031
* @const string
32+
* @deprecated Use \PhpOffice\PhpWord\SimpleType\VerticalJc::TOP instead
3133
*/
3234
const VALIGN_TOP = 'top';
35+
/**
36+
* @deprecated Use \PhpOffice\PhpWord\SimpleType\VerticalJc::CENTER instead
37+
*/
3338
const VALIGN_CENTER = 'center';
39+
/**
40+
* @deprecated Use \PhpOffice\PhpWord\SimpleType\VerticalJc::BOTTOM instead
41+
*/
3442
const VALIGN_BOTTOM = 'bottom';
43+
/**
44+
* @deprecated Use \PhpOffice\PhpWord\SimpleType\VerticalJc::BOTH instead
45+
*/
3546
const VALIGN_BOTH = 'both';
3647

3748
//Text direction constants
@@ -145,8 +156,8 @@ public function getVAlign()
145156
*/
146157
public function setVAlign($value = null)
147158
{
148-
$enum = array(self::VALIGN_TOP, self::VALIGN_CENTER, self::VALIGN_BOTTOM, self::VALIGN_BOTH);
149-
$this->vAlign = $this->setEnumVal($value, $enum, $this->vAlign);
159+
VerticalJc::validate($value);
160+
$this->vAlign = $this->setEnumVal($value, VerticalJc::values(), $this->vAlign);
150161

151162
return $this;
152163
}

src/PhpWord/Style/Section.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace PhpOffice\PhpWord\Style;
1919

20+
use PhpOffice\PhpWord\SimpleType\VerticalJc;
21+
2022
/**
2123
* Section settings
2224
*/
@@ -166,6 +168,14 @@ class Section extends Border
166168
*/
167169
private $lineNumbering;
168170

171+
/**
172+
* Vertical Text Alignment on Page
173+
* One of \PhpOffice\PhpWord\SimpleType\VerticalJc
174+
*
175+
* @var string
176+
*/
177+
private $vAlign;
178+
169179
/**
170180
* Create new instance
171181
*/
@@ -599,4 +609,28 @@ public function setLineNumbering($value = null)
599609

600610
return $this;
601611
}
612+
613+
/**
614+
* Get vertical alignment
615+
*
616+
* @return string
617+
*/
618+
public function getVAlign()
619+
{
620+
return $this->vAlign;
621+
}
622+
623+
/**
624+
* Set vertical alignment
625+
*
626+
* @param string $value
627+
* @return self
628+
*/
629+
public function setVAlign($value = null)
630+
{
631+
VerticalJc::validate($value);
632+
$this->vAlign = $value;
633+
634+
return $this;
635+
}
602636
}

src/PhpWord/Writer/Word2007/Style/Section.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public function write()
4848
$xmlWriter->writeAttribute('w:h', $style->getPageSizeH());
4949
$xmlWriter->endElement(); // w:pgSz
5050

51+
// Vertical alignment
52+
$vAlign = $style->getVAlign();
53+
$xmlWriter->writeElementIf(!is_null($vAlign), 'w:vAlign', 'w:val', $vAlign);
54+
5155
// Margins
5256
$margins = array(
5357
'w:top' => array('getMarginTop', SectionStyle::DEFAULT_MARGIN),

tests/PhpWord/Reader/Word2007/StyleTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use PhpOffice\PhpWord\AbstractTestReader;
2121
use PhpOffice\PhpWord\SimpleType\TblWidth;
22+
use PhpOffice\PhpWord\SimpleType\VerticalJc;
2223
use PhpOffice\PhpWord\Style;
2324
use PhpOffice\PhpWord\Style\Table;
2425
use PhpOffice\PhpWord\Style\TablePosition;
@@ -213,4 +214,16 @@ public function testReadHeading()
213214
$this->getDocumentFromString(array('styles' => $documentXml));
214215
$this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', Style::getStyle($name));
215216
}
217+
218+
public function testPageVerticalAlign()
219+
{
220+
$documentXml = '<w:sectPr>
221+
<w:vAlign w:val="center"/>
222+
</w:sectPr>';
223+
224+
$phpWord = $this->getDocumentFromString(array('document' => $documentXml));
225+
226+
$sectionStyle = $phpWord->getSection(0)->getStyle();
227+
$this->assertEquals(VerticalJc::CENTER, $sectionStyle->getVAlign());
228+
}
216229
}

tests/PhpWord/Style/CellTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace PhpOffice\PhpWord\Style;
1919

20+
use PhpOffice\PhpWord\SimpleType\VerticalJc;
21+
2022
/**
2123
* Test class for PhpOffice\PhpWord\Style\Cell
2224
*
@@ -33,7 +35,7 @@ public function testSetGetNormal()
3335
$object = new Cell();
3436

3537
$attributes = array(
36-
'valign' => Cell::VALIGN_TOP,
38+
'valign' => VerticalJc::TOP,
3739
'textDirection' => Cell::TEXT_DIR_BTLR,
3840
'bgColor' => 'FFFF00',
3941
'borderTopSize' => 120,

tests/PhpWord/Style/SectionTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
namespace PhpOffice\PhpWord\Style;
1919

20+
use PhpOffice\PhpWord\SimpleType\VerticalJc;
21+
2022
/**
2123
* Test class for PhpOffice\PhpWord\Style\Section
2224
*
@@ -328,4 +330,18 @@ public function testBreakType()
328330
$oSettings->setBreakType();
329331
$this->assertNull($oSettings->getBreakType());
330332
}
333+
334+
/**
335+
* Vertical page alignment
336+
*/
337+
public function testVerticalAlign()
338+
{
339+
// Section Settings
340+
$oSettings = new Section();
341+
342+
$this->assertNull($oSettings->getVAlign());
343+
344+
$oSettings->setVAlign(VerticalJc::BOTH);
345+
$this->assertEquals('both', $oSettings->getVAlign());
346+
}
331347
}

0 commit comments

Comments
 (0)