gpt4 book ai didi

java - 通过样式 ID 在 XWPFRun 上设置样式

转载 作者:行者123 更新时间:2023-12-02 10:39:41 25 4
gpt4 key购买 nike

我正在尝试将命名样式应用于 XWPFDocument 中的各个运行,但我看到了奇怪的结果。

javadoc for XWPFRun describes the setStyle method ,但样式似乎未应用于最终文档中。我说出现,是因为在Finder 的QuickLook 预览中,样式确实按预期出现在运行中。在下面的示例中,我将命名样式应用于超链接,该样式按预期显示在右侧的预览中,但在左侧的 Word 中则不然。

enter image description here

很明显 POI 实际上正在做一些事情来应用样式,但 Word 并没有渲染样式。我尝试了其他几个 .docx 阅读器,所有这些都产生了类似的结果。

因此,我开始剥离样式并将属性单独应用到运行,这在 Word 中确实。这是我似乎错过了什么的事情之一。我当然可以编写一个可以以现有样式读取并将其应用于这样的运行的例程,但我宁愿不这样做。我已经寻找答案,但这部分 POI 似乎仍在进行中。

那么我是否只是错过了一些明显的东西,或者我是否只能忍气吞声并以痛苦的方式做到这一点?

//This does not work.
run.setStyle(styleId);

if(docStyles.styleExist(styleId))
{

/*
In order to set the style on the run, we need to manually
determine the properties of the style, and set them on the
run individually.

This makes no sense.
*/
XWPFStyle style = docStyles.getStyle(styleId);

CTStyle ctStyle = style.getCTStyle();
CTRPr ctRpr = ctStyle.getRPr();

if (ctRpr.isSetB())
{
CTOnOff onOff = ctRpr.getB();
STOnOff.Enum stOnOff = onOff.getVal();

boolean bold = (stOnOff == STOnOff.TRUE);

run.setBold(bold);
}
if(ctRpr.isSetU())
{
CTUnderline underline = ctRpr.getU();
STUnderline.Enum val = underline.getVal();

UnderlinePatterns underlinePattern = UnderlinePatterns.valueOf(val.intValue());

run.setUnderline(underlinePattern);
}
// ... //
}
else
{
System.out.println("404: Style not found");
}

最佳答案

如果XWPfDocument是从模板创建的,那么该模板必须已包含命名样式“Hyperlink”。这意味着,它必须包含在 /word/styles.xml 中潜在样式中的条目

...
<w:latentStyles...
...
<w:lsdException w:name="Hyperlink" w:qFormat="1"/>
...

以及样式定义

...
<w:style w:type="character" w:styleId="Hyperlink">
<w:name w:val="Hyperlink"/>
<w:basedOn w:val="..."/>
<w:uiPriority w:val="99"/>
<w:unhideWhenUsed/>
<w:qFormat/>
<w:rsid w:val="00072FE4"/>
<w:rPr>
<w:color w:val="0000FF" w:themeColor="hyperlink"/>
<w:u w:val="single"/>
</w:rPr>
</w:style>
...

如果这是真的,那么以下代码适用于我使用 apache poi 4.0.0 :

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

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

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

public class CreateWordStyledHyperlinkRunFromTemplate {

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(new FileInputStream("Template.docx"));

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");
XWPFStyles styles = document.getStyles();
if (styles.styleExist("Hyperlink")) {
System.out.println("Style Hyperlink exists."); //Template must contain named style "Hyperlink" already
hyperlinkrun.setStyle("Hyperlink");
} else {
hyperlinkrun.setColor("0000FF");
hyperlinkrun.setUnderline(UnderlinePatterns.SINGLE);
}

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

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

}
}

请注意,不可能创建 XWPFHyperlinkRun除了使用低级别 org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHyperlink类。

它产生:

enter image description here

关于java - 通过样式 ID 在 XWPFRun 上设置样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53016828/

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