PHPWord/samples/Sample_27_Field.php

66 lines
2.3 KiB
PHP
Raw Normal View History

2014-05-28 17:59:44 +02:00
<?php
use PhpOffice\PhpWord\Element\TextRun;
2014-05-28 17:59:44 +02:00
include_once 'Sample_Header.php';
// New Word document
2015-01-01 20:41:42 +04:00
echo date('H:i:s'), ' Create new PhpWord object', EOL;
2014-05-28 17:59:44 +02:00
$phpWord = new \PhpOffice\PhpWord\PhpWord();
2016-06-04 20:06:37 +04:00
// New section
2014-05-28 17:59:44 +02:00
$section = $phpWord->addSection();
// Add Field elements
// See Element/Field.php for all options
2016-06-04 20:06:37 +04:00
$section->addText('Date field:');
2015-01-01 20:41:42 +04:00
$section->addField('DATE', array('dateformat' => 'dddd d MMMM yyyy H:mm:ss'), array('PreserveFormat'));
2016-06-04 20:06:37 +04:00
$section->addText('Page field:');
$section->addField('PAGE', array('format' => 'Arabic'));
2016-06-04 20:06:37 +04:00
$section->addText('Number of pages field:');
$section->addField('NUMPAGES', array('numformat' => '0,00', 'format' => 'Arabic'), array('PreserveFormat'));
$section->addTextBreak();
$textrun = $section->addTextRun();
$textrun->addText('An index field is ');
$textrun->addField('XE', array(), array('Italic'), 'My first index');
$textrun->addText('here:');
$indexEntryText = new TextRun();
$indexEntryText->addText('My ');
2017-09-18 22:35:27 +02:00
$indexEntryText->addText('bold index', array('bold' => true));
$indexEntryText->addText(' entry');
$textrun = $section->addTextRun();
$textrun->addText('A complex index field is ');
$textrun->addField('XE', array(), array('Bold'), $indexEntryText);
$textrun->addText('here:');
$section->addText('The actual index:');
$section->addField('INDEX', array(), array('\\e " "'), 'right click to update the index');
2014-05-28 17:59:44 +02:00
2015-10-10 19:22:19 +04:00
$textrun = $section->addTextRun(array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER));
2016-06-04 20:06:37 +04:00
$textrun->addText('This is the date of lunar calendar ');
2015-01-01 20:41:42 +04:00
$textrun->addField('DATE', array('dateformat' => 'd-M-yyyy H:mm:ss'), array('PreserveFormat', 'LunarCalendar'));
2016-06-04 20:06:37 +04:00
$textrun->addText(' written in a textrun.');
$section->addTextBreak();
$macroText = new TextRun();
$macroText->addText('Double click', array('bold' => true));
$macroText->addText(' to ');
$macroText->addText('zoom to 100%', array('italic' => true));
$section->addText('A macro button with styled text:');
$section->addField('MACROBUTTON', array('macroname' => 'Zoom100'), array(), $macroText);
$section->addTextBreak();
$section->addText('A macro button with simple text:');
$section->addField('MACROBUTTON', array('macroname' => 'Zoom100'), array(), 'double click to zoom');
2014-05-28 17:59:44 +02:00
// Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers);
if (!CLI) {
include_once 'Sample_Footer.php';
}