Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

body.clear() and body.insertFileFromBase64() not working as expected

I have the requirement in which I need to clear/remove the existing content from a Word document and then insert new Base 64 encoded document.

The following is a code snippet where I'm first clearing the document body and then inserting the new document.

Also, If I comment the body.clear() then InsertLocation.start and InsertLocation.end is working fine. InsertLocation.replace does not work in regardless of calling body.clear() or not.

It also does not clear some items such as water marks, the header, or the footer.

I'm expecting that body.clear() should result a document which is same as a new blank document.

showDocumentInWord(documentData, docName) {
    let self = this;
    window.Word.run(function (context) {
            let body = context.document.body;
            body.clear();
            body.insertFileFromBase64(documentData, 
                window.Word.InsertLocation.replace);
            return context.sync().then(function () {
                // Show success message here 
            });
        })
        .catch(function (error) {
            if (error instanceof window.OfficeExtension.Error) {
                // console.log('Debug info: ' + JSON.stringify(error.debugInfo));
            }
        });
};
like image 926
Chandan Avatar asked Dec 20 '25 04:12

Chandan


1 Answers

Since you are clearing the document body, you should use "start" rather than "replace" for the insertion location.

As for the header and footer, your code is only targeting document.body. In order to clear the header and footer of the document you will need to specifically clear those sections:

return Word.run(function (context) {
    let sections = context.document.sections;
    sections.load();
    return context.sync()
        .then(function () {
            sections.items.forEach(function (section) {
                // Clear the Body
                section.body.clear();

                // Clear any Headers
                section.getHeader('primary').clear();
                section.getHeader('firstPage').clear();
                section.getHeader('evenPages').clear();

                // Clear any Footers
                section.getFooter('primary').clear();
                section.getFooter('firstPage').clear();
                section.getFooter('evenPages').clear();
            });                
        });        
});

You may also want to take a look at the beta release of Word JavaScript API 1.4. Version 1.4 adds a createDocument(base64File: string) method that allows you to create an entirely new document based on a base64File. It also adds an document.open() method that lets you open up the document object you created using createDocument.

like image 189
Marc LaFleur Avatar answered Dec 23 '25 22:12

Marc LaFleur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!