Print in Angular Diagram component

10 Dec 202423 minutes to read

The print method helps to print the diagram as image.

 public options: IPrintOptions;
 this.options = {};
 diagram.print(this.options);

NOTE

To Print diagram you need to inject PrintAndExport in the diagram.

The diagram can be customized while printing using the following properties of printOptions.

The available print options are listed in the table below.

Name Type Description
region enum Sets the region of the diagram to be printed.
margin object Sets the margin of the page to be printed.
stretch enum Resizes the diagram content to fill its allocated space and printed.
multiplePage boolean Prints the diagram into multiple pages.
pageWidth number Sets the page width of the diagram while printing the diagram into multiple pages.
pageHeight number Sets the page height of the diagram while printing the diagram into multiple pages.
pageOrientation enum Sets the orientation of the page.

Region

Printing particular region of diagram is possible by using the region property of the printOptions.

The following code example illustrates how to print the diagram based on region.

import { DiagramModule, DiagramRegions, IPrintOptions, PageSettingsModel, PrintAndExport,
  DiagramComponent, Diagram, NodeModel  } from '@syncfusion/ej2-angular-diagrams';
import { Component, ViewEncapsulation, ViewChild } from '@angular/core';

Diagram.Inject(PrintAndExport);

@Component({
  imports: [DiagramModule],
  providers: [],
  standalone: true,
  selector: 'app-container',
  template: `
    <label for="region">Region: </label>
    <select id="region"  #regionSelect>
      <option value="PageSettings">PageSettings</option>
      <option value="Content">Content</option>
    </select>
    <button (click)="onPrintClick(regionSelect.value)">Print</button>
    <ejs-diagram #diagram id="diagram" width="100%" height="580px" [nodes] ='nodes' [pageSettings] ='pageSettings'>
    </ejs-diagram>`,
  encapsulation: ViewEncapsulation.None,
})

export class AppComponent {
  @ViewChild('diagram')
  public diagram!: DiagramComponent;
  public printOptions: IPrintOptions = {};
  public pageSettings: PageSettingsModel = { width: 200, height: 200 };

  public nodes: NodeModel[] = [
    {
      id: 'node1',
      width: 100,
      height: 100,
      offsetX: 100,
      offsetY: 100,
      annotations: [{ content: 'Node 1' }],
    },
    {
      id: 'node2',
      width: 100,
      height: 100,
      offsetX: 300,
      offsetY: 130,
      annotations: [{ content: 'Node 2' }],
    },
  ];

  // Function to handle the print button click
  onPrintClick(region: string) {
    this.printOptions = {
      region: region as DiagramRegions,
    };
    this.diagram.print(this.printOptions);
  }
  
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

Multiple page

Printing a diagram across multiple pages is possible by setting the multiplePage property of printOptions to true.

The following code example demonstrates how to set multiplePage to true:

import { Component, ViewEncapsulation, ViewChild } from '@angular/core';
import { DiagramComponent, DiagramModule, NodeModel, Diagram, PageSettingsModel,
  SnapConstraints, IPrintOptions, PrintAndExport } from '@syncfusion/ej2-angular-diagrams';

Diagram.Inject(PrintAndExport);

@Component({
  selector: 'app-container',
  template: `
  <div> <button (click)="onPrintClick()">Print</button>
  <div id="element"></div></div>`,
  imports: [
    DiagramModule
  ],
  providers: [],
  standalone: true,
  encapsulation: ViewEncapsulation.None,

})

export class AppComponent {
  @ViewChild('diagram')
  public diagram!: DiagramComponent;
  public diagramInstance!: Diagram;
  constructor() { }
  public nodes: NodeModel[] = [
    {
      id: 'node1',
      width: 100,
      height: 50,
      offsetX: 100,
      offsetY: 100,
      style: { strokeColor: '#868686', fill: '#d5f5d5' },
      annotations: [{ content: 'Node 1' }],
    },
    {
      id: 'node2',
      width: 100,
      height: 75,
      offsetX: 100,
      offsetY: 225,
      shape: { type: 'Basic', shape: 'Diamond' },
      style: { strokeColor: '#8f908f', fill: '#e2f3fa' },
      annotations: [{ content: 'Node 2' }],
    },
    {
      id: 'node3',
      width: 135,
      height: 50,
      offsetX: 400,
      offsetY: 425,
      style: { strokeColor: '#a8a8a8', fill: '#faebee' },
      annotations: [{ content: 'Node 3' }],
    },
    {
      id: 'node4',
      width: 125,
      height: 50,
      offsetX: 400,
      offsetY: 525,
      shape: { type: 'Basic', shape: 'Ellipse' },
      style: { strokeColor: '#a8a8a8', fill: '#fef0db' },
      annotations: [{ content: 'Node 4' }],
    },
  ];

