Forum


Replies: 3   Views: 122
Using getwordstyles with a type

Posted by jeremie  · 16-07-2024 - 15:01

getWordStyles only works with type "default".

The following code works well and returns a list of styles:

$docx = new CreateDocxFromTemplate($document_word);
$referenceNode = array(
         'type' => 'default',
);
$styles = $docx->getWordStyles($referenceNode);

But any other $referenceNode returns an empty $styles for instance:

$referenceNode = array( 'type' => '*' );
$referenceNode = array( 'type' => 'paragraph' );
$referenceNode = array( 'type' => 'paragraph', 'contains' => 'Hello world' );

It used to work properly with version 9. I'm using version 12 and it doesn't work anymore.

What am I missing?

Posted by admin  · 16-07-2024 - 17:46

Hello,

We recommend you test the samples included in the package (examples/DocxPath/getWordStyles).

All supported types are working correctly. * is a global DOCXPath selector, so a custom type should be chosen to get elements:

chart, image, default, list, paragraph (also for links and lists), run, style, table, table-row, table-cell, table-cell-paragraph

some of the these types may not be supported in phpdocx 12. These are the supported types in phpdocx 15.

Please note that by default, the parent is the main document body, maybe you need to choose '/' to set any parent query:

Main document body as default, allows to set any parent or a specific one. w:body (default), '/' (any parent) or any other specific parent (/w:tbl/, /w:tc/, /w:r/...).


On https://www.phpdocx.com/support you can read the available technical support in a license (How much support includes my license? section). For further support, please send an email to contact[at]phpdocx.com.

Regards.

Posted by jeremie  · 17-07-2024 - 09:24

Thank you for your response. You're right, the examples work perfectly. They helped me pintpoint the issue more accurately.

It's the "contains" option that doesn't work properly.

In example \examples\DocxPath\getWordStyles\sample_1.php, changing "contains" from 'level 2 heading' to 'simple Word' returns an empty array, although 'simple Word' is indeed contained in the second line of document \examples\files\DOCXPathTemplate.docx.

In other words, you can't find the string 'simple Word' with the following code:

require_once '../../../classes/CreateDocx.php';

$docx = new CreateDocxFromTemplate('../../files/DOCXPathTemplate.docx');

$docx = new CreateDocxFromTemplate($document_word);
$referenceNode = array(
    'type' => 'paragraph',
    'contains' => 'simple Word',
);
$styles = $docx->getWordStyles($referenceNode);

I think it's a bug because it used to work properly in previous versions.

Let me know if it's not a bug and I will gladly pay for additional technical support.

Posted by admin  · 17-07-2024 - 17:47

Hello,

The following code:

$referenceNode = array(
    'type' => 'paragraph',
    'contains' => 'simple Word',
);
$styles = $docx->getWordStyles($referenceNode);

returns an empty array because the XML of the paragraph that contains that string (simple Word) doesn't have any pPr or pStyle:

<w:p w:rsidR="008518C0" w:rsidRDefault="00764101">
    <w:r>
        <w:t xml:space="preserve">This is </w:t>
    </w:r>
    <w:r w:rsidR="007B3611" w:rsidRPr="007B3611">
        <w:t xml:space="preserve">a simple Word document </w:t>
    </w:r>
    <w:r>
        <w:t xml:space="preserve">help us illustrate the </w:t>
    </w:r>
    <w:r w:rsidR="0000124E">
        <w:t xml:space="preserve">capabilities of the </w:t>
    </w:r>
    <w:proofErr w:type="spellStart"/>
    <w:r w:rsidR="0000124E">
        <w:t>DOCXPath</w:t>
    </w:r>
    <w:proofErr w:type="spellEnd"/>
    <w:r w:rsidR="0000124E">
        <w:t xml:space="preserve"> package</w:t>
    </w:r>
    <w:r>
        <w:t>.</w:t>
    </w:r>
</w:p>

The paragraph type with getWordStyles returns pPr styles and pStyle styles. For example:

$referenceNode = array(
    'type' => 'paragraph',
    'contains' => 'One',
);

$contents = $docx->getWordStyles($referenceNode);

returns the information (pPr and pStyle styles) from the paragraph that contains "One":

<w:p w:rsidR="007B3611" w:rsidRDefault="007B3611" w:rsidP="007B3611">
    <w:pPr>
        <w:pStyle w:val="ListParagraph"/>
        <w:numPr>
            <w:ilvl w:val="0"/>
            <w:numId w:val="1"/>
        </w:numPr>
    </w:pPr>
    <w:r>
        <w:t>One</w:t>
    </w:r>
</w:p>

On https://www.phpdocx.com/documentation/snippets/get-font-size-content you can read a sample to get the font size of a content (document defaults using Indexer, default paragraph and character styles using getWordStyles, and specific styles using getWordStyles).

Regards.