Search results

Set the default format in Document Editor in React DocumentEditor component

24 Jun 2022 / 3 minutes to read

You can set the default character format, paragraph format and section format in Document editor.

Set the default character format

You can use setDefaultCharacterFormat method to set the default character format. For example, Document editor default font size is 11 and you can change it as any valid value.

The following example code illustrates how to change the default font size in Document editor.

Copied to clipboard
import * as ReactDOM from 'react-dom';
import * as React from 'react';
import {
  DocumentEditorContainerComponent,
  Toolbar,
} from '@syncfusion/ej2-react-documenteditor';

DocumentEditorContainerComponent.Inject(Toolbar);
export class App extends React.Component<{}, {}> {
  container: DocumentEditorContainerComponent;
  onCreate(): void {
    this.container.documentEditor.setDefaultCharacterFormat({fontSize: 20});
  }
  render() {
    return (
      <DocumentEditorContainerComponent
        id="container"
        ref={(scope) => {
          this.container = scope;
        }}
        height={'590px'}
        serviceUrl="https://ej2services.syncfusion.com/production/web-services/api/documenteditor/"
        enableToolbar={true}
        created={this.onCreate.bind(this)}
      />
    );
  }
}
ReactDOM.render(<App />, document.getElementById('root'));

Similarly, you can change the required CharacterFormatProperties default value.

The following example code illustrates how to change other character format default value in Document editor.

Copied to clipboard
import * as ReactDOM from 'react-dom';
import * as React from 'react';
import {
  DocumentEditorContainerComponent,
  Toolbar,CharacterFormatProperties
} from '@syncfusion/ej2-react-documenteditor';

DocumentEditorContainerComponent.Inject(Toolbar);
export class App extends React.Component<{}, {}> {
  container: DocumentEditorContainerComponent;
  onCreate(): void {
    let defaultCharacterFormat:CharacterFormatProperties = {
      bold: false,
      italic: false,
      baselineAlignment: 'Normal',
      underline: 'None',
      fontColor: "#000000",
      fontFamily: 'Algerian',
      fontSize: 12
   };
  this.container.documentEditor.setDefaultCharacterFormat(defaultCharacterFormat);
  }
  render() {
    return (
      <DocumentEditorContainerComponent
        id="container"
        ref={(scope) => {
          this.container = scope;
        }}
        height={'590px'}
        serviceUrl="https://ej2services.syncfusion.com/production/web-services/api/documenteditor/"
        enableToolbar={true}
        created={this.onCreate.bind(this)}
      />
    );
  }
}
ReactDOM.render(<App />, document.getElementById('root'));

Set the default paragraph format

You can use setDefaultParagraphFormat API to set the default paragraph format. You can change the required ParagraphFormatProperties default value.

The following example code illustrates how to change the paragraph format(before spacing, line spacing etc.,) default value in Document editor.

Copied to clipboard
import * as ReactDOM from 'react-dom';
import * as React from 'react';
import {
  DocumentEditorContainerComponent,
  Toolbar,ParagraphFormatProperties
} from '@syncfusion/ej2-react-documenteditor';

DocumentEditorContainerComponent.Inject(Toolbar);
export class App extends React.Component<{}, {}> {
  container: DocumentEditorContainerComponent;
  onCreate(): void {
    let defaultParagraphFormat:ParagraphFormatProperties = {
      beforeSpacing: 8,
      lineSpacing: 1.5,
      leftIndent: 24,
      textAlignment: "Center"
   };
   this.container.documentEditor.setDefaultParagraphFormat(defaultParagraphFormat);
  }
  render() {
    return (
      <DocumentEditorContainerComponent
        id="container"
        ref={(scope) => {
          this.container = scope;
        }}
        height={'590px'}
        serviceUrl="https://ej2services.syncfusion.com/production/web-services/api/documenteditor/"
        enableToolbar={true}
        created={this.onCreate.bind(this)}
      />
    );
  }
}
ReactDOM.render(<App />, document.getElementById('root'));

Set the default section format

You can use setDefaultSectionFormat API to set the default section format. You can change the required SectionFormatProperties default value.

The following example code illustrates how to change the section format(header and footer distance, page width and height, etc.,) default value in Document editor.

Copied to clipboard
import * as ReactDOM from 'react-dom';
import * as React from 'react';
import {
  DocumentEditorContainerComponent,
  Toolbar,SectionFormatProperties
} from '@syncfusion/ej2-react-documenteditor';

DocumentEditorContainerComponent.Inject(Toolbar);
export class App extends React.Component<{}, {}> {
  container: DocumentEditorContainerComponent;
  onCreate(): void {
    let defaultSectionFormat:SectionFormatProperties = {
      pageWidth: 500,
      pageHeight: 800,
      headerDistance: 56,
      footerDistance: 48,
      leftMargin: 12,
      rightMargin: 12,
      topMargin:0,
      bottomMargin:0
   };
   this.container.documentEditor.setDefaultSectionFormat(defaultSectionFormat);
  }
  render() {
    return (
      <DocumentEditorContainerComponent
        id="container"
        ref={(scope) => {
          this.container = scope;
        }}
        height={'590px'}
        serviceUrl="https://ej2services.syncfusion.com/production/web-services/api/documenteditor/"
        enableToolbar={true}
        created={this.onCreate.bind(this)}
      />
    );
  }
}
ReactDOM.render(<App />, document.getElementById('root'));