Forum


Replies: 2   Views: 1826
How to read page and table-cell properties
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  · 10-09-2020 - 01:40

Hi

I have a simple word document with a table with two cells that contain A1 and A2 respectively (see https://ibb.co/pRS2xN1).

I need to

  • Find the table-cell styling (background, border/borderWidth/borderColor) of the cells containing A1 and A2
  • Find out the margins of the document (basically a getPageLayout() equivalent to modifyPageLayout())

Is this possible with phpdocx? Are there any examples on how to go about this?

I have been trying to use getWordStyles() but without much luck. Even using something as simple as:

$docx = new CreateDocxFromTemplate(resource_path('word/query_test.docx'));
$referenceNode = array(
   'type' => 'table-cell',
);
print_r($docx->getWordStyles($referenceNode));

I would at least expect some output but I don't get anything. Also tried

$referenceNode = array(
   'type' => 'table-cell',
   'contains' => 'A1',
);

Which would be my ideal solution of course but probably won't work as A1 itself is a paragraph and I would have to construct a query to search for table cell containing a paragraph that contains A1 instead. But as I don't get anything with just searching for type 'table-cell' I am not sure this is even possible.

Any help appreciated.

Regards

Posted by admin  · 10-09-2020 - 07:12

Hello,

To query cell and rows in tables, you need to set the parent option, for example, using the document SimpleExample.docx in the package:

$docx = new CreateDocxFromTemplate('SimpleExample.docx');
$referenceNode = array(
    'type' => 'table-cell',
    'parent' => 'w:tbl/w:tr/',
    'contains' => 'First row',
);

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

You can read a sample of getting cell and row information in examples/DocxPath/getWordStyles/sample_7.php

Although First row (as A1 in your DOCX), is included in a paragraph, this paragrah is also include in the cell (w:tc) so the query returns the correct information. Also please note that some styles can be applied to table (w:tbl) or even a custom table style (getWordStyles is compatible with both), so you'd need to query them too depending on how the DOCX was created.

About gettion layout values, you can use Indexer: https://www.phpdocx.com/api-documentation/docxutilities/indexer-parse-word-documents-with-PHP, layout is set in sections.

Regards.

Posted by dan_brokerbusiness  · 11-09-2020 - 04:27

This was very helpful, I was able to get all the information needed from the document now.
Thanks!