Image in Vue Document editor component

17 Mar 20239 minutes to read

Document Editor supports common raster format images like PNG, BMP, JPEG, SVG and GIF. You can insert an image file or online image in the document using the insertImage() method. Refer to the following sample code.

The following example shows how to open bookmark dialog in Document Editor.

<template>
        <div id="app">
            <input id="insertImageButton" ref="insertImageButton" style="position:fixed; left:-100em" type="file" v-on:change="onInsertImage" accept=".jpeg,.jpg,.png,.gif,.bmp">
            <div>
                <button v-on:click='insertImageButtonClick' >Insert Image</button>
            </div>
            <ejs-documenteditor ref="documenteditor" :enableSfdtExport='true' :enableWordExport='true' :enableSelection='true' :enableEditor='true' :isReadOnly='false' height="370px" style="width: 100%;display:block" ></ejs-documenteditor>
        </div>
</template>
<script>
        import Vue from 'vue'
        import { DocumentEditorPlugin, Selection, Editor, SfdtExport, WordExport } from '@syncfusion/ej2-vue-documenteditor';

        Vue.use(DocumentEditorPlugin);

        export default {
            data: function() {
                return {
                };
            },
            provide: {
                //Inject require modules.
                DocumentEditor : [SfdtExport, WordExport, Selection, Editor]
            }
            methods: {
                onInsertImage: function(args: any): void {
                    if (navigator.userAgent.match('Chrome') || navigator.userAgent.match('Firefox') || navigator.userAgent.match('Edge') || navigator.userAgent.match('MSIE') || navigator.userAgent.match('.NET')) {
                        let documenteditor =this.$refs.documenteditor;
                        if (args.target.files[0]) {
                            let path = args.target.files[0];
                            let reader = new FileReader();
                            reader.onload = function (frEvent: any) {
                                let base64String = frEvent.target.result;
                                let image = document.createElement('img');
                                image.addEventListener('load', function () {
                                    //Insert image in Document Editor.
                                    documenteditor.ej2Instances.editor.insertImage(base64String, this.width, this.height);
                                })
                                image.src = base64String;
                            };
                            reader.readAsDataURL(path);
                        }
                        //Safari does not Support FileReader Class
                    } else {
                            let image = document.createElement('img');
                            image.addEventListener('load', function () {
                                //Insert image in Document Editor.
                                documenteditor.ej2Instances.editor.insertImage(args.target.value);
                            })
                            image.src = args.target.value;
                    }
                },
                insertImageButtonClick: function() {
                    this.$refs.insertImageButton.value = '';
                    this.$refs.insertImageButton.click();
                }
            }
        }
</script>
<style>
      @import "../../node_modules/@syncfusion/ej2-vue-documenteditor/styles/material.css";
</style>

Image files will be internally converted to base64 string. Whereas, online images are preserved as URL.

Note: EMF and WMF images can’t be inserted, but these types of images will be preserved in Document Editor when using ASP.NET MVC Web API.

Image resizing

Document Editor provides built-in image resizer that can be injected into your application based on the requirements. This allows you to resize the image by dragging the resizing points using mouse or touch interactions. This resizer appears as follows.

Image

Changing size

Document Editor expose API to get or set the size of the selected image. Refer to the following sample code.

this.$refs.documenteditor.ej2Instances.selection.imageFormat.width = 800;
this.$refs.documenteditor.ej2Instances.selection.imageFormat.height = 800;

Note: Images are stored and processed(read/write) as base64 string in Document Editor. The online image URL is preserved as a URL in Document Editor upon saving.

Text wrapping style

Text wrapping refers to how images fit with surrounding text in a document. Please refer to this page for more information about text wrapping styles available in Word documents.

Positioning the image

Document Editor preserves the position properties of the image and displays the image based on position properties. It does not support modifying the position properties. Whereas the image will be automatically moved along with text edited if it is positioned relative to the line or paragraph.

See Also