{"id":188,"date":"2012-04-11T12:05:55","date_gmt":"2012-04-11T02:05:55","guid":{"rendered":"http:\/\/www.docx4java.org\/blog\/?p=188"},"modified":"2012-04-11T12:25:59","modified_gmt":"2012-04-11T02:25:59","slug":"docx-internal-hyperlinks","status":"publish","type":"post","link":"https:\/\/www.docx4java.org\/blog\/2012\/04\/docx-internal-hyperlinks\/","title":{"rendered":"docx &#8211; internal hyperlinks"},"content":{"rendered":"<p>There have been a couple of posts on the forum lately regarding adding hyperlinks to other parts of a docx. <\/p>\n<p>This blog post walks you through the generic process for investigating an issue like this. <\/p>\n<p>First, create a sample docx in Word which exhibits the issue of interest.  <\/p>\n<p>Here I&#8217;m interested in hyperlinks to a heading, and to a bookmark.  So see this docx. Second, look inside it (its a zip file).  For the link to the heading, document.xml contains a w:p containing:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\"> \r\n      &lt;w:hyperlink w:anchor=&quot;_My_heading&quot; w:history=&quot;1&quot;&gt;\r\n        &lt;w:r&gt;\r\n          &lt;w:rPr&gt;\r\n            &lt;w:rStyle w:val=&quot;Hyperlink&quot;\/&gt;\r\n          &lt;\/w:rPr&gt;\r\n          &lt;w:t&gt;My heading&lt;\/w:t&gt;\r\n        &lt;\/w:r&gt;\r\n      &lt;\/w:hyperlink&gt;\r\n<\/pre>\n<p>The heading itself is automatically given a bookmark:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n    &lt;w:p&gt;\r\n      &lt;w:pPr&gt;\r\n        &lt;w:pStyle w:val=&quot;Heading1&quot;\/&gt;\r\n      &lt;\/w:pPr&gt;\r\n      &lt;w:bookmarkStart w:id=&quot;0&quot; w:name=&quot;_My_heading&quot;\/&gt;\r\n      &lt;w:bookmarkEnd w:id=&quot;0&quot;\/&gt;\r\n      &lt;w:r&gt;\r\n        &lt;w:t&gt;My heading&lt;\/w:t&gt;\r\n      &lt;\/w:r&gt;\r\n    &lt;\/w:p&gt; <\/pre>\n<p>For the link to my bookmark, Word 2010 used the legacy field formulation:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n    &lt;w:p&gt;\r\n      &lt;w:r&gt;\r\n        &lt;w:fldChar w:fldCharType=&quot;begin&quot;\/&gt;\r\n      &lt;\/w:r&gt;\r\n      &lt;w:r&gt;\r\n        &lt;w:instrText xml:space=&quot;preserve&quot;&gt; HYPERLINK  \\l &quot;bm1&quot; &lt;\/w:instrText&gt;\r\n      &lt;\/w:r&gt;\r\n      &lt;w:r&gt;\r\n        &lt;w:fldChar w:fldCharType=&quot;separate&quot;\/&gt;\r\n      &lt;\/w:r&gt;\r\n      &lt;w:r w:rsidRPr=&quot;00D16ABA&quot;&gt;\r\n        &lt;w:rPr&gt;\r\n          &lt;w:rStyle w:val=&quot;Hyperlink&quot;\/&gt;\r\n        &lt;\/w:rPr&gt;\r\n        &lt;w:t&gt;bm1&lt;\/w:t&gt;\r\n      &lt;\/w:r&gt;\r\n      &lt;w:r&gt;\r\n        &lt;w:fldChar w:fldCharType=&quot;end&quot;\/&gt;\r\n      &lt;\/w:r&gt;\r\n    &lt;\/w:p&gt; <\/pre>\n<p> Third, what rels are involved?  To answer this, I run the docx through docx4j&#8217;s PartsList sample.  It shows me that these hyperlinks don&#8217;t create any rels.  Alternatively, to see this, you could have looked at the rels part when you unzipped the docx. <\/p>\n<p> So we can see that adding an internal hyperlink to a heading requires that it be bookmarked first. Once you have a bookmark, you use a w:hyperlink to refer to the bookmark by name (not id).  Doesn&#8217;t look like there is any reason to use fields for this. <\/p>\n<p> Here&#8217;s a suitable method: <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\t\/**\r\n\t * Create a Hyperlink object, which is suitable for adding to a w:p\r\n\t * @param bookmarkName\r\n\t * @param linkText\r\n\t * @return\r\n\t *\/\r\n\tpublic static Hyperlink hyperlinkToBookmark(String bookmarkName, String linkText) {\r\n\t\t\r\n\t\ttry {\r\n\t\t\t\r\n\t\t\tString hpl = &quot;&lt;w:hyperlink w:anchor=\\&quot;&quot; + bookmarkName + &quot;\\&quot; xmlns:w=\\&quot;http:\/\/schemas.openxmlformats.org\/wordprocessingml\/2006\/main\\&quot; &quot; +\r\n            &quot;w:history=\\&quot;1\\&quot; &gt;&quot; +\r\n            &quot;&lt;w:r&gt;&quot; +\r\n            &quot;&lt;w:rPr&gt;&quot; +\r\n            &quot;&lt;w:rStyle w:val=\\&quot;Hyperlink\\&quot; \/&gt;&quot; +  \/\/ TODO: enable this style in the document!\r\n            &quot;&lt;\/w:rPr&gt;&quot; +\r\n            &quot;&lt;w:t&gt;&quot; + linkText + &quot;&lt;\/w:t&gt;&quot; +\r\n            &quot;&lt;\/w:r&gt;&quot; +\r\n            &quot;&lt;\/w:hyperlink&gt;&quot;;\r\n\r\n\t\t\treturn (Hyperlink)XmlUtils.unmarshalString(hpl);\r\n\t\t\t\r\n\t\t} catch (Exception e) {\r\n\t\t\t\/\/ Shouldn't happen\r\n\t\t\te.printStackTrace();\r\n\t\t\treturn null;\r\n\t\t}\r\n\t\t\t\t\r\n\t}  <\/pre>\n<p>We can test it by altering the BookmarkAdd sample to add a link:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nHyperlink h = MainDocumentPart.hyperlinkToBookmark(bookmarkName, &quot;link to bookmark&quot;);\r\nwordMLPackage.getMainDocumentPart().addParagraphOfText(&quot;some text&quot;).getContent().add(h);\r\n\r\n<\/pre>\n<p>then checking the result opens in Word ok.<\/p>\n<p>That&#8217;s all. Added to docx4j in revision 1777.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There have been a couple of posts on the forum lately regarding adding hyperlinks to other parts of a docx. This blog post walks you through the generic process for investigating an issue like this. First, create a sample docx in Word which exhibits the issue of interest. Here I&#8217;m interested in hyperlinks to a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92],"tags":[],"class_list":["post-188","post","type-post","status-publish","format-standard","hentry","category-docx4j"],"_links":{"self":[{"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/posts\/188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/comments?post=188"}],"version-history":[{"count":9,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/posts\/188\/revisions"}],"predecessor-version":[{"id":196,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/posts\/188\/revisions\/196"}],"wp:attachment":[{"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/media?parent=188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/categories?post=188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/tags?post=188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}