Java 如何在ApachePOI Word中创建超链接?

Java 如何在ApachePOI Word中创建超链接?,java,apache-poi,Java,Apache Poi,如何使用ApachePOI在Word文档中创建超链接?可以使用相对路径吗?到目前为止,还没有一种方法可以创建这样的路径(2018年3月,apache poi版本3.17)。因此,我们需要使用底层底层方法 以下示例提供了一种在XWPFParagraph中创建XWPFHyperlinkRun的方法。之后,可以将XWPFHyperlinkRun作为XWPFRun处理,以便进一步格式化,因为它扩展了此类 import java.io.*; import org.apache.poi.xwpf.user

如何使用ApachePOI在Word文档中创建超链接?可以使用相对路径吗?

到目前为止,还没有一种方法可以创建这样的路径(2018年3月,
apache poi
版本
3.17
)。因此,我们需要使用底层底层方法

以下示例提供了一种在
XWPFParagraph
中创建
XWPFHyperlinkRun
的方法。之后,可以将
XWPFHyperlinkRun
作为
XWPFRun
处理,以便进一步格式化,因为它扩展了此类

import java.io.*;

import org.apache.poi.xwpf.usermodel.*;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHyperlink;

public class CreateWordXWPFHyperlinkRun {

 static XWPFHyperlinkRun createHyperlinkRun(XWPFParagraph paragraph, String uri) {
  String rId = paragraph.getDocument().getPackagePart().addExternalRelationship(
    uri, 
    XWPFRelation.HYPERLINK.getRelation()
   ).getId();

  CTHyperlink cthyperLink=paragraph.getCTP().addNewHyperlink();
  cthyperLink.setId(rId);
  cthyperLink.addNewR();

  return new XWPFHyperlinkRun(
    cthyperLink,
    cthyperLink.getRArray(0),
    paragraph
   );
 }

 public static void main(String[] args) throws Exception {

  XWPFDocument document = new XWPFDocument();

  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run = paragraph.createRun();
  run.setText("This is a text paragraph having ");

  XWPFHyperlinkRun hyperlinkrun = createHyperlinkRun(paragraph, "https://www.google.de");
  hyperlinkrun.setText("a link to Google");
  hyperlinkrun.setColor("0000FF");
  hyperlinkrun.setUnderline(UnderlinePatterns.SINGLE);

  run = paragraph.createRun();
  run.setText(" in it.");

  paragraph = document.createParagraph();

  paragraph = document.createParagraph();
  run = paragraph.createRun();
  run.setText("This is a text paragraph having ");

  hyperlinkrun = createHyperlinkRun(paragraph, "./test.pdf"); //path in URI is relative to the Word document file
  hyperlinkrun.setText("a link to a file");
  hyperlinkrun.setColor("0000FF");
  hyperlinkrun.setUnderline(UnderlinePatterns.SINGLE);
  hyperlinkrun.setBold(true);
  hyperlinkrun.setFontSize(20);

  run = paragraph.createRun();
  run.setText(" in it.");
  
  FileOutputStream out = new FileOutputStream("CreateWordXWPFHyperlinkRun.docx");
  document.write(out);
  out.close();
  document.close();

 }
}
到目前为止,还没有一种方法可以创建这样的版本(2018年3月,
apachepoi
version
3.17
)。因此,我们需要使用底层底层方法

以下示例提供了一种在
XWPFParagraph
中创建
XWPFHyperlinkRun
的方法。之后,可以将
XWPFHyperlinkRun
作为
XWPFRun
处理,以便进一步格式化,因为它扩展了此类

import java.io.*;

import org.apache.poi.xwpf.usermodel.*;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHyperlink;

public class CreateWordXWPFHyperlinkRun {

 static XWPFHyperlinkRun createHyperlinkRun(XWPFParagraph paragraph, String uri) {
  String rId = paragraph.getDocument().getPackagePart().addExternalRelationship(
    uri, 
    XWPFRelation.HYPERLINK.getRelation()
   ).getId();

  CTHyperlink cthyperLink=paragraph.getCTP().addNewHyperlink();
  cthyperLink.setId(rId);
  cthyperLink.addNewR();

  return new XWPFHyperlinkRun(
    cthyperLink,
    cthyperLink.getRArray(0),
    paragraph
   );
 }

 public static void main(String[] args) throws Exception {

  XWPFDocument document = new XWPFDocument();

  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run = paragraph.createRun();
  run.setText("This is a text paragraph having ");

  XWPFHyperlinkRun hyperlinkrun = createHyperlinkRun(paragraph, "https://www.google.de");
  hyperlinkrun.setText("a link to Google");
  hyperlinkrun.setColor("0000FF");
  hyperlinkrun.setUnderline(UnderlinePatterns.SINGLE);

  run = paragraph.createRun();
  run.setText(" in it.");

  paragraph = document.createParagraph();

  paragraph = document.createParagraph();
  run = paragraph.createRun();
  run.setText("This is a text paragraph having ");

  hyperlinkrun = createHyperlinkRun(paragraph, "./test.pdf"); //path in URI is relative to the Word document file
  hyperlinkrun.setText("a link to a file");
  hyperlinkrun.setColor("0000FF");
  hyperlinkrun.setUnderline(UnderlinePatterns.SINGLE);
  hyperlinkrun.setBold(true);
  hyperlinkrun.setFontSize(20);

  run = paragraph.createRun();
  run.setText(" in it.");
  
  FileOutputStream out = new FileOutputStream("CreateWordXWPFHyperlinkRun.docx");
  document.write(out);
  out.close();
  document.close();

 }
}

衷心感谢您的回答。但是我把test.pdf和CreateWordXSSFHyperlinkRun.docx放在同一个目录中,他不工作。我想问test.pdf应该在那个目录中如果
test.pdf
文件与
Word
文件位于同一目录中,它对我有效。如果您将鼠标悬停在链接上方,Word中会显示什么URI?对不起。我刚才犯了一个错误。问题已经解决了。真诚地感谢你的回答。但是我把test.pdf和CreateWordXSSFHyperlinkRun.docx放在同一个目录中,他不工作。我想问test.pdf应该在那个目录中如果
test.pdf
文件与
Word
文件位于同一目录中,它对我有效。如果您将鼠标悬停在链接上方,Word中会显示什么URI?对不起。我刚才犯了一个错误。问题已经解决了。衷心感谢您的回答。