2016-08-17 44 views
0

我正在開發一個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

什麼是與我在做什麼區別呢?

+0

如果切換到Word.InsertLocation.replace,頁眉/頁腳是否正確插入?我懷疑Word存在內容時沒有插入頁眉/頁腳。 –

+0

如果我的問題不清楚,我很抱歉,但是我在文檔中插入了一個docx文件。源文件在第一頁上沒有標題,但在插入後,它在第一頁上有一個標題。所以它與源不一樣。我確實有一些問題需要清除頭文件,但是現在我已經有了一個解決方法。 –

回答

1

這是設計行爲,當您使用insertFileFromBase64方法插入文件時,我們既不替換文檔的頁眉/頁腳也不替換文檔的customXMLParts(可能已有頁眉和頁腳,對於XMLParts也是如此)。

因此,如果您需要更新頁眉和頁腳,則必須在插入文件後執行此操作。 (使用api你可以插入每種部分,第一頁,甚至是奇數頁支持的3種類型的頭文件)

希望這會有所幫助。 謝謝! 胡安。

+0

這有助於,雖然有點意外 –

相關問題