【问题标题】:Copy styles from template to many documents using Aspose.Words使用 Aspose.Words 将样式从模板复制到多个文档
【发布时间】: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


    【解决方案1】:

    如果我尝试使用标准的 MS Word 样式(“Normal”、“Title 1”、...), 当它们被复制时,即使使用 setName() 也会被复制 “诡计”

    请使用以下示例代码sn-p,它将帮助您获得不重复样式的输出。

    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");
    

    如果我改用自定义样式,我的表格样式有问题,在 文字颜色变化的地方

    请注意 Aspose.Words 模仿 MS Word 的行为。如果您import the styles from your template to target document using MS Word,它将显示与 Aspose.Words 相同的结果。

    我与 Aspose 合作,担任开发人员宣传员。

    【讨论】:

    • 我尝试移动copied.setName()作为复制过程的最后一步,这是我的代码和你的代码之间的唯一区别,结果仍然相同。不,如果我使用原生 MS Word 功能导入样式,我会得到不同的结果。
    • 我想我找到了问题,它只对 DOC 文件失败,而对 DOCX 文件正常工作。
    • 我的观点是正确的:即使在样式名称上添加了您建议的检查,排除所有以“Carattere”结尾的内容,我仍然在生成的文档中找到重复项
    • 请分享您的输出文档。如果您的代码与上述共享代码不同,也请分享您的代码,并说明您用于此测试的输入文档,因为您共享了三组文档。我们将再次调查该问题并为您提供相应信息。
    • 这是一个 ZIP 存档,其中包含一个测试模板文件、一个目标文件、使用我在问题中编辑的代码的合并结果以及直接使用 MS WORD 合并样式后获得的预期结果:@987654322 @
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    相关资源
    最近更新 更多