Generate Inter-Document Hyperlink with Apache POI in Java

后端 未结 1 1821
鱼传尺愫
鱼传尺愫 2021-01-27 04:01

I\'m generating an XWPFDocument with Apache POI (never used it before this) and I\'d like to link one paragraph to another paragraph inside the same .docx document. Is this pos

1条回答
  •  时光说笑
    2021-01-27 04:44

    The solution falls into two parts.

    First we need a XWPFHyperlinkRun whose target is an anchor in the document.

    Second we need that target anchor, which can be a bookmark in the document for example. So we need creating such bookmark in the document.

    Unfortunately both is not supported using only high level classes of apache poi until now. So we need the low level classes form ooxml-schemas too.

    The following code works using apache poi 4.0.0 together with ooxml-schemas-1.4.

    import java.io.FileOutputStream;
    
    import org.apache.poi.xwpf.usermodel.*;
    
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBookmark;
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHyperlink;
    
    import java.math.BigInteger;
    
    public class CreateWordHyperlinkBookmark {
    
     static XWPFHyperlinkRun createHyperlinkRunToAnchor(XWPFParagraph paragraph, String anchor) throws Exception {
      CTHyperlink cthyperLink=paragraph.getCTP().addNewHyperlink();
      cthyperLink.setAnchor(anchor);
      cthyperLink.addNewR();
      return new XWPFHyperlinkRun(
        cthyperLink,
        cthyperLink.getRArray(0),
        paragraph
       );
     }
    
     static XWPFParagraph createBookmarkedParagraph(XWPFDocument document, String anchor, int bookmarkId) {
      XWPFParagraph paragraph = document.createParagraph();
      CTBookmark bookmark = paragraph.getCTP().addNewBookmarkStart();
      bookmark.setName(anchor);
      bookmark.setId(BigInteger.valueOf(bookmarkId));
      XWPFRun run = paragraph.createRun();
      paragraph.getCTP().addNewBookmarkEnd().setId(BigInteger.valueOf(bookmarkId));
      return paragraph;
     }
    
     public static void main(String[] args) throws Exception {
    
      XWPFDocument document = new XWPFDocument();
    
      String anchor = "hyperlink_target"; 
      int bookmarkId = 0;
    
      XWPFParagraph paragraph = document.createParagraph();
      XWPFRun run = paragraph.createRun();
      run.setText("This is a text paragraph having ");
    
      //create hyperlink run
      XWPFHyperlinkRun hyperlinkrun = createHyperlinkRunToAnchor(paragraph, anchor);
      hyperlinkrun.setText("a link to an bookmark anchor");
      hyperlinkrun.setColor("0000FF");
      hyperlinkrun.setUnderline(UnderlinePatterns.SINGLE);
    
      run = paragraph.createRun();
      run.setText(" in it.");
    
      //some empty paragraphs
      for (int i = 0; i < 10; i++) {
       paragraph = document.createParagraph();
      }
    
      //create bookmarked paragraph as the hyperlink target
      paragraph = createBookmarkedParagraph(document, anchor, bookmarkId++);
      run = paragraph.getRuns().get(0);
      run.setText("This is the target.");
    
      FileOutputStream out = new FileOutputStream("CreateWordHyperlinkBookmark.docx");
      document.write(out);
      out.close();
      document.close();
    
     }
    }
    

    0 讨论(0)
提交回复
热议问题