PHPOffice / PHPPresentation

A pure PHP library for reading and writing presentations documents
https://phpoffice.github.io/PHPPresentation
Other
1.31k stars 523 forks source link

Vertical alignment does not work if it is a placeholder #799

Open NigelSwinson opened 7 months ago

NigelSwinson commented 7 months ago

The following sample demonstrates that if you have an entity in a slide (or a slide layout) which has Alignment::Vertical_CENTER, then it will only be aligned center if the object does not also have a PlaceHolder. Horizontal alignment is however respected in both cases.

<?php

use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\Shape\Placeholder;
use PhpOffice\PhpPresentation\Style\Color;
use PhpOffice\PhpPresentation\Style\Alignment;

include_once 'Sample_Header.php';

// Create new PHPPresentation object
echo date('H:i:s') . ' Create new PHPPresentation object' . EOL;
$objPHPPresentation = new PhpPresentation();

// Set properties
echo date('H:i:s') . ' Set properties' . EOL;
$objPHPPresentation->getDocumentProperties()->setCreator('PHPOffice')
->setLastModifiedBy('PHPPresentation Team')
->setTitle('Sample 20 SlideLayout')
->setSubject('Sample 20 Subject')
->setDescription('Sample 20 Description')
->setKeywords('office 2007 openxml libreoffice odt php')
->setCategory('Sample Category');

// Create slide
echo date('H:i:s') . ' Create slide' . EOL;
$currentSlide = $objPHPPresentation->getActiveSlide();

echo date('H:i:s') . ' Create SlideLayout' . EOL;
$slideLayout = $objPHPPresentation->getAllMasterSlides()[0]->createSlideLayout();
$slideLayout->setLayoutName('Sample Layout');

$slides = array($slideLayout, $objPHPPresentation->getActiveSlide());

foreach ($slides as $slide) {
    echo date('H:i:s') . ' Create Footer' . EOL;
    $footerTextShape = $slide->createRichTextShape();
    $footerTextShape->setPlaceHolder(new Placeholder(Placeholder::PH_TYPE_FOOTER));

    $footerTextShape
    ->setOffsetX(77)
    ->setOffsetY(0)
    ->setWidth(448)
    ->setHeight(223);

    $footerTextRun = $footerTextShape->createTextRun('With setPlaceholder');
    $footerTextRun->getFont()
    ->setName('Calibri')
    ->setSize(9)
    ->setColor(new Color(Color::COLOR_DARKGREEN))
    ->setBold(true);
    $footerTextShape->getActiveParagraph()->getAlignment()
    ->setVertical(Alignment::VERTICAL_CENTER);

    $footerTextShape = $slide->createRichTextShape();
    //$footerTextShape->setPlaceHolder(new Placeholder(Placeholder::PH_TYPE_FOOTER));

    $footerTextShape
    ->setOffsetX(77)
    ->setOffsetY(479)
    ->setWidth(448)
    ->setHeight(223);

    $footerTextRun = $footerTextShape->createTextRun('Without placeholder');
    $footerTextRun->getFont()
    ->setName('Calibri')
    ->setSize(9)
    ->setColor(new Color(Color::COLOR_DARKGREEN))
    ->setBold(true);
    $footerTextShape->getActiveParagraph()->getAlignment()
    ->setVertical(Alignment::VERTICAL_CENTER);
}

echo date('H:i:s') . ' Apply Layout' . EOL;
$currentSlide->setSlideLayout($slideLayout);

// Save file
echo write($objPHPPresentation, basename(__FILE__, '.php'), $writers);
if (!CLI) {
    include_once 'Sample_Footer.php';
}
NigelSwinson commented 7 months ago

I think it's possible the tag needs to have an anchor="ctr" attribute.

NigelSwinson commented 7 months ago

Note my testing was done here against the develop branch. The master branch seemed to have a lot more bugs in this area; so great to see/benefit from progress here.

By amending AbstractSlide.php to remove the if (!$shape->isPlaceholder()) { command in writeShapeText, this seems to work. It feels like it is on the same lines as what @devX2712 was trying to do on the 13th December 2023 with other commits in the same file. I'm hoping his expert advice here might conclude that these type of changes should be expanded further.