如何使用 Apache POI 将超链接添加到 XWPFDocument 的页脚?

How to add a hyperlink to the footer of a XWPFDocument using Apache POI?

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

我是 Apache POI 的新手,对底层的东西没有任何经验。有人可以解释一下这里的问题吗?

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 包部分创建外部超链接关系。它总是使用 paragraph.getDocument()。这是错误的。

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

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();

 }
}