Forum


Replies: 3   Views: 3816
Trying to use custom paragraph styles as headings included in table of contents
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by account33640  · 18-01-2016 - 19:50

I don't know if I am misunderstanding the array $legend component for addTableContents or misusing the createParagraphStyle methods (or whatever else I have wrong).  The result is that custom paragraph styles with an orange font intended to be used as headings (and therefore appear in the table of contents) get created at 1/2 the intended font size, and only one of my two headings gets included into the table of contents.  My custom styles never show in Word editor.  Using trial version & MS Word for Mac 2011.  My test script is:

<?php
// Custom Style Headings with Table of Contents          
require_once 'classes/CreateDocx.inc';

define ('ORANGE', 'E06E1E');

// new report object
$docx = new CreateDocx();

// 2 Custom Heading Styles and 1 for normal text
$style1 = array('outlineLvl' => 1,
                    'jc' => 'left', 
                    'spacingBottom' => 100, 
                    'font' => 'Times New Roman', 
                    'bold' => true, 
                    'sz' => 16,
                    'color' => ORANGE);
$docx->createParagraphStyle('style1', $style1);

$style2 = array('outlineLvl' => 2,
                    'jc' => 'left', 
                    'spacingBottom' => 100, 
                    'font' => 'Times New Roman', 
                    'bold' => true, 
                    'sz' => 13,
                    'color' => ORANGE);
$docx->createParagraphStyle('style2', $style2);

$pLeftStyle = array('jc' => 'left', 'spacingBottom' => 100);
$docx->createParagraphStyle('pLeft', $pLeftStyle);

// Table of Contents

$tocOptions = array('autoUpdate' => false, 'displayLevels' => '1-2');
$tocLegend = array('text' => 'Update this Table of Contents using <control> click -> Update Field',
                    'pStyle' => 'pLeft',
                    'font' => 'Times New Roman');
                    
$docx->addText('Table of Contents', array('bold' => true, 'fontSize' => '14'));
$docx->addTableContents($tocOptions, $tocLegend);

// New Headings to include in the Table of Contents - 2 levels
$docx->addBreak(array('type'=>'page'));
$docx->addText('First Heading1', array('pStyle' => 'style1'));
$docx->addText('First Heading2', array('pStyle' => 'style2'));

// Paragraph 1
$text =  'This is the first paragraph.';
$docx->addText($text, $pLeft);

$docx->createDocx('trytoc');  

Posted by admin  · 19-01-2016 - 09:01

Hello,

We recommend you to use the fontSize option when you add the text or duplicate the value of sz. This is:

$style1 = array('outlineLvl' => 1,
                    'jc' => 'left', 
                    'spacingBottom' => 100, 
                    'font' => 'Times New Roman', 
                    'bold' => true, 
                    'sz' => 32,
                    'color' => ORANGE);

or

$docx->addText('First Heading1', array('pStyle' => 'style1', 'fontSize' => '16'));

About displayLeves, please use 1-3 to show them:

$tocOptions = array('autoUpdate' => false, 'displayLevels' => '1-3');

Some default Word styles may be showing and they need to be set in this way.

About this chunk of code:

$text =  'This is the first paragraph.';
$docx->addText($text, $pLeft);

the $pLeft variable isn't declared.

Regards.

Posted by account33640  · 19-01-2016 - 15:30

Thanks!  These changes did solve my formatting challenge.  

I do note that the paragraph styles created above never show in Word when editing the document.  I haven't been successful trying to importStyles from a template document with 'replace'  specified either.  I will presume this has something to do with trial version style defaults that seem to be named 'xx PHPDOCX'. 

Posted by admin  · 19-01-2016 - 15:38

Hello,

There're options to show and hide custom styles. About importStyles it should work fine.

As the trial version can't be debugged, after you've purchased a license please write to contact[at]phpdocx.com and we'll be able to help you with your template.

Regards.