Tables
****************

PHPRtfLite supports tables and nested tables.

.. code-block:: php

    <?php
    // some bootstraping here
   
    $section = $rtf->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

    <?php
    // writing text to a cell for row 1 and column 2
    $table->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

    <?php
    // set "Arial" with red color to a single cell
    $fontRed = new PHPRtfLite_Font(12, 'Arial', '#f00');
    $cell = $table->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

    <?php
    $cell = $table->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

    <?php
    $cell = $table->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

    <?php
    $cell = $table->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

    <?php
    $border = new PHPRtfLite_Border(
        $rtf,                                       // PHPRtfLite instance
        new PHPRtfLite_Border_Format(2, '#00FF00'), // left border: 2pt, green color
        new PHPRtfLite_Border_Format(1, '#FFFF00'), // top border: 1pt, yellow color   
        new PHPRtfLite_Border_Format(2, '#FF0000'), // right border: 2pt, red color
        new PHPRtfLite_Border_Format(1, '#0000FF')  // bottom border: 1pt, blue color
    );
    $cell = $table->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

    <?php
    $cell = $table->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

    <?php
    // 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
    $table->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

    <?php
    // merge cells from row 1 column 1 to row 2 and column 3
    $table->mergeCellRange(1, 1, 2, 3);

    
Nested tables
================

Nested tables are not supported by OpenOffice 3.2.

.. code-block:: php

    <?php
    $cell = $table->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!');
