Forum


Replies: 3   Views: 502
How do i get bookmarks and fields that is in a 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 rabol  · 26-04-2023 - 06:55

Hi

I know how to get variables from a template, but how to I get all bookmarks and fields ?

Thanks

Steen

Posted by rabol  · 26-04-2023 - 13:04

I'm sure that I'm doing wrong, but in my case it does not work

I have a document with 2 bookmarks ( sorry, cannot insert image og doc)

$docx = new CreateDocxFromTemplate(storage_path('bookmark.docx'));

    try {
        return $docx->getDocxPathQueryInfo([
            'type' => 'bookmark'
        ]);
    } catch (\Exception $e) {
        Log::error($e->getMessage());
    }

 

result



array:3 [â–¼
  "elements" => DOMNodeList {#2358 â–¼
    +length: 0
  }
  "length" => 0
  "query" => "//w:body/w:bookmarkStart[1=1]"
]

 

Posted by admin  · 26-04-2023 - 13:56

Hello,

Please note the following option available when using DOCXPath methods:

parent    string  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/...).

As default, the query is done only to direct children of the main tag (w:body). If you want to get all bookmarks, this is with any parent, you need to set parent to '/'. For example:

$docx->getDocxPathQueryInfo([
  'type' => 'bookmark',
  'parent' => '/',
]);

Also note that in a DOCX, a bookmark is a single tag without other contents. To extract fields using getDocxPathQueryInfo you may need to do a custom DOCXPath query. If you send a sample DOCX to contact[@]phpdocx.com we can generate a sample script to illustrate it.

Regards.