]> source.dussan.org Git - poi.git/commitdiff
[github-153] XWPFParagraph: easier way to create a link. Thanks to Thibaut Cuvelier...
authorPJ Fanning <fanningpj@apache.org>
Fri, 19 Jul 2019 20:49:48 +0000 (20:49 +0000)
committerPJ Fanning <fanningpj@apache.org>
Fri, 19 Jul 2019 20:49:48 +0000 (20:49 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1863433 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java

index 8e7e383d45b536407c7c4d839e620d55a731b299..884cc8ef119dc32d98f1cb9735f4e8da29095c93 100644 (file)
@@ -273,6 +273,7 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
         // Get the hyperlinks
         // TODO: make me optional/separated in private function
         try {
+            hyperlinks = new ArrayList<>();
             for (PackageRelationship rel : getPackagePart().getRelationshipsByType(XWPFRelation.HYPERLINK.getRelation())) {
                 hyperlinks.add(new XWPFHyperlink(rel.getId(), rel.getTargetURI().toString()));
             }
@@ -410,6 +411,15 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
             }
         }
 
+        // If the link was not found, rebuild the list (maybe a new link was added into the document) and check again.
+        initHyperlinks();
+        for (XWPFHyperlink link : hyperlinks) {
+            if (link.getId().equals(id)) {
+                return link;
+            }
+        }
+
+        // Link still not there? Giving up.
         return null;
     }
 
index 305626d9ce3d6463ccaeee36a376f0af2798cdd7..4aec6eba84a5860c3d835f38c47aa5ea8e9e8241 100644 (file)
@@ -1440,6 +1440,29 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para
         return xwpfRun;
     }
 
+    /**
+     * Appends a new hyperlink run to this paragraph
+     *
+     * @return a new hyperlink run
+     */
+    public XWPFHyperlinkRun createHyperlinkRun(String uri) {
+        // Create a relationship ID for this link. 
+        String rId = getPart().getPackagePart().addExternalRelationship(
+                uri, XWPFRelation.HYPERLINK.getRelation()
+        ).getId();
+        
+        // Create the run. 
+        CTHyperlink ctHyperLink = getCTP().addNewHyperlink();
+        ctHyperLink.setId(rId);
+        ctHyperLink.addNewR();
+        
+        // Append this run to the paragraph. 
+        XWPFHyperlinkRun link = new XWPFHyperlinkRun(ctHyperLink, ctHyperLink.getRArray(0), this);
+        runs.add(link);
+        iruns.add(link);
+        return link;
+    }
+
     /**
      * insert a new Run in RunArray
      *
index 15611bcf4bdd1d553ec2df6b128c25e8078a8ce5..f68dd50a1360f4d3f51633a91819a61a83cac6d5 100644 (file)
@@ -185,6 +185,18 @@ public final class TestXWPFDocument {
         doc.close();
     }
 
+    @Test
+    public void testAddHyperlink() throws IOException {
+        XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx");
+        XWPFParagraph p = doc.createParagraph();
+        XWPFHyperlinkRun h = p.createHyperlinkRun("http://poi.apache.org/");
+        h.setText("Apache POI");
+
+        assertEquals("http://poi.apache.org/", h.getHyperlink(doc).getURL());
+        assertEquals(p.getRuns().size(), 1);
+        assertEquals(p.getRuns().get(0), h);
+    }
+
     @Test
     public void testRemoveBodyElement() throws IOException {
         XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx");