What are the allowed ways of creating hyperlinks in docx files?

docx

Solution

When you say that the docx format is vague and lacks documentation, have you looked at the specs? http://www.ecma-international.org/publications/standards/Ecma-376.htm (Though I do find them vague at key points.)

There are at least two ways I know of to create links. w:hyperlink is one of them.

The `w:hyperlink` element either links internally or externally, and works more or less how you have discovered.

In the case of an external hyperlink, it will have a relationship id, and an entry in the relationships for this document marked as external that has a uri. The spec says that if the hyperlink is external, the anchor attribute should be ignored, but in practice, I found that Word will stick the anchor part of an external url here. E.g. `http://example.com/page#myAnchor` will store the uri without `#myAnchor` in the relationships and the `anchor` attribute of `hyperlink` will have "myAnchor" without the '#'. You will probably want to check for both.

For internal hyperlinks, the anchor should either match the `name` attribute of a `w:bookmarkStart` element, or be a special value like "_GoBack" or "_top".

The second case is images that are linked, which is, unfortunately, far more complicated. There will be a `w:drawing` for the image which will have a `docPr` element with a `hlinkClick` element, which will have a relationship id with the destination. The spec seems a bit unclear at this point, but looking at what Word does, it looks like if the relationship is internal, it will be a bookmark name (with '#' prepended), and if external, a uri.

Problem

I'm currently working on a library that will take docx files as input and use that to build html-pages, due to vague and lack of documentation of docx I have to rely heavily on example output to decide on how to handle certain things. One of these things is hyperlinks. As far as I have seen so far docx has, at least, two ways of doing hyperlinks: Anchor - `<w:hyperlink w:anchor="_Toc000000000" history="1"></w:hyperlink>` This seems to be the mostly prefered way of doing things like toc-links. Id - `<w:hyperlink w:id="rId7" history="1"></w:hyperlink>` This seems to be the only way to specify a url for the hyperlink (with the id being defined in the `.xml.rels` file) So far so good, my problem is that I have encountered files where they simple specify a rStyle (on the textrun object) value of "Hyperlink" and then seems to believe that this will make the text act as a hyperlink to the title specified in the textrun. For example a document can contain the following: ``` <w:p> <w:pPr> <w:pStyle w:val="Heading1"/> </w:pPr> <w:r> <w:t>Introduction</w:t> </w:r> </w:p> ``` And then further down the follwing: ``` <w:p> <w:r> <w:t>This is a hyperlink to </w:t> </w:r> <w:r> <w:rPr> <w:rStyle w:val="Hyperlink"/> </w:rPr> <w:t>Introduction</w:t> </w:r> <w:r> <w:t>.</w:t> </w:r> </w:p> ``` So my question is, is these kind of "hyperlinks" (`w:p` instead of `w:hyperlink`) actually valid or just something that word or the authors of the files i have is doing wrong?

Original source