  ngOnInit(): void {
    // Programmatically create the Diagram component
    this.diagramInstance = new Diagram({
      width: '100%',
      height: '580px',
      nodes: this.nodes,
      snapSettings: { constraints: SnapConstraints.None },
      pageSettings: {
        width: 300,
        height: 500,
        multiplePage: true,
        showPageBreaks: true,
      } as PageSettingsModel,
    });

    this.diagramInstance.appendTo('#element');
  }

  onPrintClick() {
    const printOptions: IPrintOptions = {};
    printOptions.mode = 'Data';
    printOptions.region = 'PageSettings';
    printOptions.multiplePage = true;
    printOptions.margin = { left: 0, top: 0, bottom: 0, right: 0 };
    this.diagramInstance.print(printOptions);
  };
}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

Margin

The margin for the print region can be set using the margin property of the printOptions

import { Component, ViewChild, ViewEncapsulation } from '@angular/core';
import { DiagramComponent, DiagramModule, NodeModel, Diagram, SnapConstraints,
  IPrintOptions, PrintAndExport } from '@syncfusion/ej2-angular-diagrams';

Diagram.Inject(PrintAndExport);

@Component({
  selector: 'app-container',
  template: `
  <div> <button (click)="onPrintClick()">Print</button>
  <div id="element"></div></div>`,
  imports: [
    DiagramModule
  ],
  providers: [],
  standalone: true,
  encapsulation: ViewEncapsulation.None

})

export class AppComponent {
  @ViewChild('diagram')
  public diagram!: DiagramComponent;
  public diagramInstance!: Diagram;
  constructor() { }
  public nodes: NodeModel[] = [
    {
      id: 'node1',
      width: 100,
      height: 50,
      offsetX: 100,
      offsetY: 100,
      style: { strokeColor: '#868686', fill: '#d5f5d5' },
      annotations: [{ content: 'Node 1' }],
    },
    {
      id: 'node2',
      width: 100,
      height: 75,
      offsetX: 100,
      offsetY: 225,
      shape: { type: 'Basic', shape: 'Diamond' },
      style: { strokeColor: '#8f908f', fill: '#e2f3fa' },
      annotations: [{ content: 'Node 2' }],
    },
    {
      id: 'node3',
      width: 135,
      height: 50,
      offsetX: 400,
      offsetY: 425,
      style: { strokeColor: '#a8a8a8', fill: '#faebee' },
      annotations: [{ content: 'Node 3' }],
    },
    {
      id: 'node4',
      width: 125,
      height: 50,
      offsetX: 400,
      offsetY: 525,
      shape: { type: 'Basic', shape: 'Ellipse' },
      style: { strokeColor: '#a8a8a8', fill: '#fef0db' },
      annotations: [{ content: 'Node 4' }],
    },
  ];

  ngOnInit(): void {
    // Programmatically create the Diagram component
    this.diagramInstance = new Diagram({
      width: '100%',
      height: '580px',
      nodes: this.nodes,
      snapSettings: { constraints: SnapConstraints.None },
    });

    this.diagramInstance.appendTo('#element');
  }

  onPrintClick() {
    const printOptions: IPrintOptions = {};
    printOptions.margin = { left: 400, top: 100 };
    this.diagramInstance.print(printOptions);
  };

}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

Page width and Page height

The pageWidth and pageHeight property of printOptions is used to set the size of the printing image. The following example demonstrates how to set the pageWidth and pageHeight.

import { Component, ViewChild, ViewEncapsulation } from '@angular/core';
import { DiagramComponent, DiagramModule, NodeModel, Diagram, SnapConstraints,
  IPrintOptions, PrintAndExport } from '@syncfusion/ej2-angular-diagrams';

Diagram.Inject(PrintAndExport);

@Component({
  selector: 'app-container',
  template: `
  <div> <button (click)="onPrintClick()">Print</button>
  <div id="element"></div></div>`,
  imports: [
    DiagramModule
  ],
  providers: [],
  standalone: true,
  encapsulation: ViewEncapsulation.None

})

export class AppComponent {
  @ViewChild('diagram')
  public diagram!: DiagramComponent;
  public diagramInstance!: Diagram;
  constructor() { }
  public nodes: NodeModel[] = [
    {
      id: 'node1',
      width: 100,
      height: 50,
      offsetX: 100,
      offsetY: 100,
      style: { strokeColor: '#868686', fill: '#d5f5d5' },
      annotations: [{ content: 'Node 1' }],
    },
  
    {
      id: 'node2',
      width: 100,
      height: 75,
      offsetX: 100,
      offsetY: 225,
      shape: { type: 'Basic', shape: 'Diamond' },
      style: { strokeColor: '#8f908f', fill: '#e2f3fa' },
      annotations: [{ content: 'Node 2' }],
    },
    {
      id: 'node3',
      width: 135,
      height: 50,
      offsetX: 400,
      offsetY: 425,
      style: { strokeColor: '#a8a8a8', fill: '#faebee' },
      annotations: [{ content: 'Node 3' }],
    },
    {
      id: 'node4',
      width: 125,
      height: 50,
      offsetX: 400,
      offsetY: 525,
      shape: { type: 'Basic', shape: 'Ellipse' },
      style: { strokeColor: '#a8a8a8', fill: '#fef0db' },
      annotations: [{ content: 'Node 4' }],
    },
  ];

