@@ -15,8 +15,14 @@ folder <https://github.com/PHPOffice/PHPWord/tree/master/samples/>`__.
1515 <?php
1616 require_once 'bootstrap.php';
1717
18+ use PhpOffice\PhpWord\IOFactory;
19+ use PhpOffice\PhpWord\PhpWord;
20+ use PhpOffice\PhpWord\Style\Font;
21+ use PhpOffice\PhpWord\Style\Colors\Hex;
22+ use PhpOffice\PhpWord\Style\Lengths\Absolute;
23+
1824 // Creating the new document...
19- $phpWord = new \PhpOffice\PhpWord\ PhpWord();
25+ $phpWord = new PhpWord();
2026
2127 /* Note: any element you append to a document must reside inside of a Section. */
2228
@@ -41,14 +47,14 @@ folder <https://github.com/PHPOffice/PHPWord/tree/master/samples/>`__.
4147 '"Great achievement is usually born of great sacrifice, '
4248 . 'and is never the result of selfishness." '
4349 . '(Napoleon Hill)',
44- array('name' => 'Tahoma', 'size' => 10 )
50+ array('name' => 'Tahoma', 'size' => Absolute::from('pt', 10) )
4551 );
4652
4753 // Adding Text element with font customized using named font style...
4854 $fontStyleName = 'oneUserDefinedStyle';
4955 $phpWord->addFontStyle(
5056 $fontStyleName,
51- array('name' => 'Tahoma', 'size' => 10 , 'color' => '1B2232', 'bold' => true)
57+ array('name' => 'Tahoma', 'size' => Absolute::from('pt', 10) , 'color' => new Hex( '1B2232') , 'bold' => true)
5258 );
5359 $section->addText(
5460 '"The greatest accomplishment is not in never falling, '
@@ -58,23 +64,23 @@ folder <https://github.com/PHPOffice/PHPWord/tree/master/samples/>`__.
5864 );
5965
6066 // Adding Text element with font customized using explicitly created font style object...
61- $fontStyle = new \PhpOffice\PhpWord\Style\ Font();
67+ $fontStyle = new Font();
6268 $fontStyle->setBold(true);
6369 $fontStyle->setName('Tahoma');
64- $fontStyle->setSize(13 );
70+ $fontStyle->setSize(Absolute::from('pt', 13) );
6571 $myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
6672 $myTextElement->setFontStyle($fontStyle);
6773
6874 // Saving the document as OOXML file...
69- $objWriter = \PhpOffice\PhpWord\ IOFactory::createWriter($phpWord, 'Word2007');
75+ $objWriter = IOFactory::createWriter($phpWord, 'Word2007');
7076 $objWriter->save('helloWorld.docx');
7177
7278 // Saving the document as ODF file...
73- $objWriter = \PhpOffice\PhpWord\ IOFactory::createWriter($phpWord, 'ODText');
79+ $objWriter = IOFactory::createWriter($phpWord, 'ODText');
7480 $objWriter->save('helloWorld.odt');
7581
7682 // Saving the document as HTML file...
77- $objWriter = \PhpOffice\PhpWord\ IOFactory::createWriter($phpWord, 'HTML');
83+ $objWriter = IOFactory::createWriter($phpWord, 'HTML');
7884 $objWriter->save('helloWorld.html');
7985
8086 /* Note: we skip RTF, because it's not XML-based and requires a different example. */
@@ -138,8 +144,10 @@ default font by using the following two functions:
138144
139145.. code-block :: php
140146
147+ use PhpOffice\PhpWord\Style\Lengths\Absolute;
148+
141149 $phpWord->setDefaultFontName('Times New Roman');
142- $phpWord->setDefaultFontSize(12 );
150+ $phpWord->setDefaultFontSize(Absolute::from('pt', 12) );
143151
144152 Document settings
145153-----------------
@@ -251,25 +259,34 @@ name. Use the following functions:
251259 Measurement units
252260-----------------
253261
254- The base length unit in Open Office XML is twip. Twip means "TWentieth
255- of an Inch Point", i.e. 1 twip = 1/1440 inch.
262+ While Open Office XML uses twentieths of a point ("twips" or 1/1440 inch)
263+ for most of its measurements,
264+ all lengths in PHPWord use an instance of ``\PhpOffice\PhpWord\Style\Lengths\Length ``.
265+ Most measurements are limited to absolute units
266+ (rather than percent or "auto" values),
267+ so those use ``\PhpOffice\PhpWord\Style\Lengths\Absolute `` exclusively.
268+ Methods are typehinted to ensure the right measurement is provided.
256269
257- You can use PHPWord helper functions to convert inches, centimeters, or
258- points to twip.
270+ You can use ``\PhpOffice\PhpWord\Style\Lengths\Absolute ``
271+ to specify lengths in units you're comfortable with (eg, "pt"),
272+ and PHPWord will automatically convert those lengths
273+ to the appropriate ones for the format being exported to.
259274
260275.. code-block :: php
261276
277+ use PhpOffice\PhpWord\Style\Lengths\Absolute;
278+
262279 // Paragraph with 6 points space after
263280 $phpWord->addParagraphStyle('My Style', array(
264- 'spaceAfter' => \PhpOffice\PhpWord\Shared\Converter::pointToTwip(6) )
265- );
281+ 'spaceAfter' => Absolute::from('pt', 6 )
282+ )) ;
266283
267284 $section = $phpWord->addSection();
268285 $sectionStyle = $section->getStyle();
269286 // half inch left margin
270- $sectionStyle->setMarginLeft(\PhpOffice\PhpWord\Shared\Converter::inchToTwip( .5));
287+ $sectionStyle->setMarginLeft(Absolute::from('in', .5));
271288 // 2 cm right margin
272- $sectionStyle->setMarginRight(\PhpOffice\PhpWord\Shared\Converter::cmToTwip( 2));
289+ $sectionStyle->setMarginRight(Absolute::from('cm', 2));
273290
274291 Document protection
275292-------------------
@@ -317,12 +334,14 @@ There is no limit if the option is not set or the provided value is ``0``.
317334 Hyphenation Zone
318335~~~~~~~~~~~~~~~~
319336
320- The hyphenation zone (in * twip *) is the allowed amount of whitespace before hyphenation is applied.
337+ The hyphenation zone is the allowed amount of whitespace before hyphenation is applied.
321338The smaller the hyphenation zone the more words are hyphenated. Or in other words, the wider the hyphenation zone the less words are hyphenated.
322339
323340.. code-block :: php
324341
325- $phpWord->getSettings()->setHyphenationZone(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(1));
342+ use PhpOffice\PhpWord\Style\Lengths\Absolute;
343+
344+ $phpWord->getSettings()->setHyphenationZone(Absolute::from('cm', 1));
326345
327346 Hyphenate Caps
328347~~~~~~~~~~~~~~
0 commit comments