如何从URL将图像插入Word [英] How to insert an image into Word from a URL

查看:256
本文介绍了如何从URL将图像插入Word的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在处理 Office.js 为Word添加,我正在尝试插入来自给定网址的图片。我正在查看 Office.js 文档,该文档位于:

I am currently working on an Office.js add in for Word and I am trying to insert an image from a given Url. I was reviewing the Office.js documentation which is located at :

InlinePicture对象(Word的JavaScript API)

I看到它们可能具有内置功能,可以通过getBase64ImageSrc()从img url获取base64表示。开发办公室网站上的文档有误导性或不正确。

I see that they may have a built in functionality of getting the base64 representation from a img url by "getBase64ImageSrc()". The documentation on the dev office website is either misleading or incorrect.

想看看是否有人建立了一个使用<$ c从网址插入图片的文字插件$ C> getBase64ImageSrc()?或者我正朝着错误的方向前进。

Looking to see if anyone has built a word-addin that inserts an image from a url using "getBase64ImageSrc()"? Or am I looking in the wrong direction.

推荐答案

需要详细说明迈克的答案,以避免混淆。

Need to elaborate more on Mike's answer, to avoid confusion.

Staffer901:你在这篇文章中谈论了两个不同的主题。

Staffer901: you are talking about 2 different subjects on this post.


  1. 插入图像到文档。我认为这是你的底线问题:如何插入带有图像URL的图像。迈克尔提到的选项,基本上是为图像插入经典HTML,可以使用,但我不建议你使用它们中的任何一个。原因是因为你真正在做的是存储对连接到互联网依赖关系的图像的引用,这意味着必须连接任何使用该文档的用户才能看到图像。

  1. Inserting Images to the document. which i think is your bottom line question: how to insert an image with an image URL. The options that Michael mentioned, which are basically to insert classic HTML for an image, will work but i would NOT recommend you to use any of them. The reason why is because really what you are doing is storing a reference to the image that has a connection to the internet dependency, which means any user consuming that document must be connected to see the image.

我建议你做的图像插入(永久插入:))是使用range.insertInlinePictureFromBase64方法。您需要有一个额外的步骤来将URL中的图像编码为base64字符串,这是方法接受的输入参数和这里是一个很好的讨论如何实现这一点。查看下面的示例显示在文档的第一段插入InlinePicture,假设你有base64。请注意,您可以获取当前插入点并在需要时插入图片。 insertInlinePictureFromBase64 是从范围继承的任何对象的方法,如正文,段落,内容控件等。

What i DO recommend you to do for image insertion (permanent insertion :) ) is to use the range.insertInlinePictureFromBase64 method. You need to have an additional step to encode the image in the URL to a base64 string, which is what the methods accepts as input parameter and here is a good discussion on how to achieve this.. Check out a sample below showing inserting an InlinePicture on the first paragraph of the document, assumes you have the base64. Note that you can get the current insertion point and insert the pic there if needed as well. insertInlinePictureFromBase64 is a method of any objects that inherits from range, like body, paragraph, content control etc.

这里是一个示例:

// Run a batch operation against the Word object model.
Word.run(function (context) {

    // Create a proxy object for the paragraphs collection.
    var paragraphs = context.document.body.paragraphs;

    // Queue a commmand to load the style property for all of the paragraphs.
    context.load(paragraphs);

    // Synchronize the document state by executing the queued commands,
    // and return a promise to indicate task completion.
    return context.sync().then(function () {

        // Queue a command to get the first paragraph.
        var paragraph = paragraphs.items[0];

        var b64encodedImg = "iVBORw0KGgoAAAANSUhEUgAAAB4AAAANCAIAAAAxEEnAAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAACFSURBVDhPtY1BEoQwDMP6/0+XgIMTBAeYoTqso9Rkx1zG+tNj1H94jgGzeNSjteO5vtQQuG2seO0av8LzGbe3anzRoJ4ybm/VeKEerAEbAUpW4aWQCmrGFWykRzGBCnYy2ha3oAIq2MloW9yCCqhgJ6NtcQsqoIKdjLbFLaiACnYyf2fODbrjZcXfr2F4AAAAAElFTkSuQmCC";

        // Queue a command to insert a base64 encoded image at the beginning of the first paragraph.
        paragraph.insertInlinePictureFromBase64(b64encodedImg, Word.InsertLocation.start);

        // Synchronize the document state by executing the queued commands,
        // and return a promise to indicate task completion.
        return context.sync().then(function () {
            console.log('Added an image to the first paragraph.');
        });
    });
})
.catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});

最后请注意,Michaels提到的setSelectedDataAsync方法最近更新以支持图像插入,您还需要提供图像的base64,但好处是您可以向后兼容(它也适用于2013客户端)这里是一个代码示例:

Finally note that the setSelectedDataAsync method that Michaels mentioned, was recently updated to support image insertion, you also need to supply the base64 of the image but the benefit is that you get backwards compatibility (it will work with 2013 clients as well) here is a code sample of this:

// assumes a valid base64 is provided as the first parameter.
Office.context.document.setSelectedDataAsync(mybase64, { coercionType: 'image' }, function (result) {
            if (result.status == 'succeeded')
                app.showNotification("Image inserted");
            else
                app.showNotification("Error:" + result.error.message + " : " + error.name)


        })


  1. 从文档中使用图像。这是关于从文档中的现有图像中获取base64 。我们有一个身体。您可以使用inlinePictures集合来获取文档中的所有图像,并使用getBase64方法获取二进制文件的base64编码表示。我想知道为什么这在文档中令人困惑,你能详细说明吗?

  1. Consuming images from the document. This is about getting the base64 from existing images in the document. We have a body. inlinePictures collection you can use to get all the images in the document, and you use the getBase64 method to get the base64 encoded representation of the binary. I would like to know why this is confusing in the documentation, can you please elaborate on that?

我希望这很有用。
谢谢,编码愉快!
-Juan。

I hope this is useful. thanks and happy coding! -Juan.

这篇关于如何从URL将图像插入Word的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