To print the document, use the print
method from document editor instance.
Refer to the following example for showing a document and print it.
import { Component, ViewEncapsulation, ViewChild } from '@angular/core';
import {
DocumentEditorComponent, PrintService
} from '@syncfusion/ej2-angular-documenteditor';
@Component({
selector: 'app-container',
template: `<div style="width:100%;height:330px">
<button ejs-button (click)="onPrint()" >Print</button>
<ejs-documenteditor #document_editor style="width:100%;height:100%;display:block" [enablePrint]=true (created)="onCreated()"></ejs-documenteditor>
</div>`,
encapsulation: ViewEncapsulation.None,
providers: [PrintService]
})
export class AppComponent {
@ViewChild('document_editor')
public documentEditor: DocumentEditorComponent;
onCreated(): void {
if(this.documentEditor.isDocumentLoaded) {
let sfdt: string =`{
"sections": [
{
"blocks": [
{
"inlines": [
{
"characterFormat": {
"bold": true,
"italic": true
},
"text": "Hello World"
}
]
}
],
"headersFooters": {
}
}
]
}`;
this.documentEditor.open(sfdt);
}
}
public onPrint() :void {
this.documentEditor.print();
}
}
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 { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);
Refer to the following example for creating a document and print it.
import { Component, ViewEncapsulation, ViewChild } from '@angular/core';
import {
DocumentEditorComponent, PrintService, SelectionService, EditorService, EditorHistoryService
} from '@syncfusion/ej2-angular-documenteditor';
@Component({
selector: 'app-container',
template: `<div style="width:100%;height:330px">
<button ejs-button (click)="onPrint()" >Print</button>
<ejs-documenteditor #document_editor style="width:100%;height:100%;display:block" [isReadOnly]=true [enableSelection]=true [enableEditor]=true [enablePrint]=true (created)="onCreated()"></ejs-documenteditor>
</div>`,
encapsulation: ViewEncapsulation.None,
providers: [PrintService, SelectionService, EditorService, EditorHistoryService]
})
export class AppComponent {
@ViewChild('document_editor')
public documentEditor: DocumentEditorComponent;
onCreated(): void {
if(this.documentEditor.isDocumentLoaded) {
let sfdt: string =`{
"sections": [
{
"blocks": [
{
"inlines": [
{
"characterFormat": {
"bold": true,
"italic": true
},
"text": "Hello World"
}
]
}
],
"headersFooters": {
}
}
]
}`;
this.documentEditor.open(sfdt);
}
}
public onPrint() :void {
this.documentEditor.print();
}
}
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 { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);
You can print the document in document editor by passing the window instance. This is useful to implement print in third party frameworks such as electron, where the window instance will not be available. Refer to the following example.
import { Component, ViewEncapsulation, ViewChild } from '@angular/core';
import {
DocumentEditorComponent, PrintService, SelectionService, EditorService, EditorHistoryService
} from '@syncfusion/ej2-angular-documenteditor';
@Component({
selector: 'app-container',
template: `<div style="width:100%;height:330px">
<button ejs-button (click)="onPrint()" >Print</button>
<ejs-documenteditor #document_editor style="width:100%;height:100%;display:block" [enablePrint]=true></ejs-documenteditor>
</div>`,
encapsulation: ViewEncapsulation.None,
providers: [PrintService]
})
export class AppComponent {
@ViewChild('document_editor')
public documentEditor: DocumentEditorComponent;
public onPrint() :void {
this.documentEditor.print(window);
}
}
Some of the print options cannot be configured using JavaScript. Refer to the following links to learn more about the browser page setup:
However, you can customize margins, paper, and layout options by modifying the section format properties using page setup dialog
this.documentEditor.showPageSetupDialog();
By customizing margins, papers, and layouts, the layout of the document will be changed in document editor. To modify these options during print operation, serialize the document as SFDT using the serialize
method in document editor instance and open the SFDT data in another instance of document editor in separate window.
The following example shows how to customize layout options only for printing.
import { Component, ViewEncapsulation, ViewChild } from '@angular/core';
import {
DocumentEditorComponent, PrintService, SelectionService, EditorService, EditorHistoryService, SfdtExportService
} from '@syncfusion/ej2-angular-documenteditor';
@Component({
selector: 'app-container',
template: `<div style="width:100%;height:330px">
<button ejs-button (click)="onPrint()" >Print</button>
<ejs-documenteditor #document_editor style="width:100%;height:100%;display:block" [isReadOnly]=false [enableSelection]=true [enableEditor]=true [enablePrint]=true [enableSfdtExport]=true></ejs-documenteditor>
<ejs-documenteditor #document_editor_preview style="width:100%;height:100%;display:block" [isReadOnly]=false [enableSelection]=true [enableEditor]=true [enablePrint]=true [enableSfdtExport]=true></ejs-documenteditor>
</div>`,
encapsulation: ViewEncapsulation.None,
providers: [PrintService, SelectionService, EditorService, EditorHistoryService, SfdtExportService]
})
export class AppComponent {
@ViewChild('document_editor')
public documentEditor: DocumentEditorComponent;
@ViewChild('document_editor_preview')
public documentEditorPreview: DocumentEditorComponent;
public onPrint() :void {
let sfdtData = this.documentEditor.serialize();
this.documentEditorPreview.open(sfdtData);
//Set A5 paper size
this.documentEditorPreview.selection.sectionFormat.pageWidth = 419.55;
this.documentEditorPreview.selection.sectionFormat.pageHeight = 595.30;
this.documentEditorPreview.print();
}
}
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 { }
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './app.module';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);