java - 使用 Aspose.Words 将样式从模板复制到许多文档

标签 java ms-word aspose aspose.words

我正在尝试创建一个 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() “技巧”,它们也会被重复
  • 如果我改用自定义样式,则会遇到表格样式问题,其中文本颜色会发生变化

如何获得文档中所有样式的干净副本?

最佳答案

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 相同的结果。

我作为开发者布道者与 Aspose 合作。

关于java - 使用 Aspose.Words 将样式从模板复制到许多文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46526865/

相关文章:

java - 在 Java 中从 InputStream 获取大小未知的字节数组?

java - 如果编译一个空的 java 文件会发生什么?

java - 加载资源文件时出现问题

c# - 用于 .NET 写入 Word 的最佳库和插件

c# - 在 excel 中生成 excel 和图表的最佳库?

java - 使用 Aspose Words for Java 插入条形图

java - 如何在 JavaFX 中将 SimpleLocalizedStringProperty (controlsfx-plugin) 与 ResourceBundle 结合使用

ios - 阅读或转换 word .doc 文件 iOS

c# - c#导出数据到word

java - 无法在从html文件呈现的aspose ppt中嵌入样式