Java 如何使用ApachePOI向XWPFDocument的页脚添加超链接?

Java 如何使用ApachePOI向XWPFDocument的页脚添加超链接?,java,hyperlink,apache-poi,footer,xwpf,Java,Hyperlink,Apache Poi,Footer,Xwpf,appendExternalHyperlink()方法在XWPFDocument的页脚中不起作用。在页脚中,结果不会被识别为超链接 我是ApachePOI新手,对低级的东西没有经验。有人能解释一下这里有什么问题吗 public class FooterProblem { public static void main(final String[] args) throws Exception { final XWPFDocument docx = new XWPFDocument

appendExternalHyperlink()
方法在XWPFDocument的页脚中不起作用。在页脚中,结果不会被识别为超链接

我是ApachePOI新手,对低级的东西没有经验。有人能解释一下这里有什么问题吗

public class FooterProblem {

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

    final XWPFDocument docx = new XWPFDocument();
    final XWPFParagraph para = docx.createParagraph();
    final XWPFRun paraRun = para.createRun();
    paraRun.setText("Email: ");
    appendExternalHyperlink("mailto:me@example.com", "me@example.com", para);

    final XWPFParagraph footer = docx.createFooter(HeaderFooterType.DEFAULT).createParagraph();
    final XWPFRun footerRun = footer.createRun();
    footerRun.setText("Email: ");
    appendExternalHyperlink("mailto:me@example.com", "me@example.com", footer);

    final FileOutputStream out = new FileOutputStream("FooterProblem.docx");
    docx.write(out);
    out.close();
    docx.close();
  }

  public static void appendExternalHyperlink(final String url, final String text, final XWPFParagraph paragraph) {

    // Add the link as External relationship
    final String id = paragraph.getDocument().getPackagePart()
        .addExternalRelationship(url, XWPFRelation.HYPERLINK.getRelation()).getId();

    // Append the link and bind it to the relationship
    final CTHyperlink cLink = paragraph.getCTP().addNewHyperlink();
    cLink.setId(id);

    // Create the linked text
    final CTText ctText = CTText.Factory.newInstance();
    ctText.setStringValue(text);
    final CTR ctr = CTR.Factory.newInstance();
    ctr.setTArray(new CTText[] { ctText });

    // Insert the linked text into the link
    cLink.setRArray(new CTR[] { ctr });
  }
}

footer[n].xml
是它自己的包部分,需要它自己的关系。但是您的代码总是为
document.xml
包部分创建外部超链接关系。它总是使用
段落.getDocument()
。这是错误的

以下代码提供了一种方法,用于在给定的
XWPFParagraph
中创建
XWPFHyperlinkRun
,并获取正确的包部分以将关系置于其上。它使用
段落.getPart()
获取正确的部分。因此,此方法适用于文档正文以及页眉和/或页脚中的段落

import java.io.FileInputStream;
import java.io.FileOutputStream;

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

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

public class CreateWordHyperlinks {

 static XWPFHyperlinkRun createHyperlinkRun(XWPFParagraph paragraph, String uri) throws Exception {
  String rId = paragraph.getPart().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 a link to Google ");

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

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

  XWPFFooter footer = document.createFooter(HeaderFooterType.DEFAULT);
  paragraph = footer.createParagraph();
  run = paragraph.createRun();
  run.setText("Email: ");

  hyperlinkrun = createHyperlinkRun(paragraph, "mailto:me@example.com");
  hyperlinkrun.setText("me@example.com");
  hyperlinkrun.setColor("0000FF");
  hyperlinkrun.setUnderline(UnderlinePatterns.SINGLE);

  FileOutputStream out = new FileOutputStream("CreateWordHyperlinks.docx");
  document.write(out);
  out.close();
  document.close();

 }
}

非常感谢您立即回复。如果我想了解更多关于ApachePOI-XWPF的信息,尤其是了解底层代码的好起点是什么?@Martin:下载源代码并从中执行
javadoc
ooxml模式
是从
officeopenxml
规范生成的类。因此,如果您解压缩
*.docx
文件并查看
/word/document.XML
/word/footer1.XML
,这些类对应于您得到的
XML
元素。这些提示对我非常有用!谢谢。