Skip to content

Commit 3ae7102

Browse files
authored
Merge pull request #335 from Progi1984/issue240
#240 : PowerPoint2007 Reader : Support of Table
2 parents 0889665 + 0b98604 commit 3ae7102

File tree

13 files changed

+491
-370
lines changed

13 files changed

+491
-370
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
- PowerPoint2007 Writer : Fixed the axis title in bar chart - @pgee70 GH-267
1111
- PowerPoint2007 Writer : Fixed the label position in bar chart - @pgee70 GH-268
1212
- PowerPoint2007 Writer : Support of margins in cell in table - @carlosafonso @Progi1984 GH-273 GH-315
13+
- Fixed the corruption of file when an addExternalSlide is called - @Progi1984 GH-240
1314

1415
### Features
1516
- ODPresentation Writer : Show/Hide Value / Name / Series Name in Chart - @Progi1984 GH-272
1617
- ODPresentation Writer : Axis Bounds in Chart - @Progi1984 GH-269
18+
- PowerPoint2007 Reader : Support of Table - @Progi1984 GH-240
1719
- PowerPoint2007 Writer : Implement character spacing - @jvanoostrom GH-301
1820
- PowerPoint2007 Writer : Axis Bounds in Chart - @Progi1984 GH-269
1921
- PowerPoint2007 Writer : Implement Legend Key in Series for Chart - @Progi1984 GH-319

samples/Sample_20_ExternalSlide.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
include_once 'Sample_Header.php';
3+
4+
use PhpOffice\PhpPresentation\PhpPresentation;
5+
use PhpOffice\PhpPresentation\Shape\RichText;
6+
7+
// Create new PHPPresentation object
8+
echo date('H:i:s') . ' Create new PHPPresentation object' . EOL;
9+
$objPHPPresentation = new PhpPresentation();
10+
$objPHPPresentation->removeSlideByIndex(0);
11+
12+
$oReader = \PhpOffice\PhpPresentation\IOFactory::createReader('PowerPoint2007');
13+
$oPresentation04 = $oReader->load(__DIR__ . '/results/Sample_04_Table.pptx');
14+
15+
foreach ($oPresentation04->getAllSlides() as $oSlide) {
16+
$objPHPPresentation->addExternalSlide($oSlide);
17+
}
18+
19+
// Save file
20+
echo write($objPHPPresentation, basename(__FILE__, '.php'), $writers);
21+
if (!CLI) {
22+
include_once 'Sample_Footer.php';
23+
}

src/PhpPresentation/PhpPresentation.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ public function addExternalSlide(Slide $slide)
324324
{
325325
$slide->rebindParent($this);
326326

327+
$this->addMasterSlide($slide->getSlideLayout()->getSlideMaster());
328+
327329
return $this->addSlide($slide);
328330
}
329331

@@ -353,8 +355,8 @@ public function createMasterSlide()
353355
* Add masterslide
354356
*
355357
* @param \PhpOffice\PhpPresentation\Slide\SlideMaster $slide
358+
* @return \PhpOffice\PhpPresentation\Slide\SlideMaster
356359
* @throws \Exception
357-
* @retun \PhpOffice\PhpPresentation\Slide\SlideMaster
358360
*/
359361
public function addMasterSlide(SlideMaster $slide = null)
360362
{

src/PhpPresentation/Reader/PowerPoint2007.php

Lines changed: 355 additions & 106 deletions
Large diffs are not rendered by default.

src/PhpPresentation/Shape/Table.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,24 @@ public function createRow()
100100
return $row;
101101
}
102102

103+
/**
104+
* @return int
105+
*/
106+
public function getNumColumns()
107+
{
108+
return $this->columnCount;
109+
}
110+
111+
/**
112+
* @param int $numColumn
113+
* @return Table
114+
*/
115+
public function setNumColumns($numColumn)
116+
{
117+
$this->columnCount = $numColumn;
118+
return $this;
119+
}
120+
103121
/**
104122
* Get hash code
105123
*

src/PhpPresentation/Shape/Table/Cell.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,21 @@ public function getParagraph($index = 0)
163163
*/
164164
public function createParagraph()
165165
{
166-
$alignment = clone $this->getActiveParagraph()->getAlignment();
167-
$font = clone $this->getActiveParagraph()->getFont();
168-
$bulletStyle = clone $this->getActiveParagraph()->getBulletStyle();
169-
170166
$this->richTextParagraphs[] = new Paragraph();
171-
$this->activeParagraph = count($this->richTextParagraphs) - 1;
172167

173-
$this->getActiveParagraph()->setAlignment($alignment);
174-
$this->getActiveParagraph()->setFont($font);
175-
$this->getActiveParagraph()->setBulletStyle($bulletStyle);
168+
if (count($this->richTextParagraphs) > 1) {
169+
$alignment = clone $this->getActiveParagraph()->getAlignment();
170+
$font = clone $this->getActiveParagraph()->getFont();
171+
$bulletStyle = clone $this->getActiveParagraph()->getBulletStyle();
172+
173+
$this->activeParagraph = count($this->richTextParagraphs) - 1;
174+
175+
$this->getActiveParagraph()->setAlignment($alignment);
176+
$this->getActiveParagraph()->setFont($font);
177+
$this->getActiveParagraph()->setBulletStyle($bulletStyle);
178+
} else {
179+
$this->activeParagraph = count($this->richTextParagraphs) - 1;
180+
}
176181

177182
return $this->getActiveParagraph();
178183
}

