Tables **************** PHPRtfLite supports tables and nested tables. .. code-block:: php addSection(); $table = $section->addTable(); // add 2 rows with a height of 1cm for each of them $table->addRows(3, 1); // add a row with a height of 2cm (table has now 4 rows) $table->addRow(2); // add 3 columns (first: 1cm, second: 2cm, third: 3cm) $table->addColumnsList(array(1,2,3)); Cells ==================== Cells can contain images, texts, paragraphs and (nested) tables as well. Text in cells -------------------- Please mind that row and column indexes are starting with 1. .. code-block:: php writeToCell(1, 2, 'text'); // writing text via cell object for row 1 and column 3 $cell = $table->getCell(1, 3); $cell->writeText('text'); Cell formatting -------------------- Cell font ~~~~~~~~~~~~~~~~~~~~ Setting the font for a cell or a cell, cell range, column or row. .. code-block:: php getCell(1, 3); $cell->setFont($fontRed); // set "Times New Roman" with green color to a cell range $fontGreen = new PHPRtfLite_Font(12, 'Times New Roman', '#0f0'); $table->setFontForCellRange($fontGreen, 1, 1, 4, 2); // set "Times New Roman" with blue color to a column $fontBlue = new PHPRtfLite_Font(12, 'Times New Roman', '#00f'); $table->getColumn(1)->setFont($fontBlue); // set "Tahoma" with yellow color to a row $fontYellow = new PHPRtfLite_Font(12, 'Tahoma', '#ff0'); $table->getRow(1)->setFont($fontYellow); Cell alignment ~~~~~~~~~~~~~~~~~~~~ Cells can be aligned horizontal and vertical. Horizontal alignment is also called text alignment. These types are availble as class constants: - ``TEXT_ALIGN_LEFT`` - ``TEXT_ALIGN_RIGHT`` - ``TEXT_ALIGN_CENTER`` - ``TEXT_ALIGN_JUSTIFY`` Vertical alignment types available via class constants are: - ``VERTICAL_ALIGN_TOP`` - ``VERTICAL_ALIGN_BOTTOM`` - ``VERTICAL_ALIGN_CENTER`` .. code-block:: php getCell(1, 3); $cell->setTextAlignment(); Cell padding ~~~~~~~~~~~~~~~~~~~~ Using Microsoft Word top and bottom cell paddings are applied to all cells in a row. .. code-block:: php getCell(1, 3); // cell padding left: 0.2cm $cell->setCellPaddingLeft(0.2); // cell padding right: 0.2cm $cell->setCellPaddingRight(0.2); // cell padding left: 0.4cm $cell->setCellPaddingTop(0.4); // cell padding left: 0.4cm $cell->setCellPaddingBottom(0.4); // or the same in a shorter way $cell->setCellPaddings(0.2, 0.4, 0.2, 0.4); Background color ~~~~~~~~~~~~~~~~ The background color can be set for a single cell or for a range of cells. .. code-block:: php getCell(1, 3); // set background color of cell to red $cell->setBackgroundColor('#FF0000'); // set background color for a cell range (from row 1 column 1 to row 4 column 4) to blue $table->setBackgroundForCellRange('#0000FF', 1, 1, 4, 2); Border formatting ~~~~~~~~~~~~~~~~~ Cell borders can be set for a single cell or for a range of cells. .. code-block:: php getCell(1, 3); // cell with border $cell->setBorder($border); // set border for cell range (from row 1 and column 1 to row 3 and column 2) $table->setBorderForCellRange($border, 1, 1, 3, 2); Read more about creating borders here: :ref:`borders`. Rotate text in cells ~~~~~~~~~~~~~~~~~~~~ Cell text can be rotated to left and right. .. code-block:: php getCell(1, 3); // rotate text in cell to left $cell->rotateTo(PHPRtfLite_Container_Cell::ROTATE_LEFT); // rotate text for a cell range (from row 1, column 2 to row 3, column 4) to right $table->rotateCellRange(PHPRtfLite_Container_Cell::ROTATE_RIGHT, 1, 2, 3, 4) Images -------------------- Images can be added to a table cell. .. code-block:: php getCell(1, 3); // adding image to cell row 1 and column 3 $imageFile = '/path/to/image/example.jpg'; $image = $cell->addImage($imageFile); // image width 3cm and height 3cm $image->setWidth(3); $image->setHeight(3); // the same as short cut $cell->addImageToCell(1, 3, $imageFile, new PHPRtfLite_ParFormat, 3, 3); Adding images to the RTF document is also described in :ref:`images`. Merging a cell range -------------------- Cells can be merged horizontal, vertically and both. .. code-block:: php mergeCellRange(1, 1, 2, 3); Nested tables ================ Nested tables are not supported by OpenOffice 3.2. .. code-block:: php getCell(1, 3); $cell->writeText('Text before nested table'); // nested cell $nestedTable = $cell->addTable(); $nestedTable->addRow(1); $nestedTable->addColumnsList(array(1,2)); $nestedTable->writeToCell(1, 1, 'Text for first nested cell'); $cell->writeText('Text after nested table!');