Leo
Leo

Reputation: 171

getSelectedDataAsync and getHtml cannot get base64 in desktop application of word

getSelectedDataAsync and getHtml of shared api and word api respectively cannot get the base64 encoding of image in desktop application of word?please suggest how to get base64 of selected image in desktop application of word.

Upvotes: 1

Views: 266

Answers (2)

Juan Balmori
Juan Balmori

Reputation: 5036

of course you can get the base64 of image this is what you need to do. this is just accessing the collection of images for selection just do it on context.document.getSelection().inlinePictures.getFirst()

async function getImage() {
    try {
        Word.run(async (context) => {
            const firstPicture = context.document.body.inlinePictures.getFirst();
            context.load(firstPicture);
            await context.sync();

            const base64 = firstPicture.getBase64ImageSrc();
            await context.sync();

            console.log(base64.value);
        })
    }
    catch (exception) {
        OfficeHelpers.Utilities.log(exception);
    }
}

Upvotes: 1

Marc LaFleur
Marc LaFleur

Reputation: 33094

You can retrieve a Base64 Encoded version of the image using the getBase64ImageSrc method of the Word API.

Word.run(function (context) {
    var base64Image;

    var range = context.document.getSelection(); // Get selection
    var images = range.inlinePictures; // Get images from selection
    context.load(images); // Load images from document
    return context.sync()
        .then(function () {
            // Make sure we have at least 1 image
            if (images.items.length > 0)
                // grab the base64 encoded image
                image = images.getFirst().getBase64ImageSrc();
            else
                console.log("No images selected");
        })
        .then(context.sync)
        .then(function () {
            // image.value now contains the base64 encoded image
            console.log(image.value);
        })
        .then(context.sync);
})

Upvotes: 0

Related Questions