Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Office Add-in development: Insert image/picture in Word 2016

I would like to ask as to what is the best way to insert an image/picture to the document in JavaScript? I am weighing in between an online and offline images but don't know where to start.

I've tried searching the API references but with the lack of code samples, I am not able to fully understand the "how".

Any help will be greatly appreciated. Cheers!

like image 889
vegavegs Avatar asked Jul 04 '16 07:07

vegavegs


People also ask

How do you enable insert a picture in Word?

Insert a picture in Word, PowerPoint, or Excel Click the location in your document where you want to insert a picture. On the Insert tab, click Pictures. Select the option you want to use for inserting pictures.

What are the correct steps for inserting a picture into a Word 2016 document?

To insert a picture from a file:Place the insertion point where you want the image to appear. Select the Insert tab on the Ribbon, then click the Pictures command. The Insert Picture dialog box will appear. Navigate to the folder where your image is located, then select the image and click Insert.

Which toolbar holds the insert image option?

Insert Image dialog 2) Choose Insert > Image on the Menu bar or click the Insert Image icon on the Standard toolbar.

Does Word automatically embed images?

Images inserted from a file are embedded by default; you have to choose to link them. Images pasted from the Internet will usually be pasted as links; you can select such a picture (when it is displayed as a picture) and press Ctrl+Shift+F9 to unlink it and embed it in the document.


1 Answers

We can insert images into document via OOXML, Html. And it is easy that using Html to insert online images. Here is an example for your reference:

  function InsertImageHtml() {

    var imgHTML = "<img " +
"    src=''"
+ " alt ='apps for Office image1' />       ";

    Office.context.document.setSelectedDataAsync(
        imgHTML, { coercionType: "html" },
        function (asyncResult) {
            if (asyncResult.status == "failed") {
                write('Error: ' + asyncResult.error.message);
            }
        });
}

And the new version Word API also support to insert the image with base 64 data format inline pictures. You can refer the sample below:

  function insertImageBase64New() {

    Word.run(function (context) {

        var body = context.document.body;

        body.insertInlinePictureFromBase64(getBase64(), Word.InsertLocation.start);

        return context.sync().then(function () {
            console.log('Added base64 pictures to the beginning of the document body.');
        });
    })
    .catch(function (error) {
        console.log('Error: ' + JSON.stringify(error));
        if (error instanceof OfficeExtension.Error) {
            console.log('Debug info: ' + JSON.stringify(error.debugInfo));
        }
    });
}

function getBase64() {
    return "iVBORw0K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