src/PhpPresentation/Slide/SlideLayout.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,12 @@ public function setLayoutName($layoutName)
8888
$this->layoutName = $layoutName;
8989
return $this;
9090
}
91+
92+
/**
93+
* @return SlideMaster
94+
*/
95+
public function getSlideMaster()
96+
{
97+
return $this->slideMaster;
98+
}
9199
}

src/PhpPresentation/Style/Color.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,19 @@ public function getRGB()
119119
/**
120120
* Set RGB
121121
*
122-
* @param string $pValue
122+
* @param string $pValue
123+
* @param string $pAlpha
123124
* @return \PhpOffice\PhpPresentation\Style\Color
124125
*/
125-
public function setRGB($pValue = '000000')
126+
public function setRGB($pValue = '000000', $pAlpha = 'FF')
126127
{
127128
if ($pValue == '') {
128129
$pValue = '000000';
129130
}
130-
$this->argb = 'FF' . $pValue;
131+
if ($pAlpha == '') {
132+
$pAlpha = 'FF';
133+
}
134+
$this->argb = $pAlpha . $pValue;
131135

132136
return $this;
133137
}

src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -303,55 +303,64 @@ protected function writeShapeTable(XMLWriter $objWriter, ShapeTable $shape, $sha
303303
{
304304
// p:graphicFrame
305305
$objWriter->startElement('p:graphicFrame');
306-
// p:nvGraphicFramePr
306+
// p:graphicFrame/p:nvGraphicFramePr
307307
$objWriter->startElement('p:nvGraphicFramePr');
308-
// p:cNvPr
308+
// p:graphicFrame/p:nvGraphicFramePr/p:cNvPr
309309
$objWriter->startElement('p:cNvPr');
310310
$objWriter->writeAttribute('id', $shapeId);
311311
$objWriter->writeAttribute('name', $shape->getName());
312312
$objWriter->writeAttribute('descr', $shape->getDescription());
313313
$objWriter->endElement();
314-
// p:cNvGraphicFramePr
314+
// p:graphicFrame/p:nvGraphicFramePr/p:cNvGraphicFramePr
315315
$objWriter->startElement('p:cNvGraphicFramePr');
316-
// a:graphicFrameLocks
316+
// p:graphicFrame/p:nvGraphicFramePr/p:cNvGraphicFramePr/a:graphicFrameLocks
317317
$objWriter->startElement('a:graphicFrameLocks');
318318
$objWriter->writeAttribute('noGrp', '1');
319319
$objWriter->endElement();
320+
// p:graphicFrame/p:nvGraphicFramePr/p:cNvGraphicFramePr/
320321
$objWriter->endElement();
321-
// p:nvPr
322-
$objWriter->writeElement('p:nvPr', null);
322+
// p:graphicFrame/p:nvGraphicFramePr/p:nvPr
323+
$objWriter->startElement('p:nvPr');
324+
if ($shape->isPlaceholder()) {
325+
$objWriter->startElement('p:ph');
326+
$objWriter->writeAttribute('type', $shape->getPlaceholder()->getType());
327+
$objWriter->endElement();
328+
}
323329
$objWriter->endElement();
324-
// p:xfrm
330+
// p:graphicFrame/p:nvGraphicFramePr/
331+
$objWriter->endElement();
332+
// p:graphicFrame/p:xfrm
325333
$objWriter->startElement('p:xfrm');
326-
// a:off
334+
// p:graphicFrame/p:xfrm/a:off
327335
$objWriter->startElement('a:off');
328336
$objWriter->writeAttribute('x', CommonDrawing::pixelsToEmu($shape->getOffsetX()));
329337
$objWriter->writeAttribute('y', CommonDrawing::pixelsToEmu($shape->getOffsetY()));
330338
$objWriter->endElement();
331-
// a:ext
339+
// p:graphicFrame/p:xfrm/a:ext
332340
$objWriter->startElement('a:ext');
333341
$objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu($shape->getWidth()));
334342
$objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu($shape->getHeight()));
335343
$objWriter->endElement();
344+
// p:graphicFrame/p:xfrm/
336345
$objWriter->endElement();
337-
// a:graphic
346+
// p:graphicFrame/a:graphic
338347
$objWriter->startElement('a:graphic');
339-
// a:graphicData
348+
// p:graphicFrame/a:graphic/a:graphicData
340349
$objWriter->startElement('a:graphicData');
341350
$objWriter->writeAttribute('uri', 'http://schemas.openxmlformats.org/drawingml/2006/table');
342-
// a:tbl
351+
// p:graphicFrame/a:graphic/a:graphicData/a:tbl
343352
$objWriter->startElement('a:tbl');
344-
// a:tblPr
353+
// p:graphicFrame/a:graphic/a:graphicData/a:tbl/a:tblPr
345354
$objWriter->startElement('a:tblPr');
346355
$objWriter->writeAttribute('firstRow', '1');
347356
$objWriter->writeAttribute('bandRow', '1');
348357
$objWriter->endElement();
349-
// a:tblGrid
358+
// p:graphicFrame/a:graphic/a:graphicData/a:tbl/a:tblGrid
350359
$objWriter->startElement('a:tblGrid');
351360
// Write cell widths
352361
$countCells = count($shape->getRow(0)->getCells());
353362
for ($cell = 0; $cell < $countCells; $cell++) {
354-
// a:gridCol
363+
// p:graphicFrame/a:graphic/a:graphicData/a:tbl/a:tblGrid/a:gridCol
355364
$objWriter->startElement('a:gridCol');
356365
// Calculate column width
357366
$width = $shape->getRow(0)->getCell($cell)->getWidth();
@@ -363,6 +372,7 @@ protected function writeShapeTable(XMLWriter $objWriter, ShapeTable $shape, $sha
363372
$objWriter->writeAttribute('w', CommonDrawing::pixelsToEmu($width));
364373
$objWriter->endElement();
365374
}
375+
// p:graphicFrame/a:graphic/a:graphicData/a:tbl/a:tblGrid/
366376
$objWriter->endElement();
367377
// Colspan / rowspan containers
368378
$colSpan = array();
@@ -372,7 +382,7 @@ protected function writeShapeTable(XMLWriter $objWriter, ShapeTable $shape, $sha
372382
// Write rows
373383
$countRows = count($shape->getRows());
374384
for ($row = 0; $row < $countRows; $row++) {
375-
// a:tr
385+
// p:graphicFrame/a:graphic/a:graphicData/a:tbl/a:tr
376386
$objWriter->startElement('a:tr');
377387
$objWriter->writeAttribute('h', CommonDrawing::pixelsToEmu($shape->getRow($row)->getHeight()));
378388
// Write cells
@@ -408,12 +418,13 @@ protected function writeShapeTable(XMLWriter $objWriter, ShapeTable $shape, $sha
408418
}
409419
// a:txBody
410420
$objWriter->startElement('a:txBody');
411-
// a:bodyPr
421+
// a:txBody/a:bodyPr
412422
$objWriter->startElement('a:bodyPr');
413423
$objWriter->writeAttribute('wrap', 'square');
414424
$objWriter->writeAttribute('rtlCol', '0');
415-
// a:spAutoFit
425+
// a:txBody/a:bodyPr/a:spAutoFit
416426
$objWriter->writeElement('a:spAutoFit', null);
427+
// a:txBody/a:bodyPr/
417428
$objWriter->endElement();
418429
// a:lstStyle
419430
$objWriter->writeElement('a:lstStyle', null);
@@ -428,6 +439,14 @@ protected function writeShapeTable(XMLWriter $objWriter, ShapeTable $shape, $sha
428439
if ($verticalAlign != Alignment::VERTICAL_BASE && $verticalAlign != Alignment::VERTICAL_AUTO) {
429440
$objWriter->writeAttribute('anchor', $verticalAlign);
430441
}
442+
443+
// Margins
444+
$alignment = $firstParagraph->getAlignment();
445+
$objWriter->writeAttribute('marL', $alignment->getMarginLeft());
446+
$objWriter->writeAttribute('marR', $alignment->getMarginRight());
447+
$objWriter->writeAttribute('marT', $alignment->getMarginTop());
448+
$objWriter->writeAttribute('marB', $alignment->getMarginBottom());
449+
431450
// Determine borders
432451
$borderLeft = $currentCell->getBorders()->getLeft();
433452
$borderRight = $currentCell->getBorders()->getRight();

0 commit comments

Comments
 (0)