将XWPFParagraph的超链接插入XWPFDocument中的另一个段落

时间:2018-01-10 09:00:35

标签: java hyperlink xwpf

我想在<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li class="btn checkall"> <input type="checkbox" name="offer[key1]" class="check_one"> <input type="checkbox" name="offer[key2]" class="check_one"> <input type="checkbox" name="offer[key3]" class="check_one"> <input type="checkbox" name="offer[key4]" class="check_one"> </li> <li class="btn checkall"> <input type="checkbox" name="offer[key1]" class="check_one"> <input type="checkbox" name="offer[key2]" class="check_one"> <input type="checkbox" name="offer[key3]" class="check_one"> <input type="checkbox" name="offer[key4]" class="check_one"> </li> <li class="btn checkall"> <input type="checkbox" name="offer[key1]" class="check_one"> <input type="checkbox" name="offer[key2]" class="check_one"> <input type="checkbox" name="offer[key3]" class="check_one"> <input type="checkbox" name="offer[key4]" class="check_one"> </li> <li class="btn checkall"> <input type="checkbox" name="offer[key1]" class="check_one"> <input type="checkbox" name="offer[key2]" class="check_one"> <input type="checkbox" name="offer[key3]" class="check_one"> <input type="checkbox" name="offer[key4]" class="check_one"> </li> </ul>(开始)内部将文本设置为文档中另一个具体XWPFParagraph(结束)的超链接。我找到了一个代码来在XWPFParagraph内创建超链接,但它无效(启动链接开始):

XWPFCell

1 个答案:

答案 0 :(得分:1)

我终于做到了。最初的想法是创建一个从XWPFParagraph到另一个XWPFParagraph的超链接,但由于我将始终链接到文档中具有唯一文本的段落,我发现它是这样的:

    private static void createHyperLink(XWPFParagraph start, String startTxt, String endTxt) {

        // Creating the hyperlink in the start paragraph
        CTHyperlink cLink = start.getCTP().addNewHyperlink();

        // Link to the end text in the doc
        cLink.setAnchor(endTxt);

        // Creating the String that will have the hyperlink
        CTText ctText = CTText.Factory.newInstance();
        ctText.setStringValue(startTxt);
        CTR ctr=CTR.Factory.newInstance();
        ctr.setTArray(new CTText[]{ctText});

        // Inserting the String in the doc
        cLink.setRArray(new CTR[]{ctr});            
    }