使用 Aspose.Words 将样式从模板复制到多个文档
Copy styles from template to many documents using Aspose.Words
我正在尝试创建一个 Java “脚本”,这样,给定一个模板和一个目标文档:
- 读取模板中定义的所有样式,包括文本和 tables
- 将每个样式复制到目标文档,如果样式已存在则替换它
总而言之,这就是我到目前为止所得出的结论,也是基于 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()“技巧”它们也会被复制
- 如果我改为使用自定义样式,我会遇到 table 样式的问题,其中文本颜色会发生变化
如何获得文档中所有样式的干净副本?
if I tried using the standard MS Word styles (“Normal”, “Title 1”, …),
when they’re copied over they get duplicated even using the setName()
“trick”
请使用以下示例代码片段,它将帮助您获得没有样式重复的输出。
Document source = new Document(MyDir + "template.doc");
Document target = new Document(MyDir + "target.doc");
StyleCollection sourceStyles = source.getStyles();
StyleCollection targetStyles = target.getStyles();
System.out.println("SORGENTE = " + sourceStyles.getCount() + " stili");
System.out.println("DESTINAZIONE = " + targetStyles.getCount() + " stili");
for (Style style : sourceStyles) {
String name = style.getName();
if (name.endsWith("Carattere")) continue;
if (style.getType() == StyleType.PARAGRAPH
|| style.getType() == StyleType.TABLE)
{
Style copied = targetStyles.addCopy(style);
copied.setBaseStyleName(style.getBaseStyleName());
if (style.getType() == StyleType.PARAGRAPH) {
copied.setNextParagraphStyleName(style.getNextParagraphStyleName());
}
copied.setName(name);
System.out.println("COPIA STILE " + name + " -> " + copied.getName());
}
}
target.cleanup();
target.save(MyDir + "output.docx");
if I instead use custom style, I have a problem with table styles, in
which the text color changes
请注意Aspose.Words 模仿 MS Word 行为。如果您 import the styles from your template to target document using MS Word,它将显示与 Aspose.Words 相同的结果。
我作为开发人员 Evangelist 在 Aspose 工作。
我正在尝试创建一个 Java “脚本”,这样,给定一个模板和一个目标文档:
- 读取模板中定义的所有样式,包括文本和 tables
- 将每个样式复制到目标文档,如果样式已存在则替换它
总而言之,这就是我到目前为止所得出的结论,也是基于 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()“技巧”它们也会被复制
- 如果我改为使用自定义样式,我会遇到 table 样式的问题,其中文本颜色会发生变化
如何获得文档中所有样式的干净副本?
if I tried using the standard MS Word styles (“Normal”, “Title 1”, …), when they’re copied over they get duplicated even using the setName() “trick”
请使用以下示例代码片段,它将帮助您获得没有样式重复的输出。
Document source = new Document(MyDir + "template.doc");
Document target = new Document(MyDir + "target.doc");
StyleCollection sourceStyles = source.getStyles();
StyleCollection targetStyles = target.getStyles();
System.out.println("SORGENTE = " + sourceStyles.getCount() + " stili");
System.out.println("DESTINAZIONE = " + targetStyles.getCount() + " stili");
for (Style style : sourceStyles) {
String name = style.getName();
if (name.endsWith("Carattere")) continue;
if (style.getType() == StyleType.PARAGRAPH
|| style.getType() == StyleType.TABLE)
{
Style copied = targetStyles.addCopy(style);
copied.setBaseStyleName(style.getBaseStyleName());
if (style.getType() == StyleType.PARAGRAPH) {
copied.setNextParagraphStyleName(style.getNextParagraphStyleName());
}
copied.setName(name);
System.out.println("COPIA STILE " + name + " -> " + copied.getName());
}
}
target.cleanup();
target.save(MyDir + "output.docx");
if I instead use custom style, I have a problem with table styles, in which the text color changes
请注意Aspose.Words 模仿 MS Word 行为。如果您 import the styles from your template to target document using MS Word,它将显示与 Aspose.Words 相同的结果。
我作为开发人员 Evangelist 在 Aspose 工作。