Forum


Replies: 1   Views: 2153
How to check in code if a style (name or id) exists or not
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 jari.heinonen  · 08-09-2017 - 05:23

Hi,

We're using Phpdocx 7.0 advanced. A great product by the way, thank you.

We have a little problem because we need to use multiple of documents for styling the output. As fas as I understand the parseStyles method creates a separate document from where you can check the available styles manually.

But how to check in the code if the style (by name) is available or not e.g. after a call to importStyles method?

Any quick advice is appreciated, thanks

Jari

 

Posted by admin  · 08-09-2017 - 06:46

Hello,

UPDATE: Indexer class returns information from a DOCX document, including styles.

There's no method in the current version to query if a style ID exists or not. We move the request to the dev team to add a new method.

Using the current version you could change the CreateDocx.inc file and add a new method that returns the _wordStylesT variable:

/**
 * Returns the wordStyles content.
 *
 * @return DOMDocument
 */
public function getWordStylesT()
{
  return $this->_wordStylesT;
}

It returns a DOMDocument that can be queried using DOM methods such as DOMXPath. If you save the return of the method in the $stylesDOCX var, you can query a styleID using these lines:

$xpathStyles = new DOMXPath($stylesDOCX);
$xpathStyles->registerNamespace('w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');
$stylesID = $xpathStyles->query('//w:style[@w:styleId="' . $styleId . '"]');

If the length of $stylesID if 0 then the style ID doesn't exist.

Regards.