Tables in Word Documents with PHP

Tables in Word Documents with PHP

Add or delete a table, add a row or a column, extract data from a table, - all this and much more you can do with Word documents in PHP with Cloud API.

If you have worked in Microsoft Word, then you understand the main concept of working with tables to create text and graphics or showing data in a tabular style (similar to a spreadsheet).
In a standard document page text, graphics and other content appear in a free-flowing way. The text and images wrap, and one item appears after the next. There is no way to control where items appear in the document, and this is where tables become useful. You can use a table to structure content, place text or images in one cell and align it accordingly. Furthermore, tables can be nested inside other tables up to any depth with PHP API.

Note: As a prerequisite, you need PHP and Words Cloud PHP library installed on your computer.

Creating and Formatting Tables with Word

A table is made up of rows and columns. The intersection of a row and column is called a cell.
You can use Cloud API in PHP to create a table, format it, apply a style, modify a table in code, insert data into rows and cells.
A table is imported as a Table node from any document loaded into the Cloud Storage. Aspose.Words.Tables namespace contains classes that represent tables, rows, cells and their formatting.

Table manipulation in PHP

You can perform the following tasks with tables in DOCX, DOC, RTF and document file formats using PHP:

  • Add or delete a table.
  • Resize a table.
  • Change columns width in a table.
  • Change rows height in a table.
  • Convert a table to text.
  • Text wrap to surround a picture or a diagram with text.
  • Add rows or columns to a Table.
  • Delete rows or columns in a Table.
  • Change properties of a table, a row or a cell.

Extract data from tables using PHP

PHP API allows you to extract text from tables. You can call API methods to get a row or to get a cell from a table.

Working with borders and margins

You can set border style, margins and cell padding in tables with PHP API. You can use border property to set a border for a table. You can apply borders to a table, to each cell or to a single cell.
Cell margin represents the number of blank spaces between the content of a cell and the border of a cell. Usually, all cells have the same margin in the table.
Working with margins, give you the right way to control spacing in a Word table.

Table properties with PHP API

A table has the following properties:

AutoFit

Resize a table, a column or a row using AutoFit option.
Turn on AutoFit to adjust tables or columns to fit the size of your content automatically. Otherwise, turn it off for Fixed Column Width option.

Alignment

You can align a table between page margins either right, left, or center with PHP API.

Merge a table

PHP API allows you to merge two or more tables into one larger table. After merging tables, you can also merge specific cells within the table.

Split a table

It allows you to split a table into two or more tables. It creates smaller tables or adds text between two tables.

How to work with tables in a DOCX document in PHP

  1. Go to the Aspose Cloud Dashboard.
  2. Create a new Account to access all applications and services or Sign In to your account.
  3. Click on My Apps tab.
  4. Get App Key and App SID.
  5. Check API Reference page for the description of APIs parameters.
  6. Install PHP SDK library via composer.
  7. Check Developer Guide to work with tables in a DOCX file in PHP.

Working with tables in a Word document in PHP
use Aspose\Words\Model\Requests;
use Aspose\Words\WordsApi;

$appSid = "####-####-####-####-####";
$appKey = "##################";
$wordsApi = new WordsApi($appSid, $appKey);

// Add a table
$table = new Model\TableInsert(array(
	"columns_count" => 3,
	"rows_count" => 5));
$request = new Requests\InsertTableRequest("sample.docx", $table, "sections/0");
$wordsApi->insertTable($request);

// Extract data from table
$index = 0;
$request = new Requests\GetTableWithoutNodePathRequest("sample.docx", $index);
$data = $wordsApi->getTableWithoutNodePath($request);
echo $data;

// Edit a cell at row 2, column 3
$paragraph = new Model\ParagraphInsert(array(
	"text" => "Lorem Ipsum"
));
$node_path = "sections/0/tables/0/rows/1/cells/2";
$request = new Requests\InsertParagraphRequest("sample.docx", $paragraph, $node_path);
$wordsApi->insertParagraph($request);