Tables

This document is about how to set up and use tables. A table lives inside a story — like text — and consists of cells. Its look is controlled by table styles and cell styles, see also styling text.

Table of contents

Cells

A cell holds content and spans a defined number of rows and columns. Its content is a paragraph style range, like in a story:

<?php

use BitAndBlack\IdmlCreator\Content\Story\CharacterStyleRange;
use BitAndBlack\IdmlCreator\Content\Story\ParagraphStyleRange;
use BitAndBlack\IdmlCreator\Content\Story\Text;
use BitAndBlack\IdmlCreator\Content\Table\Cell;

$cell = new Cell(
    1,
    1,
    new ParagraphStyleRange(
        null,
        new CharacterStyleRange(
            null,
            new Text('Cell content')
        )
    )
);

The first two parameters are the row span and the column span — both 1 for a regular cell. A cell has its own insets, fill color and borders:

<?php

use BitAndBlack\IdmlCreator\Content\Enum\Unit;

/** @var \BitAndBlack\IdmlCreator\Content\Table\Cell $cell the cell created above */
/** @var \BitAndBlack\IdmlCreator\Content\Color\Color $blue a color, created in the "colors" chapter */
/** @var \BitAndBlack\IdmlCreator\Content\Style\CellStyle $cellStyle a cell style, set up in the last section of this chapter */
$cell
    ->setFillColor($blue)
    ->setLeftInset(2, Unit::MILLIMETERS)
    ->setRightInset(2, Unit::MILLIMETERS)
    ->setAppliedCellStyle($cellStyle)
;

The table

A table is created with a number of columns and rows, sizes, and the cells themselves:

<?php

use BitAndBlack\IdmlCreator\Content\Enum\Unit;
use BitAndBlack\IdmlCreator\Content\Table\Table;

$table = new Table();
/** @var \BitAndBlack\IdmlCreator\Content\Table\Cell $cell1 the first cell, created like in the "Cells" section */
/** @var \BitAndBlack\IdmlCreator\Content\Table\Cell $cell2 the second cell */
/** @var \BitAndBlack\IdmlCreator\Content\Table\Cell $cell3 the third cell */
/** @var \BitAndBlack\IdmlCreator\Content\Table\Cell $cell4 the fourth cell */
$table
    ->setColumnCount(2)
    ->setColumnsWidths([80, 80], Unit::MILLIMETERS)
    ->setBodyRowsHeights([50, 50], Unit::MILLIMETERS)
    ->setHeaderRowCount(1)
    ->setBodyRowCount(1)
    ->setFooterRowCount(1)
    ->addCell(
        $cell1,
        $cell2,
        $cell3,
        $cell4
    )
;

The cells are filled row by row. A table can also be formatted directly — its borders, alternating row fills and header handling are all exposed as setters:

<?php

/** @var \BitAndBlack\IdmlCreator\Content\Table\Table $table the table created above */
/** @var \BitAndBlack\IdmlCreator\Content\Color\Color $lightBlue a color, created in the "colors" chapter */
/** @var \BitAndBlack\IdmlCreator\Content\Style\TableStyle $tableStyle a table style, set up in the last section of this chapter */
$table
    ->setTopBorderStrokeWeight(2)
    ->setStartRowFillColor($lightBlue)
    ->setAppliedTableStyle($tableStyle)
;

Table into the document

A table is placed inside a story and from there into a text frame:

<?php

use BitAndBlack\IdmlCreator\Content\Story\CharacterStyleRange;
use BitAndBlack\IdmlCreator\Content\Story\ParagraphStyleRange;
use BitAndBlack\IdmlCreator\Factory;

/** @var \BitAndBlack\IdmlCreator\Content\Table\Table $table the table created in the "The table" section above */
$factory = new Factory();

$story = $factory->createStory()
    ->addContent(
        new ParagraphStyleRange(
            null,
            new CharacterStyleRange(
                null,
                $table
            )
        )
    )
;

/** @var \BitAndBlack\IdmlCreator\Content\TextFrame\TextFrame $textFrame a text frame, created in the "text frames" chapter */
$textFrame->setParentStory($story);

Table styles and cell styles

A table style bundles the look of a table, a cell style the look of a cell — including the paragraph style for the text inside:

<?php

/** @var \BitAndBlack\IdmlCreator\Factory $factory the factory, initialized in the "Table into the document" section */
/** @var \BitAndBlack\IdmlCreator\Content\Style\ParagraphStyle $paragraphStyle a paragraph style, created in the "styling text" chapter */
$cellStyle = $factory->createCellStyle('Cell')
    ->setAppliedParagraphStyle($paragraphStyle)
;

$tableStyle = $factory->createTableStyle('Table')
    ->setBodyRegionCellStyle($cellStyle)
;

/** @var \BitAndBlack\IdmlCreator\Content\Table\Table $table the table created above */
$table->setAppliedTableStyle($tableStyle);