OfficeDev / office-js-docs

[ARCHIVED] Microsoft Office Add-ins API Reference Documentation
https://docs.microsoft.com/javascript/api/overview/office
398 stars 247 forks source link

insertFileFromBase64 sometimes creates a blank page at the start of the document. #1266

Closed IanPearce closed 6 years ago

IanPearce commented 6 years ago

DocFile.docx using the following code:

            ```
Word.run(function (context) {
                    var body = context.document.body;
                    body.clear();
                    body.insertFileFromBase64(base64String, Word.InsertLocation.replace);

                    // Synchronize the document state by executing the queued commands,
                    // and return a promise to indicate task completion.
                    return context.sync().then(function () {
                        console.log('Added base64 encoded text to the beginning of the document body.');
                    });
                });

sometimes the page is inserted with an extra leading page in the document. Sometimes it is fine. I have attached one such example base64 string. If you decode this to a file using 
https://www.base64decode.org/
rename the resulting file to something.docx, the string is one page and starts from the top of that page. But if you load the same string through insertFileFromBase64 as above, there is a leading blank page.

[Base64DocxFile.txt](https://github.com/OfficeDev/office-js-docs/files/1579651/Base64DocxFile.txt)

<!---
Thank you for helping us to improve the Office Add-ins documentation.

So that we can better assist you, please note the following:
- If you have a question, need help, or are experiencing an issue with your code, we encourage you to post your question or issue on **Stack Overflow**. Tag your question with [office-js](http://stackoverflow.com/questions/tagged/office-js)
- If you have a feature suggestion, please post your idea on our [**User Voice**](https://officespdev.uservoice.com/) page, and vote for your suggestions there.
- If you have an issue with the documentation, please provide the information here, or feel free to submit a pull request with your suggested changes. We will review your contributions and update our documentation accordingly.
-->

Article:

Issue: 
IanPearce commented 6 years ago

Hi

Is there any news on this issue, or a work around that I can use, its stopping the release of a new addin for word because we get random results. Sometimes it is fine and others it is not.

Kind regards

Ian

JuaneloJuanelo commented 6 years ago

Hello Ian, sorry for the delay here. I am not sure what your scenario is intended to do, but what I noticed is the following:

  1. I decoded your base64 and noticed that there is a section break at the start of it. (that's why a blank page gets inserted)
  2. I am guessing that what you need is NOT to have a blank page, so what you need to do is basically remove that section break and then re-create the base64 string.
  3. if you DO want to insert a blank page I recommend you to change your code so enter the page break programmatically before inserting the document.

this will fix your issue. thanks! Juan.

IanPearce commented 6 years ago

Dear Juan,

Thanks for getting back to me. I think you have missed the point... If you open the docx file that was attached directly into word it opens without a preceeding page. (and I guess therefore no page/section break).

However if you take this same docx file and base64 encode this document yourself (using whatever method you like) and insert it using insertFileFromBase64 it will have a preceeding page.

If there is another way to open a base64 encoded document as a new document rather than use the insertFileFromBase64, I will happily use it. But really what I'm after is consistency between normal word open... And insertFileFromBase64.

I hope you can help and will reopen this case.

Kind regards

Ian

On 10 Jan 2018 20:00, "Juan Balmori" notifications@github.com wrote:

Hello Ian, sorry for the delay here. I am not sure what your scenarios is intended to do, but what I noticed is the following:

  1. I decoded your base64 and noticed that there is a section break on your document.
  2. I am guessing that what you need is NOT to have a blank page, so what you need to do is basically remove that section break and then re-create the base64 string.
  3. if you DO want to insert a page I recommend you to change your code so enter the page break programmatically before inserting the document.

this will fix your issue. thanks! Juan.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/OfficeDev/office-js-docs/issues/1266#issuecomment-356719067, or mute the thread https://github.com/notifications/unsubscribe-auth/AI0jm96USPnikjeZoFMOFuIa--2_m7FBks5tJRbFgaJpZM4RJ-j9 .

JuaneloJuanelo commented 6 years ago

hello Ian.. yes I did notice that, In fact, I created the bug internally to follow up on why the continuous break in your doc gets translated into a page break when inserted in the document using insertFileFromBase64. But, again if you want this to work with no problem and if its possible in the context of your scenarios you should remove the break in the document you are inserting.

that been said, yes there is a way to open an entire new doc, we recently shipped our create document API (as part of 1.3) . here is a sample on how you can use it.

async function createDoc() {
    await Word.run(async (context) => {

        context.application.createDocument(myBase64).open();

        await context.sync();
    });
}
IanPearce commented 6 years ago

Hi Juan,

Again thanks for the response and for trying to help. I tried using the code above but it popped it into a new word window in which my addin was no longer open. Is there a way to cure this? Also tried:

 Word.run(function (context) {

                        var newDoc = context.application.createDocument(binary);
                        context.load(newDoc);

                        return context.sync().then(function () {
                            newDoc.open();
                        });
                    })

Both popped a new word window with no addin...

Cheers

Ian

JuaneloJuanelo commented 6 years ago

Hello Ian! yes, that opens a completely new instance of word. you can auto open with an add-in check out this https://docs.microsoft.com/en-us/office/dev/add-ins/design/automatically-open-a-task-pane-with-a-document

you will need to add it to the document passing as parameter.

IanPearce commented 6 years ago

Hi Juan

I have tried to use the createDocument Api, but the new window, and having to open the addin makes it so hard. I have built up so much state etc by the time i get to location for editing the document, it is impossible to open the new addin and navigate it to the exact same spot. Plus it just confuses the user, its too hard to explain here. I have also noticed that inserFileFromBase64 totally ignores the headers and footers, in which case it is not a great name for it, as its not a file, its a bit of a file, not the bit that contains the file contains headers and footers. Do you know of any up coming dev work that will just simply allow me to download a base64 file and open it up in the document next to the current addin, header/footers and content?

Cheers

Ian

On 12 January 2018 at 17:21, Juan Balmori notifications@github.com wrote:

Hello Ian! yes, that opens a completely new instance of word. you can auto open with an add-in check out this https://docs.microsoft.com/en-us/office/dev/add-ins/design/ automatically-open-a-task-pane-with-a-document

you will need to add it to the document passing as parameter.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/OfficeDev/office-js-docs/issues/1266#issuecomment-357300016, or mute the thread https://github.com/notifications/unsubscribe-auth/AI0jm-LNttqIL64bOJfQw-K62jexAMd1ks5tJ5SPgaJpZM4RJ-j9 .

IanPearce commented 6 years ago

Hi Juan,

As you are an expert in all thing word, is there any way I can get the ooxml for the header/footer from the docx file and insert into the header and footer? The docx file is a zipped file structure containing xml for the varous bits, I just found the header file header2.xml in the word folder, but when I try and insert that using insertOoxml, I get a general exception (no other information). I think because it contains a image which is stored relatively to the document (in the word/media folder), i have attached the word document, renamed to zip instead of docx. If you look in it, you will see what im on about.

I think i can get the header/footer ooxml to the addin from the server, which can unzip. I

I'd really appreciate your help on this, I have developed a big addin and at the 11th hour, I have found that the inserFileFromBase64 doesnt do what I thought it did, ie insert a file into the current document, it just inserts the content. I need to try and come up with a solution, the ideal one would be if there was inserFileFromBase64 (base64, "replace","content, headers"). But I dont think that will happen any time soon.

Kind regards

Ian

Word.run(function (context) { var myOOXML = "<pkg:part pkg:name='/_rels/.rels' pkg:contentType='application/vnd.openxmlformats-package.relationships+xml' pkg:padding='512'><Relationship Id='rId1' Type=' http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument' Target='word/document.xml'/></pkg:xmlData></pkg:part><pkg:part pkg:name='/word/document.xml' pkg:contentType='application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml'><w:document xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'

"; myOOXML += "-930910-448310"; myOOXML += ""; // Create a proxy object for the document body. var body = context.document.body; //make sure there is nothing else there. body.clear();
                    // Queue a commmand to insert base64 encoded .docx

at the beginning of the content body. // You will need to implement getBase64() to pass in a string of a base64 encoded docx file. body.insertFileFromBase64(binary, Word.InsertLocation.replace);

                    var myHeader =

context.document.sections.getFirst().getHeader("primary"); myHeader.clear(); myHeader.insertOoxml(myOOXML,'start');

                    // Synchronize the document state by executing the

queued commands, // and return a promise to indicate task completion. return context.sync().then(function () { console.log('Added base64 encoded text to the beginning of the document body.'); parentViewModel.loadingData(false); parentViewModel.Title(title); resolve();

                    });
                })

On 24 January 2018 at 16:49, Ian Pearce ian.pearce.home@gmail.com wrote:

Hi Juan

I have tried to use the createDocument Api, but the new window, and having to open the addin makes it so hard. I have built up so much state etc by the time i get to location for editing the document, it is impossible to open the new addin and navigate it to the exact same spot. Plus it just confuses the user, its too hard to explain here. I have also noticed that inserFileFromBase64 totally ignores the headers and footers, in which case it is not a great name for it, as its not a file, its a bit of a file, not the bit that contains the file contains headers and footers. Do you know of any up coming dev work that will just simply allow me to download a base64 file and open it up in the document next to the current addin, header/footers and content?

Cheers

Ian

On 12 January 2018 at 17:21, Juan Balmori notifications@github.com wrote:

Hello Ian! yes, that opens a completely new instance of word. you can auto open with an add-in check out this https://docs.microsoft.com/en-us/office/dev/add-ins/design/a utomatically-open-a-task-pane-with-a-document

you will need to add it to the document passing as parameter.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/OfficeDev/office-js-docs/issues/1266#issuecomment-357300016, or mute the thread https://github.com/notifications/unsubscribe-auth/AI0jm-LNttqIL64bOJfQw-K62jexAMd1ks5tJ5SPgaJpZM4RJ-j9 .

sebasthuis commented 6 years ago

I have the same problem, it seems that when a document contains an image, it will return a general exception, which is very unfortunate.