首页 > 解决方案 > 如何在 Office 插件中插入外部图像?

问题描述

我想使用以下方法将图像插入到 PowerPoint 幻灯片setSelectedDataAsyncOffice.CoercionType.Image

Office.context.document.setSelectedDataAsync(image, {
    coercionType: Office.CoercionType.Image,
    imageLeft: 5,
    imageTop: 5,
    imageWidth: 100,
    imageHeight: 100
});

该系统适用于资产,我只需将图像插入画布,然后将其转换为数据 URL:

const image = new Image();
image.src = props.url;
ctx.drawImage(image, 0, 0, canvasWidth, canvasHeight);

然后我们只需将画布插入幻灯片:

var cvs = canvas.current;
var result = cvs.toDataURL().split(",")[1];
Office.context.document.setSelectedDataAsync(result, {
    coercionType: Office.CoercionType.Image,
    imageLeft: 5,
    imageTop: 5,
    imageWidth: canvas.current.width / 3,
    imageHeight: canvas.current.height / 3
});

问题是该系统不适用于外部图像。我收到错误“SecurityError”,因为一旦在画布中插入外部图像,clean-origin 标志就会设置为 false。如何插入外部图像?

谢谢你。

标签: javascriptreactjspowerpointoffice-js

解决方案


这是在 Office 加载项中插入图像的教程。


推荐阅读