title: Generate Inter-Document Hyperlink with Apache POI in Java
body: <p>I'm generating an XWPFDocument with Apache POI (never used it before this) and I'd like to link one paragraph to another paragraph inside the same .docx document.  Is this possible using POI's native functionality or do I need to deep-dive into XML Bean wrapper classes (i.e. CTP) to hand-jam this or am I out of luck?  Every instance of a question regarding hyperlinks and POI that I have seen references creating either an external-type hyperlink or a link between Excel workbook sheets.  I am as of now only able to generate a 'hyperlink' in the sense of ctrl-clicking the paragraph inside the finished document and it appears to simply do a text search starting from the top of the document.  Here's the code I am currently using to achieve this.  Thanks in advance!</p>
  
  <pre><code>public static void addInternalHyperlink(XWPFParagraph origin, String text, XWPFParagraph target) {
  
          if (target != null) {
  
          // Create the hyperlink itself
          CTHyperlink link = origin.getCTP().addNewHyperlink();       
          link.setAnchor(target.getText());
  
          // Create hyperlink text
          CTText linkText = CTText.Factory.newInstance();
          linkText.setStringValue(text);      
          CTR ctr = CTR.Factory.newInstance();
          ctr.setTArray(new CTText[] {linkText});
  
          // Format hyperlink text
          CTFonts fonts = CTFonts.Factory.newInstance();
          fonts.setAscii("Times New Roman");
          CTRPr rpr = ctr.addNewRPr();
          CTColor color = CTColor.Factory.newInstance();
          color.setVal("0000FF");
          rpr.setColor(color);
          CTRPr rpr1 = ctr.addNewRPr();
          rpr1.addNewU().setVal(STUnderline.SINGLE);
  
          // Insert formatted text into link
          link.setRArray(new CTR[] {ctr});
          }
      }
  </code></pre>
  
  <p>Please note that I'd like to use the 'origin' argument as the paragraph containing the actual link, the 'text' argument as the link text, and the 'target' argument as the actual link destination. </p>
  
  <p>UPDATE: Here's an XML snippet containing a sample paragraph which I have linked to a section heading via the Word GUI.</p>
  
  <pre><code>&lt;w:p w14:paraId="5B1C3A0C" w14:textId="659E388D" w:rsidR="00A4419C" w:rsidRDefault="00A4419C" w:rsidP="00A4419C"&gt;&lt;w:hyperlink w:anchor="_Another_Heading" w:history="1"&gt;&lt;w:r w:rsidRPr="00A4419C"&gt;&lt;w:rPr&gt;&lt;w:rStyle w:val="Hyperlink"/&gt;&lt;/w:rPr&gt;&lt;w:t&gt;Here is some stuff that could b&lt;/w:t&gt;&lt;/w:r&gt;&lt;w:r w:rsidRPr="00A4419C"&gt;&lt;w:rPr&gt;&lt;w:rStyle w:val="Hyperlink"/&gt;&lt;/w:rPr&gt;&lt;w:t&gt;e&lt;/w:t&gt;&lt;/w:r&gt;&lt;w:r w:rsidRPr="00A4419C"&gt;&lt;w:rPr&gt;&lt;w:rStyle w:val="Hyperlink"/&gt;&lt;/w:rPr&gt;&lt;w:t xml:space="preserve"&gt; the link&lt;/w:t&gt;&lt;/w:r&gt;&lt;/w:hyperlink&gt;&lt;/w:p&gt;&lt;w:p w14:paraId="19996B78" w14:textId="5C39B081" w:rsidR="00A4419C" w:rsidRPr="00A4419C" w:rsidRDefault="00A4419C" w:rsidP="00A4419C"&gt;&lt;w:pPr&gt;&lt;w:pStyle w:val="Heading1"/&gt;&lt;/w:pPr&gt;&lt;w:bookmarkStart w:id="0" w:name="_Another_Heading"/&gt;&lt;w:bookmarkEnd w:id="0"/&gt;&lt;w:r&gt;&lt;w:t&gt;Another Heading&lt;/w:t&gt;&lt;/w:r&gt;&lt;w:bookmarkStart w:id="1" w:name="_GoBack"/&gt;&lt;w:bookmarkEnd w:id="1"/&gt;&lt;/w:p&gt;
  </code></pre>