Forum


Replies: 3   Views: 174
Using getwordstyles with a type
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  · 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.