gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:43:23 29 4
gpt4 key购买 nike

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()来获取正确的部分。因此,此方法适用于文档正文以及页眉和/或页脚中的段落。

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

}
}

关于java - 如何使用 Apache POI 将超链接添加到 XWPFDocument 的页脚?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55275241/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com