  ngOnInit(): void {
    // Programmatically create the Diagram component
    this.diagramInstance = new Diagram({
      width: '100%',
      height: '580px',
      nodes: this.nodes,
      snapSettings: { constraints: SnapConstraints.None },
    });

    this.diagramInstance.appendTo('#element');
  }

  onPrintClick() {
    const printOptions: IPrintOptions = {};
    printOptions.pageHeight = 700;
    printOptions.pageWidth = 1000;
    this.diagramInstance.print(printOptions);
  };

}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

Page Orientation

pageOrientation of printOptions is used to set the orientation of the page to be printed.

  • Landscape - Display with page Width is more than the page Height.
  • Portrait - Display with page Height is more than the page width.

The following example shows how to set pageOrientation for the printOptions.

import { Component, ViewChild, ViewEncapsulation } from '@angular/core';
import { DiagramComponent, DiagramModule, NodeModel, Diagram, SnapConstraints,
  IPrintOptions, PrintAndExport } from '@syncfusion/ej2-angular-diagrams';

Diagram.Inject(PrintAndExport);

@Component({
  selector: 'app-container',
  template: `
  <div> <button (click)="onPrintClick()">Print</button>
  <div id="element"></div></div>`,
  imports: [
    DiagramModule
  ],
  providers: [],
  standalone: true,
  encapsulation: ViewEncapsulation.None

})

export class AppComponent {
  @ViewChild('diagram')
  public diagram!: DiagramComponent;
  public diagramInstance!: Diagram;
  constructor() { }
  public nodes: NodeModel[] = [
    {
      id: 'node1',
      width: 100,
      height: 50,
      offsetX: 100,
      offsetY: 100,
      style: { strokeColor: '#868686', fill: '#d5f5d5' },
      annotations: [{ content: 'Node 1' }],
    },
    {
      id: 'node2',
      width: 100,
      height: 75,
      offsetX: 100,
      offsetY: 225,
      shape: { type: 'Basic', shape: 'Diamond' },
      style: { strokeColor: '#8f908f', fill: '#e2f3fa' },
      annotations: [{ content: 'Node 2' }],
    },
    {
      id: 'node3',
      width: 135,
      height: 50,
      offsetX: 400,
      offsetY: 425,
      style: { strokeColor: '#a8a8a8', fill: '#faebee' },
      annotations: [{ content: 'Node 3' }],
    },
    {
      id: 'node4',
      width: 125,
      height: 50,
      offsetX: 400,
      offsetY: 725,
      shape: { type: 'Basic', shape: 'Ellipse' },
      style: { strokeColor: '#a8a8a8', fill: '#fef0db' },
      annotations: [{ content: 'Node 4' }],
    }
  ];

  ngOnInit(): void {
    // Programmatically create the Diagram component
    this.diagramInstance = new Diagram({
      width: '100%',
      height: '580px',
      nodes: this.nodes,
      snapSettings: { constraints: SnapConstraints.None },
    });

    this.diagramInstance.appendTo('#element');
  }

  onPrintClick() {
    const printOptions: IPrintOptions = {};
    printOptions.pageOrientation = 'Portrait';
    this.diagramInstance.print(printOptions);
  };

}
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

Limitations

Currently, printing diagram with native and HTML nodes is not supported. To overcome this limitation, we make use of the Syncfusion Essential® PDF library. This library incorporates the Syncfusion Essential® HTML converter, which employs the advanced Blink rendering engine. This converter seamlessly transforms HTML content into images. Refer to export Html-and-Native node kb for more information.

See Also