【问题标题】:Insert hyperlinks to a XWPFParagraph to another Paragraph in a XWPFDocument将 XWPFParagraph 的超链接插入到 XWPFDocument 中的另一个段落
【发布时间】:2018-06-19 09:36:58
【问题描述】:

我想让XWPFParagraph(开始)中的文本成为文档中另一个具体XWPFParagraph(结束)的超链接。我找到了在XWPFCell 中创建超链接的代码,但它不起作用(启动链接以开始):

    private static void createHyperlink(XWPFParagraph start, XWPFParagraph end, String endText, String startText) {

    CTHyperlink cLink = end.getCTP().addNewHyperlink();
    cLink.setAnchor(startText);

    CTText ctText = CTText.Factory.newInstance();
    ctText.setStringValue(endText);
    CTR ctr = CTR.Factory.newInstance();
    ctr.setTArray(new CTText[] { ctText });


    cLink.setRArray(new CTR[] { ctr });
    start.getCTP().setHyperlinkArray(new CTHyperlink[] { cLink });
    end.getCTP().removeHyperlink(0);
}

【问题讨论】:

    标签: java hyperlink xwpf


    【解决方案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});            
        }
    

    【讨论】:

      猜你喜欢
      • 2013-01-27
      • 1970-01-01
      • 2020-03-04
      • 1970-01-01
      • 1970-01-01
      • 2015-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多