Forum


Replies: 4   Views: 2305
Get margins from template?
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  · 11-12-2018 - 09:21

Hello,

You need to query by section type. But it will only work if the template has more than one section.

We have moved the request to the dev team to add that information to the Indexer class (https://www.phpdocx.com/api-documentation/docxutilities/indexer-parse-word-documents-with-PHP).

We have generated a code to get that information from any existing DOCX:

<?php

require_once 'classes/CreateDocx.php';

$docxStructure = new DOCXStructure();
$docxStructure->parseDocx('template.docx');

$documentDOM = new DOMDocument();
$optionEntityLoader = libxml_disable_entity_loader(true);
$documentDOM->loadXML($docxStructure->getContent('word/document.xml'));
libxml_disable_entity_loader($optionEntityLoader);

$documentXpath = new DOMXPath($documentDOM);
$documentXpath->registerNamespace('w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');

$sectionEntries = $documentXpath->query('//w:sectPr/w:pgMar');

foreach ($sectionEntries as $sectionEntry) {
    echo 'Top:' . $sectionEntry->getAttribute('w:top') . PHP_EOL;
    echo 'Right: ' . $sectionEntry->getAttribute('w:right') . PHP_EOL;
    echo 'Left: ' . $sectionEntry->getAttribute('w:left') . PHP_EOL;
    echo 'Bottom: ' . $sectionEntry->getAttribute('w:bottom') . PHP_EOL;
}

The returned values are twips (Twentieths of a Point): http://officeopenxml.com/WPSectionPgMar.php

Regards.