Forum


Replies: 1   Views: 2167
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 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.