tables.txt
5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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
$cell = $table->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
$image->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!');