PHPWord/samples/Sample_27_Field.php

72 lines
2.4 KiB
PHP
Raw Normal View History

2014-05-28 17:59:44 +02:00
<?php
2022-09-16 11:45:45 +02:00
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();
2022-09-16 11:45:45 +02:00
PhpOffice\PhpWord\Style::addTitleStyle(1, ['size' => 14]);
2014-05-28 17:59:44 +02:00
2016-06-04 20:06:37 +04:00
// New section
2014-05-28 17:59:44 +02:00
$section = $phpWord->addSection();
2018-04-17 07:34:57 +02:00
$section->addTitle('This page demos fields');
2014-05-28 17:59:44 +02:00
// Add Field elements
// See Element/Field.php for all options
2016-06-04 20:06:37 +04:00
$section->addText('Date field:');
2022-09-16 11:45:45 +02:00
$section->addField('DATE', ['dateformat' => 'dddd d MMMM yyyy H:mm:ss'], ['PreserveFormat']);
2018-04-17 07:34:57 +02:00
$section->addText('Style Ref field:');
2022-09-16 11:45:45 +02:00
$section->addField('STYLEREF', ['StyleIdentifier' => 'Heading 1']);
2018-04-17 07:34:57 +02:00
2016-06-04 20:06:37 +04:00
$section->addText('Page field:');
2022-09-16 11:45:45 +02:00
$section->addField('PAGE', ['format' => 'Arabic']);
2016-06-04 20:06:37 +04:00
$section->addText('Number of pages field:');
2022-09-16 11:45:45 +02:00
$section->addField('NUMPAGES', ['numformat' => '0,00', 'format' => 'Arabic'], ['PreserveFormat']);
$section->addTextBreak();
$textrun = $section->addTextRun();
$textrun->addText('An index field is ');
2022-09-16 11:45:45 +02:00
$textrun->addField('XE', [], ['Italic'], 'My first index');
$textrun->addText('here:');
$indexEntryText = new TextRun();
$indexEntryText->addText('My ');
2022-09-16 11:45:45 +02:00
$indexEntryText->addText('bold index', ['bold' => true]);
$indexEntryText->addText(' entry');
$textrun = $section->addTextRun();
$textrun->addText('A complex index field is ');
2022-09-16 11:45:45 +02:00
$textrun->addField('XE', [], ['Bold'], $indexEntryText);
$textrun->addText('here:');
$section->addText('The actual index:');
2022-09-16 11:45:45 +02:00
$section->addField('INDEX', [], ['\\e " "'], 'right click to update the index');
2014-05-28 17:59:44 +02:00
2022-09-16 11:45:45 +02:00
$textrun = $section->addTextRun(['alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER]);
2016-06-04 20:06:37 +04:00
$textrun->addText('This is the date of lunar calendar ');
2022-09-16 11:45:45 +02:00
$textrun->addField('DATE', ['dateformat' => 'd-M-yyyy H:mm:ss'], ['PreserveFormat', 'LunarCalendar']);
2016-06-04 20:06:37 +04:00
$textrun->addText(' written in a textrun.');
$section->addTextBreak();
$macroText = new TextRun();
2022-09-16 11:45:45 +02:00
$macroText->addText('Double click', ['bold' => true]);
$macroText->addText(' to ');
2022-09-16 11:45:45 +02:00
$macroText->addText('zoom to 100%', ['italic' => true]);
$section->addText('A macro button with styled text:');
2022-09-16 11:45:45 +02:00
$section->addField('MACROBUTTON', ['macroname' => 'Zoom100'], [], $macroText);
$section->addTextBreak();
$section->addText('A macro button with simple text:');
2022-09-16 11:45:45 +02:00
$section->addField('MACROBUTTON', ['macroname' => 'Zoom100'], [], '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';
}