Change word document style with an app for Office

好久不见. 提交于 2019-12-02 02:33:49

The answer to you question is yes and now, the word api is a bit new and missing some functionalities which may make a developer life easier. First, headers are not supported in the word api, a work around for this is to create a Content control and use it as a header. I am currently using this work around in a word app that I use.

Now for styles, styles are awesome to use. If a user's pc currently have those styles installed on their pc you can easily reference those styles. However, as a developer you may have found out that life is not easy. So I too ran int this issue and approached it by first inserting my desired content, then making a collection of paragraphs proxy object. I then load both both objects, and sync. After I iterate through the collection of paragraphs and add my custom styles which is an preset object which I made. the following function will show you what I tried to explain. I hope this help

    function InsertHtml(content, styleSelection) {

    Word.run(function (context) {

        var range = context.document.body.insertHtml(content, "end");
        var paragraphs = context.document.body.paragraphs;

        return context.sync().then(function () {

            var index = $.map(headerStyles, function (obj, index) {
                if (obj.name == styleSelection) {
                    return index;
                }
            })
            for (var x = 0; x < paragraphs.items.length; x++) {

                var paragraph = paragraphs.items[x];
                paragraph.font.name = headerStyles[index].fontName;
                paragraph.font.color = headerStyles[index].color;
                paragraph.font.size = headerStyles[index].size;
                paragraph.leftIndent = headerStyles[index].indent;
                //paragraph.lineSpacing = headerStyles[index].lineSpacing;
                //paragraph.alignment = headerStyles[index].alignment;                          
            }



            return context.sync();
        });
    })
    .catch(feedBackMessage);
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!