Skip to content

Commit

Permalink
[github-153] XWPFParagraph: easier way to create a link. Thanks to Th…
Browse files Browse the repository at this point in the history
…ibaut Cuvelier. This closes #153

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1863433 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
pjfanning committed Jul 19, 2019
1 parent 10b5a46 commit ea58359
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ private void initHyperlinks() {
// 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()));
}
Expand Down Expand Up @@ -410,6 +411,15 @@ public XWPFHyperlink getHyperlinkByID(String id) {
}
}

// 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;
}

Expand Down
23 changes: 23 additions & 0 deletions src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,29 @@ public XWPFRun createRun() {
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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ public void testAllPictureFormats() throws IOException, InvalidFormatException {
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");
Expand Down

0 comments on commit ea58359

Please # to comment.