/** * Sets border to a cell * * @param PHPRtfLite_Border $border */ public function setBorder(PHPRtfLite_Border $border) { $borderFormatTop = $border->getBorderTop(); $borderFormatBottom = $border->getBorderBottom(); $borderFormatLeft = $border->getBorderLeft(); $borderFormatRight = $border->getBorderRight(); if (!$this->_border) { $this->_border = new PHPRtfLite_Border(); } if ($borderFormatLeft) { $this->_border->setBorderLeft($borderFormatLeft); } if ($borderFormatRight) { $this->_border->setBorderRight($borderFormatRight); } if ($borderFormatTop) { $this->_border->setBorderTop($borderFormatTop); } if ($borderFormatBottom) { $this->_border->setBorderBottom($borderFormatBottom); } if ($borderFormatTop && $this->_table->checkIfCellExists($this->_rowIndex - 1, $this->_columnIndex)) { $cell = $this->_table->getCell($this->_rowIndex - 1, $this->_columnIndex); $borderBottom = $cell->getBorder(); if ($borderBottom == null) { $borderBottom = new PHPRtfLite_Border(); } $borderBottom->setBorderBottom($borderFormatTop); $cell->setCellBorder($borderBottom); } if ($borderFormatBottom && $this->_table->checkIfCellExists($this->_rowIndex + 1, $this->_columnIndex)) { $cell = $this->_table->getCell($this->_rowIndex + 1, $this->_columnIndex); $borderTop = $cell->getBorder(); if ($borderTop == null) { $borderTop = new PHPRtfLite_Border(); } $borderTop->setBorderTop($borderFormatBottom); $cell->setCellBorder($borderTop); } if ($borderFormatLeft && $this->_table->checkIfCellExists($this->_rowIndex, $this->_columnIndex - 1)) { $cell = $this->_table->getCell($this->_rowIndex, $this->_columnIndex - 1); $borderRight = $cell->getBorder(); if ($borderRight == null) { $borderRight = new PHPRtfLite_Border(); } $borderRight->setBorderRight($borderFormatLeft); $cell->setCellBorder($borderRight); } if ($borderFormatRight && $this->_table->checkIfCellExists($this->_rowIndex, $this->_columnIndex + 1)) { $cell = $this->_table->getCell($this->_rowIndex, $this->_columnIndex + 1); $borderLeft = $cell->getBorder(); if ($borderLeft == null) { $borderLeft = new PHPRtfLite_Border(); } $borderLeft->setBorderLeft($borderFormatRight); $cell->setCellBorder($borderLeft); } }
/** * Sets borders to rtf document. * * @param PHPRtfLite_Border_Format $borderFormat * @param boolean $left * @param boolean $top * @param boolean $right * @param boolean $bottom */ public function setBorders(PHPRtfLite_Border_Format $borderFormat, $left = true, $top = true, $right = true, $bottom = true) { if ($this->_border === null) { $this->_border = new PHPRtfLite_Border(); } if ($left) { $this->_border->setBorderLeft($borderFormat); } if ($top) { $this->_border->setBorderTop($borderFormat); } if ($right) { $this->_border->setBorderRight($borderFormat); } if ($bottom) { $this->_border->setBorderBottom($borderFormat); } }
$table->writeToCell(1, 1, '<br> Sample borders', new PHPRtfLite_Font(), new PHPRtfLite_ParFormat()); $cell = $table->getCell(1, 2); $cell->writeText('<br> ', new PHPRtfLite_Font(), new PHPRtfLite_ParFormat()); $img = $cell->addImage($dir . '/sources/cats.jpg', null); $border = PHPRtfLite_Border::create($rtf, 3, '#000000'); $img->setBorder($border); $table->writeToCell(2, 1, '<br> Borders with space', new PHPRtfLite_Font(), new PHPRtfLite_ParFormat()); $cell = $table->getCell(2, 2); $img = $cell->addImage($dir . '/sources/cats.jpg', null); $borderFormatBlue = new PHPRtfLite_Border_Format(2, '#0000ff', 'simple', 0.5); $borderFormatRed = new PHPRtfLite_Border_Format(2, '#ff0000', 'simple', 0.5); $border = new PHPRtfLite_Border($rtf); $border->setBorderLeft($borderFormatRed); $border->setBorderTop($borderFormatBlue); $border->setBorderRight($borderFormatRed); $border->setBorderBottom($borderFormatBlue); $img->setBorder($border); $table->writeToCell(3, 1, '<br> Image centered by ParFormat', new PHPRtfLite_Font()); $cell = $table->getCell(3, 2); $parFormatCenter = new PHPRtfLite_ParFormat(PHPRtfLite_ParFormat::TEXT_ALIGN_CENTER); $img = $cell->addImage($dir . '/sources/cats.jpg', $parFormatCenter); $table->writeToCell(4, 1, '<br> Image centered horizontal and vertically by cell', new PHPRtfLite_Font()); $cell = $table->getCell(4, 2); $cell->setTextAlignment(PHPRtfLite_Table_Cell::TEXT_ALIGN_CENTER); $cell->setVerticalAlignment(PHPRtfLite_Table_Cell::VERTICAL_ALIGN_CENTER); $img = $cell->addImage($dir . '/sources/cats.jpg'); $sect->writeText('<b>Images in paragraph</b><br><br>', new PHPRtfLite_Font(), $parGreyLeft); $img = $sect->addImage($dir . '/sources/html.png', $parGreyCenter); $img->setWidth(1.5); // save rtf document $rtf->save($dir . '/generated/' . basename(__FILE__, '.php') . '.rtf');
$table = $sect->addTable(); $table->addRows($rowCount, $rowHeight); $table->addColumnsList(array_fill(0, $columnCount, $columnWidth)); for ($rowIndex = 1; $rowIndex <= $rowCount; $rowIndex++) { for ($columnIndex = 1; $columnIndex <= $columnCount; $columnIndex++) { $cell = $table->getCell($rowIndex, $columnIndex); $cell->writeText("Cell {$rowIndex}:{$columnIndex}"); $cell->setTextAlignment(PHPRtfLite_Table_Cell::TEXT_ALIGN_CENTER); $cell->setVerticalAlignment(PHPRtfLite_Table_Cell::VERTICAL_ALIGN_CENTER); } } $borderTop = new PHPRtfLite_Border($rtf); $borderTop->setBorderTop(new PHPRtfLite_Border_Format(2, '#f33')); $table->setBorderForCellRange($borderTop, 1, 1, 1, $columnCount); $borderBottom = new PHPRtfLite_Border($rtf); $borderBottom->setBorderBottom(new PHPRtfLite_Border_Format(2, '#33f')); $table->setBorderForCellRange($borderBottom, $rowCount, 1, $rowCount, $columnCount); $sect->writeText('Table with cell paddings (Using Microsoft Word top and bottom cell paddings are applied to all cells in a row)'); $table = $sect->addTable(); $table->addRows(4); $table->addColumnsList(array_fill(0, 4, $columnWidth)); $cell = $table->getCell(1, 1); $cell->writeText('cell 1:1 (with left padding)'); $cell->setPaddingLeft(1); $cell = $table->getCell(2, 2); $cell->writeText('cell 2:2 (with right padding)'); $cell->setPaddingRight(1); $cell = $table->getCell(3, 3); $cell->writeText('cell 3:3 (with top padding)'); $cell->setPaddingTop(1);