PHPWord/samples/Sample_04_Textrun.php

51 lines
1.9 KiB
PHP
Raw Permalink Normal View History

2014-03-04 10:54:14 -07:00
<?php
2022-09-16 11:45:45 +02:00
2014-03-15 22:39:57 +07:00
include_once 'Sample_Header.php';
2014-03-04 10:54:14 -07:00
// New Word Document
2015-01-01 20:41:42 +04:00
echo date('H:i:s'), ' Create new PhpWord object', EOL;
$phpWord = new \PhpOffice\PhpWord\PhpWord();
2014-03-04 10:54:14 -07:00
2016-06-04 20:06:37 +04:00
// Define styles
$paragraphStyleName = 'pStyle';
2022-09-16 11:45:45 +02:00
$phpWord->addParagraphStyle($paragraphStyleName, ['spacing' => 100]);
2016-06-04 20:06:37 +04:00
$boldFontStyleName = 'BoldText';
2022-09-16 11:45:45 +02:00
$phpWord->addFontStyle($boldFontStyleName, ['bold' => true]);
2016-06-04 20:06:37 +04:00
$coloredFontStyleName = 'ColoredText';
2022-09-16 11:45:45 +02:00
$phpWord->addFontStyle($coloredFontStyleName, ['color' => 'FF8080', 'bgColor' => 'FFFFCC']);
2016-06-04 20:06:37 +04:00
$linkFontStyleName = 'NLink';
2022-09-16 11:45:45 +02:00
$phpWord->addLinkStyle($linkFontStyleName, ['color' => '0000FF', 'underline' => \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE]);
2014-03-04 10:54:14 -07:00
// New portrait section
$section = $phpWord->addSection();
2014-03-04 10:54:14 -07:00
// Add text run
2016-06-04 20:06:37 +04:00
$textrun = $section->addTextRun($paragraphStyleName);
$textrun->addText('Each textrun can contain native text, link elements or an image.');
$textrun->addText(' No break is placed after adding an element.', $boldFontStyleName);
$textrun->addText(' Both ');
2022-09-16 11:45:45 +02:00
$textrun->addText('superscript', ['superScript' => true]);
2016-06-04 20:06:37 +04:00
$textrun->addText(' and ');
2022-09-16 11:45:45 +02:00
$textrun->addText('subscript', ['subScript' => true]);
2016-06-04 20:06:37 +04:00
$textrun->addText(' are also available.');
$textrun->addText(' All elements are placed inside a paragraph with the optionally given paragraph style.', $coloredFontStyleName);
$textrun->addText(' Sample Link: ');
$textrun->addLink('https://github.com/PHPOffice/PHPWord', 'PHPWord on GitHub', $linkFontStyleName);
$textrun->addText(' Sample Image: ');
2022-09-16 11:45:45 +02:00
$textrun->addImage('resources/_earth.jpg', ['width' => 18, 'height' => 18]);
2016-06-04 20:06:37 +04:00
$textrun->addText(' Sample Object: ');
$textrun->addObject('resources/_sheet.xls');
2016-06-04 20:06:37 +04:00
$textrun->addText(' Here is some more text. ');
2014-03-04 10:54:14 -07:00
$textrun = $section->addTextRun();
2022-09-16 11:45:45 +02:00
$textrun->addText('This text is not visible.', ['hidden' => true]);
// Save file
2014-04-12 10:12:24 +07:00
echo write($phpWord, basename(__FILE__, '.php'), $writers);
if (!CLI) {
include_once 'Sample_Footer.php';
}