我正在尝试将命名样式应用于 XWPFDocument 中的单个运行,但我看到了奇怪的结果。
XWPFRun的javadoc 描述了 setStyle 方法,但该样式似乎未应用于最终文档。我说会出现,因为在 Finder 的 QuickLook 预览中,样式确实会按预期出现在运行中。在下面的示例中,我将命名样式应用于超链接,它在右侧的预览中按预期显示,但在左侧的 Word 中没有显示。
很明显 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");
}
摇曳的蔷薇
相关分类