Search results

Images in Angular DocumentEditor component

01 Dec 2021 / 3 minutes to read

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

Source
Preview
app.component.ts
app.module.ts
main.ts
Copied to clipboard
import { Component, ViewEncapsulation, ViewChild } from '@angular/core';
import {
    DocumentEditorComponent, EditorService, SelectionService, SfdtExportService, EditorHistoryService, ImageResizerService
} from '@syncfusion/ej2-angular-documenteditor';

@Component({
      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;
        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;
        }
    }
}
Copied to clipboard
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 { AppComponent } from './app.component';



/**
 * Module
 */
@NgModule({
    imports: [
        BrowserModule,
        ButtonModule,
        DocumentEditorAllModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
export class AppModule { }
Copied to clipboard
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';

enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

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

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.

Copied to clipboard
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.

See Also