编程语言
首页 > 编程语言> > javascript – Office加载项开发:在Word 2016中插入图像/图片

javascript – Office加载项开发:在Word 2016中插入图像/图片

作者:互联网

我想问一下在JavaScript中将图像/图片插入文档的最佳方法是什么?我正在权衡在线和离线图像,但不知道从哪里开始.

我已经尝试搜索API参考但由于缺少代码示例,我无法完全理解“如何”.

任何帮助将不胜感激.干杯!

解决方法:

我们可以通过OOXML,Html将图像插入到文档中.使用Html插入在线图像很容易.以下是供您参考的示例:

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

新版本Word API还支持使用基本64数据格式的内嵌图片插入图像.您可以参考以下示例:

  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/ATPXobtHv0evQmcq9U8Ywf4Sf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