【问题标题】:Header not showing up when I'm inserting a document插入文档时标题不显示
【发布时间】:2016-08-17 13:19:29
【问题描述】:

我正在开发一个 Word 插件,用户可以在其中选择一些从 SharePoint 加载的预定义模板 (docx) 文档。用户通过向导设置文档中的内容控件。到目前为止,体验非常好。

但是,我在加载带有标题的 docx 文件时遇到了问题。

我正在使用这个函数来加载 docx 文件:(下面的完整代码)

body.insertFileFromBase64(templateDoc.base64String, Word.InsertLocation.end);

这可行,但有时源文档的标题不存在。或者其他时候它们在所有页面上,而第一页应该不同。

问题: 插入带有页眉和页脚的文档应该有效吗?我做错了什么吗?

private applyTemplate(template: Template): void {
if (!Office.context.requirements.isSetSupported("WordApi", 1.2)) {
    this.errorMessage = 'Deze versie van Word wordt niet ondersteund';
    this.showError = true;
    return;
}

let calls: [
    ng.IPromise<TemplateFile>
] = [
        this.appService.getTemplateDocument(template.templateId)
    ];

this.q.all(calls)
    .then((results: any[]) => {
        let templateDoc: TemplateFile = results[0].data;

        Word.run((context) => {
            let body = context.document.body;
            let sections = context.document.sections;
            context.load(sections, 'body/style');
            body.clear();
            return context.sync()
                .then(() => {
                    sections.items[0].getHeader(Word.HeaderFooterType.primary).clear();
                    sections.items[0].getFooter(Word.HeaderFooterType.primary).clear();

                    return context.sync()
                        .then(() => {
                            body.insertFileFromBase64(templateDoc.base64String, Word.InsertLocation.end);
                            this.appService.setTemplateSelected(template);                                      
                            return context.sync()
                                .then(() => {
                                    this.go('/customers');
                                    this.scope.$apply();
                                }, ((result: OfficeErrorMessage) => {
                                    this.setErrorState(result);
                                }));
                        }, ((result: OfficeErrorMessage) => {
                            this.setErrorState(result);
                        }));
                }, ((result: OfficeErrorMessage) => {
                    this.setErrorState(result);
                }));
        });
    }, ((result: ng.IHttpPromiseCallbackArg<ErrorMessage>) => {
        this.errorMessage = result.data.exceptionMessage;
        this.showError = true;
        this.scope.$apply();
    }));
}

***编辑 我看到这是一个新版本: https://github.com/OfficeDev/office-js-docs/blob/WordJs_1.3_Openspec/word/resources/application.md

和我做的有什么区别?

【问题讨论】:

  • 如果切换到 Word.InsertLocation.replace,页眉/页脚是否正确插入?我怀疑存在现有内容时 Word 没有插入页眉/页脚。
  • 如果我的问题不清楚,我很抱歉,但我在文档中插入了一个 docx 文件。源文件第一页没有页眉,但插入后,第一页有页眉。所以它与来源不一样。我确实在清除标题时遇到了一些问题,但在我的情况下,这些问题现在有一个解决方法。

标签: ms-word office-js


【解决方案1】:

这是一种设计行为,当您使用 insertFileFromBase64 方法插入文件时,我们不会替换文档的页眉/页脚和 customXMLParts(那里可能已经有页眉和页脚,对于 XMLParts 也是如此)。

因此,如果您需要更新页眉和页脚,则必须在插入文件后进行。 (通过api可以为每个部分插入word中支持的3种标题,第一页,偶数页,奇数页)

希望这会有所帮助。 谢谢! 胡安。

【讨论】:

  • 这有帮助,虽然有点出乎意料
猜你喜欢
  • 2023-03-19
  • 2015-04-30
  • 1970-01-01
  • 1970-01-01
  • 2020-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-14
相关资源
最近更新 更多