Paragraphs

As we saw in the previous tutorials, when we want to add some text to the document, we first create a paragraph, then we add the text using the method addText.

We'll now see in more depth what we can do with paragraphs.

Add plain text

As we saw in the previous tutorials that we add text by calling addText:

$p = new Paragraph();
$p->addText('Some text');

Add styled text

We format text using an TextStyle object, as we saw in the last tutorial

$textStyle = new TextStyle();
//do some styling
$p = new Paragraph();
$p->addText('Some text', $textStyle);

Add hyperlinks

We can add hyperlinks to a paragraph using the method addHyperlink($text, $url, $title).

  • $text: The text that will be displayed.
  • $url: The URL for the target location of the link.
  • $text: A short description for hint text.
$p = new Paragraph();
$p->addHyperlink('GO to google', 'http://www.google.com', 'A short description');

Add images

We can also add images to a paragraph, using the method addImage($image, $width, $height).

  • $image: The path to the image.
  • $width: The width of the image.
  • $height: The height of the image.

The dimensions can be expressed with percentage "%" or one og these units ("mm", "cm", "in" (2.54cm), "pt" (1/72in)).

$p = new Paragraph();
$p->addImage('myImage.jpg', '20mm', '20mm');

Line break

We can add a line break by using addLineBreak().

$p = new Paragraph();
$p->addText("First line");
$p->addLineBreak();
$p->addText("Second line");

Bookmarks

Bookmarks are used to mark a text position. To create a bookmark , we use addBookmark($bookmarkName).

$p = new Paragraph();
$p->addBookmark("myBookmark");
$p->addText("This text position is marked with a bookmark");

Next, we need to create a bookmark reference, using addBookmarkRef($bookmarkName, $refText).

//Somewhere else in the document
$p2 = new Paragraph();
$p2->addBookmarkRef('myBookmark', 'Go to myBookmark');

So, when we click in the bookmark reference "Go to myBookmark", we will be sent to the position we marked.

Notes

A note is generally a reference to more information about a word, topic, etc.

There are two types of notes:

  • footnotes: are inserted in the end of the page.
  • endnotes: are inserted in the end of the document.

We add notes using addNote($body, $noteClass).

  • $body: The content of the note.
  • $noteClass: The type of the note, either FOOTNOTE or ENDNOTE. Default is FOOTNOTE
$p = new Paragraph();
$p->addText("This topic need some more information");
$p->addNote('Some more informations about the topic');

Rubies

A ruby is additional text th displayed above or below some base text. The purpose of ruby is to annotate the base text or provide information about its pronunciation.<

We add a ruby using the method addRuby($base, $text, $textRubyStyle).

  • $base: The text that will be annotated.
  • $text: The annotation text.
  • $textRubyStyle: A TextStyle object that will be applied to the annotation text. We will talk about this class in the next tutorial.
$p = new Paragraph();
$p->addRuby('This is some text', 'This is a ruby');
$p->addLineBreak();
$textStyle = new TextStyle();
$textStyle->setColor('#ff0000'); //Red text
$p->addRuby('This is some other text', 'This is another ruby', $textStyle);