How to create a email link in POI Word Format

前端 未结 4 1799
迷失自我
迷失自我 2021-01-14 16:12

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

4条回答
  •  难免孤独
    2021-01-14 16:47

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

提交回复
热议问题