How can I create an external link or an email link in a XWPFDocument? There is a description for Excel (HSSF XSSF), but i haven\'t found anything similar for Word (HWPF XWPF
All,
The above example shows how to create an external hyperlink. For those who need to create an internal hyperlink, see below code:
XWPFParagraph hyperPara = document.createParagraph();
hyperPara.setAlignment(ParagraphAlignment.CENTER);
addHyperlink(hyperPara, "Hyperlink Text", "Heading Text");
private static void addHyperlink(XWPFParagraph para, String text, String bookmark) {
//Create hyperlink in paragraph
CTHyperlink cLink=para.getCTP().addNewHyperlink();
cLink.setAnchor(bookmark);
//Create the linked text
CTText ctText=CTText.Factory.newInstance();
ctText.setStringValue(text);
CTR ctr=CTR.Factory.newInstance();
ctr.setTArray(new CTText[]{ctText});
//Create the formatting
CTFonts fonts = CTFonts.Factory.newInstance();
fonts.setAscii("Calibri Light" );
CTRPr rpr = ctr.addNewRPr();
CTColor colour = CTColor.Factory.newInstance();
colour.setVal("0000FF");
rpr.setColor(colour);
CTRPr rpr1 = ctr.addNewRPr();
rpr1.addNewU().setVal(STUnderline.SINGLE);
//Insert the linked text into the link
cLink.setRArray(new CTR[]{ctr});
}