Skip to content

Word2007 Reader: Support for Paragraph Border Style #2651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading