getSelectedDataAsync和getHtml无法在word桌面应用中获取base64

getSelectedDataAsync and getHtml cannot get base64 in desktop application of word

sharedapi和wordapi的getSelectedDataAsync和getHtml分别无法获取word桌面应用中图片的base64编码?求教如何获取word桌面应用中选中图片的base64编码.

您可以使用 getBase64ImageSrc method of the Word API 检索图像的 Base64 编码版本。

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);
})

当然你可以得到图片的base64,这就是你需要做的。 这只是访问图像集合以供选择,只需在 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);
    }
}