Images in Vue DOCX Editor

28 Aug 202612 minutes to read

Vue DOCX Editor (Document Editor) supports common raster image formats such as 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 DOCX 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 setup>
import { provide, ref } from "vue";
import { DocumentEditorComponent as EjsDocumenteditor, Selection, Editor, SfdtExport, WordExport } from '@syncfusion/ej2-vue-documenteditor';

const documenteditor = ref(null);
const insertImageButton = ref(null);

provide('DocumentEditor', [SfdtExport, WordExport, Selection, Editor]);

const onInsertImage = function (args) {
    if (navigator.userAgent.match('Chrome') || navigator.userAgent.match('Firefox') || navigator.userAgent.match('Edge') || navigator.userAgent.match('MSIE') || navigator.userAgent.match('.NET')) {
        if (args.target.files[0]) {
            let path = args.target.files[0];
            let reader = new FileReader();
            reader.onload = function (frEvent) {
                let base64String = frEvent.target.result;
                let image = document.createElement('img');
                image.addEventListener('load', function () {
                    //Insert image in Document Editor.
                    documenteditor.value.ej2Instances.editor.insertImage(base64String, width, 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.value.ej2Instances.editor.insertImage(args.target.value);
        })
        image.src = args.target.value;
    }
}
const insertImageButtonClick = function () {
    insertImageButton.value.value = '';
    insertImageButton.value.click();
}

</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-documenteditor/styles/material.css";
</style>
<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 { DocumentEditorComponent, Selection, Editor, SfdtExport, WordExport } from '@syncfusion/ej2-vue-documenteditor';

export default {
    name: "App",
    components: {
        "ejs-documenteditor": DocumentEditorComponent
    },
    data: function () {
        return {
        };
    },
    provide: {
        //Inject require modules.
        DocumentEditor: [SfdtExport, WordExport, Selection, Editor]
    },
    methods: {
        onInsertImage: function (args) {
            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) {
                        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 are internally converted to base64 strings, whereas online images are preserved as URLs.

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

Image resizing

DOCX Editor provides a 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 a mouse or touch interactions. This resizer appears as follows.

Image

Changing size

DOCX Editor exposes an 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;

Images are stored and processed (read/write) as base64 string in DOCX Editor. The online image URL is preserved as a URL in DocumentEditor 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

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

See Also