Forum


Replies: 2   Views: 726
How to insert cross-reference at a specific place
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 arnaudbouchard  · 24-07-2018 - 23:01

Is it possible to insert a cross-reference (via the addCrossReference method, or anything more suitable if needed) to a specific place in my template? Same as I would do with insertWordFragment + referenceNode.

Thanks,

Posted by arnaudbouchard  · 25-07-2018 - 04:58

I tried setting up a wordML with the following:

$wordML = '<w:r><w:fldChar w:fldCharType="begin"/></w:r>';
$wordML .= '<w:r><w:instrText xml:space="preserve">' . 'REF ' . $crossRefName . '</w:instrText></w:r>';
$wordML .= '<w:r><w:fldChar w:fldCharType="separate"/></w:r>';
$wordML .= '<w:r><w:t>' . $crossRefText . '</w:t></w:r>';
$wordML .= '<w:r><w:fldChar w:fldCharType="end"/></w:r>';

$bookmarkId = rand(9999999, 999999999);
$wordML .= '<w:bookmarkStart w:id="' . $bookmarkId . '" w:name="_GoBack"/>';
$wordML .= '<w:bookmarkEnd w:id="' . $bookmarkId . '"/>';

And then: 

$docx->replaceVariableByWordML(['myVar' => $wordML]);

But when opening the generated document I get the following error:

The Open XML file 'test.docx' cannot be opened because there are problems with the content or the file name might contain invalid characters. Unspecified error | Location: 2.

Where do you think the definition of my wordML is wrong?

Posted by admin  · 26-07-2018 - 07:23

Hello,

Yes, you can use addCrossReference using a bookmark as any other content to generate a WordFragment and then use DOCXPath. Please check the included examples in the DOCXPath folder of your package to view similar samples.

If you open a ticket on https://www.phpdocx.com/support, the dev team will generate a custom script to illustrate how to do it.

Regards.