Forum


Replies: 1   Views: 762
Modify imported style on the fly
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 dan_brokerbusiness  · 24-09-2022 - 02:40

Hi

We import various heading styles from various ms word docx into a phpdocx document, apply them throughout a generated document and generate a table of contents from those headings. Everything works as expected.

Now we have a specific title that needs to look like one of the headings but should not be included in the ToC. As the style definitions come from various documents we can not simply hard code how it should look, we really need "Heading 1 Style minus the headingLevel". 

Ideally we could simply override the headingLevel property on this specific text in the paragraphParams to unset the headingLevel or set it to a higher value which could be excluded in the ToC.

$docx->addText('Test Heading', [
   'pStyle'       => 'Heading1',
   'headingLevel' => 5
]);

Sadly this does not seem to work, the headingLevel of the pStyle takes precedence and can not be overwritten.

We are aware that we can create our own style using createParagraphStyle but there does not seem to be a way to read the current definition of an imported style in a format that can easily be modified and passed to createParagraphStyle.
We can see a way using the Indexer and creating the definitions from scratch, but because the original styles are out of our control and can basically contain any possible formattings this seems to be a big task for something seemingly simple.

Is there any alternative/better way?

Thanks!

Posted by admin  · 24-09-2022 - 08:42

Hello,

Your explanation is correct. An outline level set in a style can't be overridden in the paragraph styles. For this reason, this won't work:

$docx->addText('Test Heading', [
   'pStyle'       => 'Heading1',
   'headingLevel' => 9
]);

You can see this same behaviour in MS Word. If you apply a Heading style to a paragraph, then you can't set a custom Outline level for this paragraph (right click => Outline level option appears disabled). There's no specific tag to exclude a content from TOCs but using another style or do not apply a custom style.

DOCXCustomizer allows customize styles on-the-fly but to accomplish your task you need:

  • Clone an existing MS Word style and create a new style with the same styles but with a new name and a new styleId.
  • Handle external dependencies from the cloned style (basedOn tag).
  • Remove the outline level from the new style.

There's no method in the current version of phpdocx to accomplish this task. DOCXPath allows doing these workflows but only with contents, not styles. A DOCXPathStyles feature would be required, but this feature is in the roadmap to be included in a future release of phpdocx.

Please send an email to contact[at]phpdocx.com, so we can send you a proposal.

Regards.