The most important part of any documents is usually its text. Text can be introduced in Docxpresso in quite a few different ways but the most versatile is, without doubt, with the text method.
Although one may use the text method upon many different elements we always recommend to do it with paragraphs. Indeed Docxpresso always do so internally, if you try to insert directly text in the document object or in a table cell, just to give a couple of examples, Docxpresso will create the corresponding paragraph wrapper following pre-established patterns. This will render the expected results 99% of the time but if you do not want to get unexpected results once in a while is much better that you keep full control.
The Public API of the text method is summarized as follows:
Signature
public text ([$options])
Parameters
- $options (type: array). This array has the following available keys and values:
- text (type: string). The run of text that we wan to insert.
- style (type: string). A string of styles in CSS format.
The relevant CSS style type styles that one may apply to a run of text include:
- Background:
- background-color: sets the text background color.
- Font:
- font: shorthand for font properties.
- font-family: sets the font to be used.
- font-size: sets the font size.
- font-style: sets the font style.
- font-variant: sets the font variant.
- font-weight: specifies how thick the characters should be displayed.
- font-kerning: specifies the kerning information.
- letter-spacing: increases or decreases the separation among characters in the text.
- color: sets the font color.
- Other:
- hyphens: specifies if hyphens are or are not used.
- text-decoration: specifies the decoration added to the text.
- text-decoration-color: sets the color of the text decoration.
- text-decoration-line: specifices the line type used for the text decoration.
- text-decoration-style: specifies how the text decoration line will be displayed.
- text-shadow: applies a shadow to the text.
- text-transform: specifies the capitalization of text.
Let us construct an example that illustrates how all of this works in practice:
<?php /** * This sample script inserts a few runs of text with different formats */ require_once 'pathToDOCXPRESSO/CreateDocument.inc'; $doc = new Docxpresso\CreateDocument(); $format = '.pdf';//.pdf, .doc, .docx, .odt, .rtf //insert a few runs of text $doc->paragraph() ->text(array('text' => 'A paragraph with some ')) ->text(array('text' => 'bold text ', 'style' => 'font-weight: bold')) ->text(array('text' => 'and some italics.', 'style' => 'font-style: italic')) ->text(array('text' => ' We finish with a text in red.', 'style' => 'color: red')); //include in the render method the path where you want your document to be saved $doc->render('text' . $format); //echo a link to the generated document echo 'You may download the generated document from the link below:<br/>'; echo '<a href="' . 'text' . $format . '">Download document</a>';
You will also find all over the documentation many other multiple examples of the use of the text method in different practical situations.