Styled Hello world

In the previous tutorial, we created a paragraph with some plain text, now, we will style it a little.

Till now, we saw two classes:

  • ODT: Create the needed xml files required for a valid odt document.
  • Paragraph: Represents a paragraph in the document.

We will introduce a new class: TextStyle, which let us change the text properties, such as color, size, alignment, etc.

First, as always, include the file phpodt.php, and create an instance of the ODT class.

include 'phpodt-0.3/phpodt.php';

$odt = ODT::getInstance();

Before we create a paragraph, we first create a TextStyle object, specify the changes that we want to make, then pass the created TextStyle object to the method addText of the class Paragraph.

$textStyle = new TextStyle('textstyle1'); //You can omit the name, but it's better to specify one
$textStyle->setColor('#ff0000');   //Change the color of the text to red. The color must be in hexadecimal notation
$textStyle->setBold();      //Make the text bold
$textStyle->setFontSize('15px');    //Change the font size
//....

Next, we create our paragraph, and we call addText with the text we want to insert, and the styles we want to apply to that text.

$styledParagraph = new Paragraph();
$styledParagraph->addText('Hello World!! with style.', $textStyle);

Finally, we write the document.

$odt->output('helloworldstyle.odt');

Full example

include 'phpodt-0.3/phpodt.php';

$odt = ODT::getInstance();

$textStyle = new TextStyle('textstyle1');   //You can the name, but it's better to specify one
$textStyle->setColor('#ff6600');            //Change the color of the text to red. The color must be in hexa  notation
$textStyle->setBold();                      //Make the text bold
$textStyle->setFontSize(15);                //Change the font size

$styledParagraph = new Paragraph();
$styledParagraph->addText('Hello World!! with style.', $textStyle);

$odt->output('helloworldstyle.odt');