简体   繁体   English

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

[英]Copy styles from template to many documents using Aspose.Words

I am trying to create a Java “script” such that, given a template and a target document: 我试图创建一个Java“脚本”,以便给定模板和目标文档:

  • reads all styles defined in the template, both for text and tables 读取模板中定义的所有样式,包括文本和表格
  • copies each style onto the target document, replacing the style if it already exists 将每个样式复制到目标文档,如果样式已经存在,则替换样式

To summarize, this is what I have come up until now, also based on the API reference for the StyleCollection class : 总而言之,这是我到目前为止提出的内容,也是基于StyleCollectionAPI参考

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

As of now, the problems I have identified are the following: 到目前为止,我发现的问题如下:

  • 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” 如果我尝试使用标准的MS Word样式(“普通”,“标题1”等),则当它们被复制时,即使使用setName()“技巧”,它们也会被复制
  • if I instead use custom style, I have a problem with table styles, in which the text color changes 如果我改用自定义样式,则表格样式会出现问题,其中文本颜色会更改

How do I get a clean copy of all the styles in the document? 如何获得文档中所有样式的干净副本?

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” 如果我尝试使用标准的MS Word样式(“普通”,“标题1”等),则当它们被复制时,即使使用setName()“技巧”,它们也会被复制

Please use following sample code snippet, it will help you to get output without style duplication. 请使用以下示例代码片段,它将帮助您获得输出而无需样式重复。

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 如果我改用自定义样式,则表格样式会出现问题,其中文本颜色会更改

Please note Aspose.Words mimics MS Word behavior. 请注意Aspose.Words模仿MS Word行为。 If you import the styles from your template to target document using MS Word , it will show same results as Aspose.Words does. 如果使用MS Word样式从模板导入模板到目标文档 ,它将显示与Aspose.Words相同的结果。

I work with Aspose as developer Evangelist. 我与Aspose合作,担任开发人员福音。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM