Page 1 of 1

How to add hyperlink into a content control?

PostPosted: Tue Jun 04, 2013 8:34 pm
by raylapse
Hi Jason,

With your help and read your samples, I can successfully build my document by using content control binding solution. It's quite powerful and easy to use. With nested repeating I can even build dynamic paragraphs with dynamic content in each paragraph.

However, now I come to the last issue which I can not solve. I didn't find any answer in this forum also.
The problem is, I want to bind a content control to a hyperlink, and dynamically set the hyperlink text and target address from custom XML. Is this possible with current Docx4j implementation?

I know the Docx4j can automatically convert 'http://' texts to a hyperlink. But my case is different - I want to link to local share folders, so the link would be like '../folder1/folder2/test.xlsx'. And I want to set the hyperlink text dynamically.

Best Regards,
Daniel

Re: How to add hyperlink into a content control?

PostPosted: Tue Jun 04, 2013 9:44 pm
by jason
Hi Daniel

If you could include escaped WordML in your xml document, this would provide a general solution to your problem and others.

For example:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
<yourxmlnode>
    &lt;w:p&gt;
      &lt;w:r&gt;
        &lt;w:rPr&gt;
          &lt;w:rStyle w:val="Hyperlink"/&gt;
        &lt;/w:rPr&gt;
        &lt;w:fldChar w:fldCharType="begin"/&gt;
        &lt;w:instrText xml:space="preserve"&gt; HYPERLINK "../folder1/folder2/test.xlsx" &lt;/w:instrText&gt;
        &lt;w:fldChar w:fldCharType="separate"/&gt;
        &lt;w:t&gt;test.xlsx&lt;/w:t&gt;
        &lt;w:fldChar w:fldCharType="end"/&gt;
      &lt;/w:r&gt;
    &lt;/w:p&gt;
</yourxmlnode>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Docx4j would then recognise this as WordML, and insert it directly in the relevant content control.

Of course, it'd be up to you to write suitable escaped WordML into your XML file.

What do you think?

Re: How to add hyperlink into a content control?

PostPosted: Wed Jun 05, 2013 5:44 pm
by raylapse
Hi Jason,

I tried the way you suggested but have no success (sorry, I am new to Docx4j).
Can you kindly provide a simple example doc file and code snippet?

My another thought is, maybe it's worth to do it in BindingHandler as you already did for text start with 'http://' and 'Mailto:'.
Enhance the BindingHandler as following:
1. Remove the hard coded pattern of 'http://' and 'mailto:', and create new method setHyperLinkPattern. So the caller can explicitly set what kinds of text should be treated as a link.
2. Support the display text (i.e. <w:t>) of a link. To let the BindingHandler find the display text of a link, we can simply define a rule to get the display text from binded xml node. For example if the binded node contains text 'http://www.docx4j.org$$$Docx4j$$$", then the BindingHanlder treat "http://www.docx4j.org" as the link address and treat "Docx4j" as the display text. The benefit of this approach is, we don't need to update OpenDoPE add-ins to support the binding of display text.

Best Regards,
Daniel

Re: How to add hyperlink into a content control?

PostPosted: Wed Jun 05, 2013 11:20 pm
by jason
It wouldn't work, because it isn't implemented yet. Thinking a bit more about it, the approach would have problems if the content included anything with a rel to another part. So that aspect needs to be thought through.

Your suggestion is similar to what docx4j 3.0 will do with hyperlinks in its HTML and PDF output. (interface ConversionHyperlinkHandler containing method handleHyperlink)

Re: How to add hyperlink into a content control?

PostPosted: Mon Jun 10, 2013 1:12 pm
by raylapse
Hi Jason,

Thanks for the clarification.
To overcome this issue, currently I made a workaround:
- Generate the document using content controls. For hyperlink content, insert placeholders as text like "${hyperlink1}".
- Remove SDTs by removehandler
- find all hyperlink placeholders and manually replacement to real hyperlinks.

We would still expect the docx4j can handle the hyperlink inside content controls.
Because the workaround I made can not support below case:
- the user generate the document A.
- the user do some manually changes for A
- the user want to re-generate document A with new XML data but want to keep the manual changes he made before

Best Regards,
Daniel

Re: How to add hyperlink into a content control?

PostPosted: Tue Jun 25, 2013 2:51 pm
by jason
raylapse wrote:We would still expect the docx4j can handle the hyperlink inside content controls.


Please see now https://github.com/plutext/docx4j/commi ... 5de3415730

BindingHyperlinkResolver is new, and can be overridden:-

https://github.com/plutext/docx4j/blob/ ... olver.java