From ea58359b7c4f4a4b9f26803a94d5aadd289f2bac Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Fri, 19 Jul 2019 20:49:48 +0000 Subject: [PATCH] [github-153] XWPFParagraph: easier way to create a link. Thanks to Thibaut Cuvelier. This closes #153 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1863433 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xwpf/usermodel/XWPFDocument.java | 10 ++++++++ .../poi/xwpf/usermodel/XWPFParagraph.java | 23 +++++++++++++++++++ .../poi/xwpf/usermodel/TestXWPFDocument.java | 12 ++++++++++ 3 files changed, 45 insertions(+) diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java index 8e7e383d45..884cc8ef11 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java @@ -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; } diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java index 305626d9ce..4aec6eba84 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java @@ -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 * diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java index 15611bcf4b..f68dd50a13 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java @@ -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"); -- 2.39.5