Skip to content

Commit

Permalink
Word2007 Reader: Support for Paragraph Border Style (#2651)
Browse files Browse the repository at this point in the history
Co-authored-by: damienfa <[email protected]>
  • Loading branch information
Progi1984 and damienfa authored Aug 12, 2024
1 parent 6e27dbf commit 72c29a6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/changes/2.x/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- IOFactory : Added extractVariables method to extract variables from a document [@sibalonat](https://github.com/sibalonat) in [#2515](https://github.com/PHPOffice/PHPWord/pull/2515)
- PDF Writer : Documented how to specify a PDF renderer, when working with the PDF writer, as well as the three available choices by [@settermjd](https://github.com/settermjd) in [#2642](https://github.com/PHPOffice/PHPWord/pull/2642)
- Word2007 Reader: Support for Paragraph Border Style by [@damienfa](https://github.com/damienfa) in [#2651](https://github.com/PHPOffice/PHPWord/pull/2651)

### Bug fixes

Expand Down
10 changes: 5 additions & 5 deletions src/PhpWord/Element/TextRun.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ class TextRun extends AbstractContainer
/**
* Paragraph style.
*
* @var \PhpOffice\PhpWord\Style\Paragraph|string
* @var Paragraph|string
*/
protected $paragraphStyle;

/**
* Create new instance.
*
* @param array|\PhpOffice\PhpWord\Style\Paragraph|string $paragraphStyle
* @param array|Paragraph|string $paragraphStyle
*/
public function __construct($paragraphStyle = null)
{
Expand All @@ -49,7 +49,7 @@ public function __construct($paragraphStyle = null)
/**
* Get Paragraph style.
*
* @return \PhpOffice\PhpWord\Style\Paragraph|string
* @return Paragraph|string
*/
public function getParagraphStyle()
{
Expand All @@ -59,9 +59,9 @@ public function getParagraphStyle()
/**
* Set Paragraph style.
*
* @param array|\PhpOffice\PhpWord\Style\Paragraph|string $style
* @param array|Paragraph|string $style
*
* @return \PhpOffice\PhpWord\Style\Paragraph|string
* @return Paragraph|string
*/
public function setParagraphStyle($style = null)
{
Expand Down
12 changes: 12 additions & 0 deletions src/PhpWord/Reader/Word2007/AbstractPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,18 @@ protected function readParagraphStyle(XMLReader $xmlReader, DOMElement $domNode)
'contextualSpacing' => [self::READ_TRUE, 'w:contextualSpacing'],
'bidi' => [self::READ_TRUE, 'w:bidi'],
'suppressAutoHyphens' => [self::READ_TRUE, 'w:suppressAutoHyphens'],
'borderTopStyle' => [self::READ_VALUE, 'w:pBdr/w:top'],
'borderTopColor' => [self::READ_VALUE, 'w:pBdr/w:top', 'w:color'],
'borderTopSize' => [self::READ_VALUE, 'w:pBdr/w:top', 'w:sz'],
'borderRightStyle' => [self::READ_VALUE, 'w:pBdr/w:right'],
'borderRightColor' => [self::READ_VALUE, 'w:pBdr/w:right', 'w:color'],
'borderRightSize' => [self::READ_VALUE, 'w:pBdr/w:right', 'w:sz'],
'borderBottomStyle' => [self::READ_VALUE, 'w:pBdr/w:bottom'],
'borderBottomColor' => [self::READ_VALUE, 'w:pBdr/w:bottom', 'w:color'],
'borderBottomSize' => [self::READ_VALUE, 'w:pBdr/w:bottom', 'w:sz'],
'borderLeftStyle' => [self::READ_VALUE, 'w:pBdr/w:left'],
'borderLeftColor' => [self::READ_VALUE, 'w:pBdr/w:left', 'w:color'],
'borderLeftSize' => [self::READ_VALUE, 'w:pBdr/w:left', 'w:sz'],
];

return $this->readStyleDefs($xmlReader, $styleNode, $styleDefs);
Expand Down
36 changes: 36 additions & 0 deletions tests/PhpWordTests/Reader/Word2007Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Reader\Word2007;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Style\Paragraph;
use PhpOffice\PhpWordTests\TestHelperDOCX;

/**
Expand Down Expand Up @@ -82,6 +83,41 @@ public function testLoad(): void
self::assertEquals('0', $doc->getElementAttribute('/w:document/w:body/w:p/w:r[w:t/node()="italics"]/w:rPr/w:b', 'w:val'));
}

public function testLoadStyles(): void
{
$phpWord = IOFactory::load(dirname(__DIR__, 1) . '/_files/documents/reader-styles.docx', 'Word2007');

self::assertInstanceOf(PhpWord::class, $phpWord);

$section2 = $phpWord->getSection(2);
self::assertInstanceOf(Section::class, $section2);

$element2_31 = $section2->getElement(31);
self::assertInstanceOf(TextRun::class, $element2_31);
self::assertEquals('This is a paragraph with border differents', $element2_31->getText());

/** @var Paragraph $element2_31_pStyle */
$element2_31_pStyle = $element2_31->getParagraphStyle();
self::assertInstanceOf(Paragraph::class, $element2_31_pStyle);

// Top
self::assertEquals('FFFF00', $element2_31_pStyle->getBorderTopColor());
self::assertEquals('10', $element2_31_pStyle->getBorderTopSize());
self::assertEquals('dotted', $element2_31_pStyle->getBorderTopStyle());
// Right
self::assertEquals('00A933', $element2_31_pStyle->getBorderRightColor());
self::assertEquals('4', $element2_31_pStyle->getBorderRightSize());
self::assertEquals('dashed', $element2_31_pStyle->getBorderRightStyle());
// Bottom
self::assertEquals('F10D0C', $element2_31_pStyle->getBorderBottomColor());
self::assertEquals('8', $element2_31_pStyle->getBorderBottomSize());
self::assertEquals('dashSmallGap', $element2_31_pStyle->getBorderBottomStyle());
// Left
self::assertEquals('3465A4', $element2_31_pStyle->getBorderLeftColor());
self::assertEquals('8', $element2_31_pStyle->getBorderLeftSize());
self::assertEquals('dashed', $element2_31_pStyle->getBorderLeftStyle());
}

/**
* Load a Word 2011 file.
*/
Expand Down
Binary file not shown.

0 comments on commit 72c29a6

Please sign in to comment.