Forum


Replies: 8   Views: 1914
Include heading item number in cross reference
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 dev@ams  · 03-04-2020 - 08:01

Hi there,

Is it possible to use the number of a heading in a cross reference like in the attached example?

For your information: we are using this document as reference doc (for styling and heading numberings) for generating new documents.

https://ibb.co/VNzqmKX
https://ibb.co/R9SbDcW (cross ref shown with field code)

 

Posted by dev@ams  · 03-04-2020 - 08:53

When we use 

<phpdocx_crossreference data-text="someText" data-type="heading" data-referenceName="refName"/>`

we get a reference source not found

Posted by admin  · 03-04-2020 - 08:56

Hello,

It's not possible to accomplish that task using the current version of addCrossReference. We move the request to the dev team to add support.

As the dev team sent to your email the latest version of addCrossReference that supports REF and ABOVE_BELOW, maybe you could add the following change to be able to set custom modifiers:

/**
 * Adds a cross reference
 *
 * @access public
 * @param string $text Text of the reference
 * @param array $options
 *  Values:
 * 'type' (bookmark, heading)
 * 'modifiers' (string) custom modifiers: \r \h, \n \h...
 * 'referenceName' (string) the name of the element to be referred
 * 'referenceTo' (string) content to display when the field is updated PAGEREF (default), REF, ABOVE_BELOW
 * For other options @see addText
 */
public function addCrossReference($text, $options = array())
{
    if (!isset($options['type'])) {
        $options['type'] = 'bookmark';
    }
    if (!isset($options['referenceTo'])) {
        $options['referenceTo'] = 'PAGEREF';
    }
    $modifiers = ' \h';
    if ($options['referenceTo'] == 'ABOVE_BELOW') {
        $options['referenceTo'] = 'REF';
        $modifiers = ' \p \h';
    }

    if (isset($options['modifiers'])) {
        $modifiers = $options['modifiers'];
    }

So you could set a custom modifier when adding a cross-reference:

$docx->addCrossReference('Custom text cross-reference', array('type' => 'bookmark', 'referenceName'=> 'bookmark_name', 'referenceTo' => 'REF', 'modifiers' => '\r \h'));

Maybe this small change is enough for your needs?

Regards.

Posted by admin  · 03-04-2020 - 08:59

Hello,

heading type in addCrossReference needs a Heading style and doesn't reference a heading number (it does an internal search to get the content based on style name), so it won't be useful for your needs. We always recommend using bookmarks to add cross-references, please check our previous reply.

Regards

Posted by dev@ams  · 03-04-2020 - 09:28

Thanks for your quick response! We followed your suggestion by using:

<phpdocx_bookmark data-name="scoobie" data-type="start" /><phpdocx_heading data-text="Scoobie doo" data-level="2" data-pStyle="Heading2" data-b="" data-sz="" data-fontSize="" data-color="" data-font="" /><phpdocx_bookmark data-name="scoobie" data-type="end" />

But then we loose the heading (i.e. Word regards it as a 'normal' paragraph.

Posted by admin  · 03-04-2020 - 10:20

Hello,

Sorry, but we don't see that the HTML you are using is following our suggestion: maybe you could add the following change to be able to set custom modifiers.

Anyway, the HTML you are using:

<phpdocx_bookmark data-name="scoobie" data-type="start" /><phpdocx_heading data-text="Scoobie doo" data-level="2" data-pStyle="Heading2" data-b="" data-sz="" data-fontSize="" data-color="" data-font="" /><phpdocx_bookmark data-name="scoobie" data-type="end" />

is applying a Heading2 style name. Does it exist in your DOCX? if a style doesn't exist, the default normal style is applied. For example, if you are using the default template included in phpdocx, you could use:

<phpdocx_bookmark data-name="scoobie" data-type="start" /><phpdocx_heading data-text="Scoobie doo" data-level="2" data-pStyle="Heading2PHPDOCX" data-b="" data-sz="" data-fontSize="" data-color="" data-font="" /><phpdocx_bookmark data-name="scoobie" data-type="end" />

or create a custom paragraph style using phpdocx.

But you shouldn't confuse styles tags/attributes and heading tags when creating contents. A DOCX can have a normal style and a heading level; for example, the HTML you are transforming generates a paragraph with an outline level (data-level="2") and a normal style (because Heading2 style name doesn't exist in your DOCX).

Regards.

Posted by dev@ams  · 03-04-2020 - 14:53

Thanks! We managed to make it work. Only limitation is where we want to make a crossref from one heading to another. The headings don't allow for child elements right? Is there a specific reason for that? And is it something you have on the roadmap?

Posted by admin  · 03-04-2020 - 17:03

Hello,

We have added a task to the dev team to be checked. For this kind of specific tasks that are not supported in the current version, we recommend you to open a support ticket (https://www.phpdocx.com/support), so you can attach a sample DOCX and the dev team will check it to send the best approach, even doing a minor development to support it if possible.

Regards.

Posted by dev@ams  · 06-04-2020 - 06:51

Thanks for the advice. We'll probably do that then.