- Image resizing
- Changing size
- Text wrapping style
- Positioning the image
- See Also
Contact Support
Image in Angular Document editor component
27 Apr 20247 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.
import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { ButtonModule } from '@syncfusion/ej2-angular-buttons'
import { DocumentEditorAllModule } from '@syncfusion/ej2-angular-documenteditor'
import { Component, ViewEncapsulation, ViewChild } from '@angular/core';
import {
DocumentEditorComponent, EditorService, SelectionService, SfdtExportService, EditorHistoryService, ImageResizerService
} from '@syncfusion/ej2-angular-documenteditor';
@Component({
imports: [
ButtonModule,
DocumentEditorAllModule
],
standalone: true,
selector: 'app-container',
//specifies the template string for the Document Editor component.
template: `<div>
<input id="insertImageButton" #insert_Image_Button style="position:fixed; left:-100em" type="file" (change)="onInsertImage($event)" accept=".jpeg,.jpg,.png,.gif,.bmp">
<button ejs-button (click)="insertImageButtonClick()" >Insert Image</button>
<ejs-documenteditor #document_editor id="container" height="330px" style="display:block" [enableSfdtExport]=true [enableWordExport]=true [enableSelection]=true [enableEditor]=true [isReadOnly]=false [enableImageResizer]=true> </ejs-documenteditor>
</div>`,
encapsulation: ViewEncapsulation.None,
providers: [EditorService, SelectionService, SfdtExportService, ImageResizerService]
})
export class AppComponent {
@ViewChild('document_editor')
public documentEditor?: DocumentEditorComponent;
public insertImageButtonClick(): void {
let pictureUpload: HTMLInputElement = document.getElementById("insertImageButton") as HTMLInputElement;
pictureUpload.value = '';
//Open file picker.
pictureUpload.click();
}
public onInsertImage(args: any): void {
let documentEditor: DocumentEditorComponent = this.documentEditor as DocumentEditorComponent;
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]) {
//Get selected image.
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 the image into Document Editor.
documentEditor.editor.insertImage(base64String, this.width, this.height);
})
image.src = base64String;
};
//Convert the image in to base64 format.
reader.readAsDataURL(path);
}
//Safari does not Support FileReader Class
} else {
let image = document.createElement('img');
image.addEventListener('load', function () {
//Insert the image into Document Editor.
documentEditor.editor.insertImage(args.target.value);
})
image.src = args.target.value;
}
}
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));
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.
Changing size
Document Editor expose API to get or set the size of the selected image. Refer to the following sample code.
this.documentEditor.selection.imageFormat.width = 800;
this.documentEditor.selection.imageFormat.height = 800;
Note: Images are stored and processed(read/write) as base64 string in DocumentEditor. 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
DocumentEditor 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.