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

Captions for Images/Tables #809

Open kilianweisl opened 3 months ago

kilianweisl commented 3 months ago

Hello!

I'd like to add captions to images or tables.

I couldn't find anything in the docs. Either it's possible to get the height of a dynamic shape after filling it (like adding rows to a table) or there is a solution implemented already.

For images, I could also pre-calculate the height when drawing the image with a specific width (considering its aspect-ration), but getting the height via the library would be much more convenient.

Many thanks in advance!

Progi1984 commented 3 months ago

@kilianweisl Hi have you got a sample file as expected result for analysis ? Thanks

kilianweisl commented 3 months ago

@Progi1984 Sure! Thanks for the fast reply.

Suppose I have the following code:

  public function createTable()
  {
    $pptx = new PhpPresentation();
    $slide = $pptx->getActiveSlide();

    $shape = $slide->createTableShape(3);
    $shape->setWidth(Converter::convertEmuToPx($pptx->getLayout()->getCX()) - 20);
    $shape->setOffsetX(10);
    $shape->setOffsetY(10);

    for($rowCount = 0; $rowCount < 3; $rowCount++) {
      $row = $shape->createRow();
      for($cellCount = 0; $cellCount < 3; $cellCount++) {
        $cell = $row->nextCell();
        $cell->createTextRun('Cell ' . $cellCount)
          ->getFont()
          ->setSize(20);
      }
    }

    $writer = IOFactory::createWriter($pptx, 'PowerPoint2007');
    $writer->save('./table.pptx');
  }

The output .pptx file is:

Screenshot 2024-08-19 at 17 29 58

Now the problem is, I don't know how many rows my table has and I need to add a caption below it like so:

Screenshot 2024-08-19 at 17 31 43

How can I get the final height of the table (after adding all rows) in order to place a text object afterwards?

This might also include splitting the table and printing them on multiple slides, as mentioned in https://github.com/PHPOffice/PHPPresentation/issues/72.

Progi1984 commented 3 months ago

@kilianweisl Could you send me a sample docx file with expected result ? I want to analyze the file. Thanks

kilianweisl commented 3 months ago

Do you mean the pptx file? It's just a bunch of rows and a text underneath it.

table.pptx

Progi1984 commented 3 months ago

Oh. I didn't understand it. I think you must all calculate : font size, margin, border size, etc...

kilianweisl commented 3 months ago

While that'd be somehow possible with simple tables, I don't think that it is possible when you e.g. have the following case, where an arbitrary column contains text that forces a line break.

Or is there a way to access the height of a cell/row/RichTextShape after putting text into it?

Screenshot 2024-08-19 at 18 09 30