【发布时间】:2017-10-02 13:52:41
【问题描述】:
我正在尝试创建一个 Java“脚本”,这样,给定一个模板和一个目标文档:
- 读取模板中定义的所有样式,包括文本和表格
- 将每个样式复制到目标文档中,如果样式已经存在则替换它
总而言之,这是我到现在为止的,也是基于API reference for the StyleCollection class:
Document target = new Document(targetPath);
StyleCollection targetStyles = target.getStyles();
for (Style style : source.getStyles()) {
switch (style.getType()) {
case StyleType.PARAGRAPH:
case StyleType.TABLE:
case StyleType.LIST:
if (!style.getName().trim().toLowerCase().endsWith("carattere")) {
String name = style.getName();
Style copied = styles.addCopy(style);
switch (style.getType()) {
case StyleType.PARAGRAPH:
copied.setNextParagraphStyleName(style.getNextParagraphStyleName());
// fallthrough
case StyleType.CHARACTER:
case StyleType.TABLE:
copied.setBaseStyleName(style.getBaseStyleName());
break;
default:
break;
}
copied.setName(name);
}
break;
default:
break;
}
}
target.save(savePath);
截至目前,我发现的问题如下:
- 如果我尝试使用标准的 MS Word 样式(“Normal”、“Title 1”、...),当它们被复制时,即使使用 setName()“技巧”,它们也会被复制
- 如果我改用自定义样式,我会遇到表格样式问题,其中文本颜色会发生变化
如何获得文档中所有样式的干净副本?
【问题讨论】:
标签: java ms-word aspose aspose.words