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

1

6

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!

Vday answered 4/7, 2016 at 7:44 Comment(0)
D
6

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 "iVBORw0KGgoAAAANSUhEUgAAAMcAAACwCAIAAAA0QO3oAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAYLSURBVHhe7dLRdqs6DEXR8/8/3ZsBSx32BRKSqsUyaz5KIrHs/e9LymaqlM9UKZ+pUj5TpXymSvlMlfKZKuUzVcpnqpTPVCmfqVI+U6V8pkr5TJXymSrlM1XKZ6qUz1Qpn6lSPlP1hn8bNA4wFKjegKk6hVwcYChQPcbcvEzVCwThhLfm1+FZ3T1VPHKDxoLS7+A/ZnTfVPG2B14OpFhPMp87poon/Qg/saD0FKMLSj16c7ldqnjMj/ATPXobtHv0evQmcq9U8Ywf4Sf2MNGgsYeJBo2JTLUSr9SgsaC0QXtBaYP2A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r9Oi9gy/z3pWfewdfzqvShrxJg8ZpfBao/gA/9A6+nFqxJXmZBo1z+CZQ/QF+6DQ+m135VD3QO4EPGjQ+xa+cwzc3UG9VnmiD9lOMNmh8hJ94hek7Kbkzz7WHiR69Awy9j+8DVRVN1QMvmYFffBMfN2jIVK340dP4rEFDi5LXwUum4qdP4IMePS3qXQfP+KnnP7J2jzC0QVuh2I3wjD16TwPHRIPGHiYaNPYwoUb5VNEIVBs09jDxA/yQerVTRbVHL1A9wNBH+Alt3D1VK0bfwZfaUztVDzQaNALVE/jgFaZ1rNId8aobtBeUGjTewZc9ejqhzGXxtm/iY/0tU6V8Ne6djLyJj/XnSqZqt/g/64wuMfrtk5Hek9bD2tWFhn4DYnIO32gA4z4GYTmHbzQGU6V8M6SKDzSM8qliWiMZ+lUIzh4mNKTRn4cQ9ehpVAVeiCgFqhqYj6R8pkr5TJXymSrlM1XKZ6qUz1Qpn6lSPlOlfKZK+UyV8pkq5TNVymeqlM9UKZ+pUj5TpXymSvlMlfKZKuUzVcpnqpTPVCmfqVI+U6V8pkr5TJXymSrlM1XKZ6qUz1Qpn6lSPlOlfKZK+UyV8pkq5TNVymeqlM9UKZ+pUj5TpXymSvlMlfKZKuUzVcpnqpTPVCmfqVI+U6V8pkr5TJXymSrlM1XKZ6qUz1Qpn6lStq+v/wBAeRoY6+61lgAAAABJRU5ErkJggg==";
}
Deli answered 5/7, 2016 at 3:31 Comment(3)
Is this possible in excel as well?Attentive
Looks from these docs like the first snippet doesn't work in Excel? dev.office.com/reference/add-ins/shared/…. Even worse, does that means there's no way of adding images to Excel?Jointworm
Yes, it can be done in Excel: https://mcmap.net/q/1777850/-office-js-add-in-insert-image-picture-in-excel-office-365Targum

© 2022 - 2024 — McMap. All rights reserved.