Search code examples
javascriptoffice365office-js

How to insert an image into Word from a URL


I am currently working on an Office.js add in for Word and I am trying to insert an image from a given Url. I was reviewing the Office.js documentation which is located at :

InlinePicture class (JavaScript API for Word)

I see that they may have a built in functionality of getting the base64 representation from a img url by "getBase64ImageSrc()". The documentation on the dev office website is either misleading or incorrect.

Looking to see if anyone has built a word-addin that inserts an image from a url using "getBase64ImageSrc()"? Or am I looking in the wrong direction.


Solution

  • Need to elaborate more on Mike's answer, to avoid confusion.

    Staffer901: you are talking about 2 different subjects on this post.

    1. Inserting Images to the document. which i think is your bottom line question: how to insert an image with an image URL. The options that Michael mentioned, which are basically to insert classic HTML for an image, will work but i would NOT recommend you to use any of them. The reason why is because really what you are doing is storing a reference to the image that has a connection to the internet dependency, which means any user consuming that document must be connected to see the image.

    What i DO recommend you to do for image insertion (permanent insertion :) ) is to use the range.insertInlinePictureFromBase64 method. You need to have an additional step to encode the image in the URL to a base64 string, which is what the methods accepts as input parameter and here is a good discussion on how to achieve this.. Check out a sample below showing inserting an InlinePicture on the first paragraph of the document, assumes you have the base64. Note that you can get the current insertion point and insert the pic there if needed as well. insertInlinePictureFromBase64 is a method of any objects that inherits from range, like body, paragraph, content control etc.

    here is a sample:

    // Run a batch operation against the Word object model.
    Word.run(function (context) {
    
        // Create a proxy object for the paragraphs collection.
        var paragraphs = context.document.body.paragraphs;
    
        // Queue a commmand to load the style property for all of the paragraphs.
        context.load(paragraphs);
    
        // Synchronize the document state by executing the queued commands,
        // and return a promise to indicate task completion.
        return context.sync().then(function () {
    
            // Queue a command to get the first paragraph.
            var paragraph = paragraphs.items[0];
    
            var b64encodedImg = "iVBORw0KGgoAAAANSUhEUgAAAB4AAAANCAIAAAAxEEnAAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAACFSURBVDhPtY1BEoQwDMP6/0+XgIMTBAeYoTqso9Rkx1zG+tNj1H94jgGzeNSjteO5vtQQuG2seO0av8LzGbe3anzRoJ4ybm/VeKEerAEbAUpW4aWQCmrGFWykRzGBCnYy2ha3oAIq2MloW9yCCqhgJ6NtcQsqoIKdjLbFLaiACnYyf2fODbrjZcXfr2F4AAAAAElFTkSuQmCC";
    
            // Queue a command to insert a base64 encoded image at the beginning of the first paragraph.
            paragraph.insertInlinePictureFromBase64(b64encodedImg, Word.InsertLocation.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 an image to the first paragraph.');
            });
        });
    })
    .catch(function (error) {
        console.log('Error: ' + JSON.stringify(error));
        if (error instanceof OfficeExtension.Error) {
            console.log('Debug info: ' + JSON.stringify(error.debugInfo));
        }
    });

    Finally note that the setSelectedDataAsync method that Michaels mentioned, was recently updated to support image insertion, you also need to supply the base64 of the image but the benefit is that you get backwards compatibility (it will work with 2013 clients as well) here is a code sample of this:

    // assumes a valid base64 is provided as the first parameter.
    Office.context.document.setSelectedDataAsync(mybase64, { coercionType: 'image' }, function (result) {
                if (result.status == 'succeeded')
                    app.showNotification("Image inserted");
                else
                    app.showNotification("Error:" + result.error.message + " : " + error.name)
    
    
            })

    1. Consuming images from the document. This is about getting the base64 from existing images in the document. We have a body. inlinePictures collection you can use to get all the images in the document, and you use the getBase64 method to get the base64 encoded representation of the binary. I would like to know why this is confusing in the documentation, can you please elaborate on that?

    I hope this is useful. thanks and happy coding! -Juan.