Skip to content

Commit ee66f17

Browse files
committed
RTF Writer : Support for Table Border Style
1 parent c917d03 commit ee66f17

File tree

4 files changed

+114
-3
lines changed

4 files changed

+114
-3
lines changed

docs/changes/2.x/2.0.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Word2007 Reader: Support for Paragraph Border Style by [@damienfa](https://github.com/damienfa) in [#2651](https://github.com/PHPOffice/PHPWord/pull/2651)
1010
- Word2007 Writer: Support for field REF by [@crystoline](https://github.com/crystoline) in [#2652](https://github.com/PHPOffice/PHPWord/pull/2652)
1111
- Word2007 Reader : Support for FormFields by [@vincentKool](https://github.com/vincentKool) in [#2653](https://github.com/PHPOffice/PHPWord/pull/2653)
12+
- RTF Writer : Support for Table Border Style fixing [#345](https://github.com/PHPOffice/PHPWord/issues/345) by [@Progi1984](https://github.com/Progi1984) in [#2656](https://github.com/PHPOffice/PHPWord/pull/2656)
1213

1314
### Bug fixes
1415

src/PhpWord/Writer/HTML/Element/Table.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,7 @@ private function getTableStyle($tableStyle = null): string
120120
return '';
121121
}
122122
if (is_string($tableStyle)) {
123-
$style = ' class="' . $tableStyle;
124-
125-
return $style . '"';
123+
return ' class="' . $tableStyle . '"';
126124
}
127125

128126
$styleWriter = new TableStyleWriter($tableStyle);

src/PhpWord/Writer/RTF/Element/Table.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
use PhpOffice\PhpWord\Element\Table as TableElement;
2323
use PhpOffice\PhpWord\Settings;
2424
use PhpOffice\PhpWord\Style;
25+
use PhpOffice\PhpWord\Style\Border as BorderStyle;
26+
use PhpOffice\PhpWord\Style\Cell as CellStyle;
27+
use PhpOffice\PhpWord\Style\Table as TableStyle;
28+
use PhpOffice\PhpWord\SimpleType\Border;
2529

2630
/**
2731
* Table element RTF writer.
@@ -77,9 +81,15 @@ public function write()
7781
private function writeRowDef(RowElement $row)
7882
{
7983
$content = '';
84+
$tableStyle = $this->element->getStyle();
85+
if (is_string($tableStyle)) {
86+
$tableStyle = Style::getStyle($tableStyle);
87+
}
8088

8189
$rightMargin = 0;
8290
foreach ($row->getCells() as $cell) {
91+
$content .= $this->writeCellStyle($cell->getStyle(), $tableStyle);
92+
8393
$width = $cell->getWidth();
8494
$vMerge = $this->getVMerge($cell->getStyle()->getVMerge());
8595
if ($width === null) {
@@ -127,6 +137,100 @@ private function writeCell(CellElement $cell)
127137
return $content;
128138
}
129139

140+
private function writeCellStyle(CellStyle $cell, ?TableStyle $table): string
141+
{
142+
$content = $this->writeCellBorder(
143+
't',
144+
$cell->getBorderTopStyle(),
145+
round($cell->getBorderTopSize() ?? 0),
146+
$cell->getBorderTopColor() ?? ($table ? $table->getBorderTopColor() : null)
147+
) . PHP_EOL;
148+
$content .= $this->writeCellBorder(
149+
'l',
150+
$cell->getBorderLeftStyle(),
151+
round($cell->getBorderLeftSize() ?? 0),
152+
$cell->getBorderLeftColor() ?? ($table ? $table->getBorderLeftColor() : null)
153+
) . PHP_EOL;
154+
$content .= $this->writeCellBorder(
155+
'b',
156+
$cell->getBorderBottomStyle(),
157+
round($cell->getBorderBottomSize() ?? 0),
158+
$cell->getBorderBottomColor() ?? ($table ? $table->getBorderBottomColor() : null)
159+
) . PHP_EOL;
160+
$content .= $this->writeCellBorder(
161+
'r',
162+
$cell->getBorderRightStyle(),
163+
round($cell->getBorderRightSize() ?? 0),
164+
$cell->getBorderRightColor() ?? ($table ? $table->getBorderRightColor() : null)
165+
) . PHP_EOL;
166+
167+
return $content;
168+
}
169+
170+
private function writeCellBorder(string $prefix, ?string $borderStyle, int $borderSize, ?string $borderColor): string
171+
{
172+
if ($borderSize == 0) {
173+
return '';
174+
}
175+
176+
$content = '\clbrdr'. $prefix;
177+
/**
178+
* \brdrs Single-thickness border.
179+
* \brdrth Double-thickness border.
180+
* \brdrsh Shadowed border.
181+
* \brdrdb Double border.
182+
* \brdrdot Dotted border.
183+
* \brdrdash Dashed border.
184+
* \brdrhair Hairline border.
185+
* \brdrinset Inset border.
186+
* \brdrdashsm Dash border (small).
187+
* \brdrdashd Dot dash border.
188+
* \brdrdashdd Dot dot dash border.
189+
* \brdroutset Outset border.
190+
* \brdrtriple Triple border.
191+
* \brdrtnthsg Thick thin border (small).
192+
* \brdrthtnsg Thin thick border (small).
193+
* \brdrtnthtnsg Thin thick thin border (small).
194+
* \brdrtnthmg Thick thin border (medium).
195+
* \brdrthtnmg Thin thick border (medium).
196+
* \brdrtnthtnmg Thin thick thin border (medium).
197+
* \brdrtnthlg Thick thin border (large).
198+
* \brdrthtnlg Thin thick border (large).
199+
* \brdrtnthtnlg Thin thick thin border (large).
200+
* \brdrwavy Wavy border.
201+
* \brdrwavydb Double wavy border.
202+
* \brdrdashdotstr Striped border.
203+
* \brdremboss Emboss border.
204+
* \brdrengrave Engrave border.
205+
*/
206+
switch($borderStyle) {
207+
case Border::DOTTED:
208+
$content .= '\brdrdot';
209+
break;
210+
case Border::SINGLE:
211+
default:
212+
$content .= '\brdrs';
213+
break;
214+
}
215+
216+
// \brdrwN N is the width in twips (1/20 pt) of the pen used to draw the paragraph border line.
217+
// N cannot be greater than 75.
218+
// To obtain a larger border width, the \brdth control word can be used to obtain a width double that of N.
219+
// $borderSize is in eights of a point, i.e. 4 / 8 = .5pt
220+
// 1/20 pt => 1/8 / 2.5
221+
$content .= '\brdrw' . (int) ($borderSize / 2.5);
222+
223+
// \brdrcfN N is the color of the paragraph border, specified as an index into the color table in the RTF header.
224+
$colorIndex = 0;
225+
$index = array_search($borderColor, $this->parentWriter->getColorTable());
226+
if ($index !== false) {
227+
$colorIndex = $index + 1;
228+
}
229+
$content .= '\brdrcf' . $colorIndex;
230+
231+
return $content;
232+
}
233+
130234
/**
131235
* Get vertical merge style.
132236
*

src/PhpWord/Writer/RTF/Part/Header.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use PhpOffice\PhpWord\Shared\Converter;
2222
use PhpOffice\PhpWord\Style;
2323
use PhpOffice\PhpWord\Style\Font;
24+
use PhpOffice\PhpWord\Style\Table;
2425

2526
/**
2627
* RTF header part writer.
@@ -236,6 +237,13 @@ private function registerFontItems($style): void
236237
$this->registerTableItem($this->fontTable, $style->getName(), $defaultFont);
237238
$this->registerTableItem($this->colorTable, $style->getColor(), $defaultColor);
238239
$this->registerTableItem($this->colorTable, $style->getFgColor(), $defaultColor);
240+
return;
241+
}
242+
if ($style instanceof Table) {
243+
$this->registerTableItem($this->colorTable, $style->getBorderTopColor(), $defaultColor);
244+
$this->registerTableItem($this->colorTable, $style->getBorderRightColor(), $defaultColor);
245+
$this->registerTableItem($this->colorTable, $style->getBorderLeftColor(), $defaultColor);
246+
$this->registerTableItem($this->colorTable, $style->getBorderBottomColor(), $defaultColor);
239247
}
240248
}
241249

0 commit comments

Comments
 (0)