Forum


Replies: 4   Views: 2479
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 kobbe  · 10-12-2018 - 13:28

Hello! Am using template and need a way to find out margins (top, right, bottom and left). How can I do it? I only find how to set it using modifyPageLayout... but I do not want to set it since it will be different depending on template. :)

Posted by kobbe  · 11-12-2018 - 08:18

Thank you for fast reply.

I am not understandig what type to use.

$queryInfo = $docx->getDocxPathQueryInfo(null);
var_dump($queryInfo);

Result:

array(3) {
  ["elements"]=>
  object(DOMNodeList)#271 (1) {
    ["length"]=>
    int(1)
  }
  ["length"]=>
  int(1)
  ["query"]=>
  string(15) "//w:body/*[1=1]"
}
 

What parameters should I use? :o

Posted by admin  · 11-12-2018 - 08:50

Deleted by admin · 11-12-2018 - 08:55

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.