Search results

Disable Header and Footer edit in Document in React DocumentEditor component

24 May 2022 / 3 minutes to read

You can use restrictEditing property to disable header and footer editing based on selection context type.

RestrictEditing allows you to restrict the document modification and makes the Document read only mode. So, by using this property, and if selection inside header or footer, you can set this property as true.

The following example code illustrates how to header and footer edit in DocumentEditorContainer instance.

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 Default extends React.Component {
  selectionChanges() {
        // Check whether selection is in header
  if (this.container.documentEditor.selection.contextType.indexOf('Header') > -1 ||
  // Check whether selection is in Footer
    this.container.documentEditor.selection.contextType.indexOf('Footer') > -1) {
    // Change the document to read only mode
    this.container.restrictEditing = true;
  } else {
    // Change the document to editable mode
    this.container.restrictEditing = false;
  }
  };
  render() {
    return (
      <DocumentEditorContainerComponent
        id="container" ref={(scope) => {
            this.container = scope;
          }}
        height={'590px'}
        serviceUrl="https://ej2services.syncfusion.com/production/web-services/api/documenteditor/"
        enableToolbar={true}
        selectionChange={this.selectionChanges.bind(this)}
      />
    );
  }
}
ReactDOM.render(<Default />, document.getElementById('sample'));

Otherwise, you can disable clicking inside Header or Footer by using closeHeaderFooter API in selection module.

The following example code illustrates how to close header and footer when selection is inside header or footer in DocumentEditorContainer instance.

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 Default extends React.Component {
  selectionChanges() {
        // Check whether selection is in header
  if (this.container.documentEditor.selection.contextType.indexOf('Header') > -1 ||
  // Check whether selection is in Footer
    this.container.documentEditor.selection.contextType.indexOf('Footer') > -1) {
    // Close header Footer
    this.container.documentEditor.selection.closeHeaderFooter();
  }
  };
  render() {
    return (
      <DocumentEditorContainerComponent
        id="container" ref={(scope) => {
            this.container = scope;
          }}
        height={'590px'}
        serviceUrl="https://ej2services.syncfusion.com/production/web-services/api/documenteditor/"
        enableToolbar={true}
        selectionChange={this.selectionChanges.bind(this)}
      />
    );
  }
}
ReactDOM.render(<Default />, document.getElementById('sample'));

Like restrictEditing, you can use isReadOnly property in Document editor to disable header and footer edit.

The following example code illustrates how to header and footer edit in DocumentEditor instance.

Copied to clipboard
import * as ReactDOM from 'react-dom';
import * as React from 'react';
import {
  DocumentEditorComponent,
  Print,
  SfdtExport,
  WordExport,
  TextExport,
  Selection,
  Search,
  Editor,
  ImageResizer,
  EditorHistory,
  ContextMenu,
  OptionsPane,
  HyperlinkDialog,
  TableDialog,
  BookmarkDialog,
  TableOfContentsDialog,
  PageSetupDialog,
  StyleDialog,
  ListDialog,
  ParagraphDialog,
  BulletsAndNumberingDialog,
  FontDialog,
  TablePropertiesDialog,
  BordersAndShadingDialog,
  TableOptionsDialog,
  CellOptionsDialog,
  StylesDialog,
} from '@syncfusion/ej2-react-documenteditor';
DocumentEditorComponent.Inject(
  Print,
  SfdtExport,
  WordExport,
  TextExport,
  Selection,
  Search,
  Editor,
  ImageResizer,
  EditorHistory,
  ContextMenu,
  OptionsPane,
  HyperlinkDialog,
  TableDialog,
  BookmarkDialog,
  TableOfContentsDialog,
  PageSetupDialog,
  StyleDialog,
  ListDialog,
  ParagraphDialog,
  BulletsAndNumberingDialog,
  FontDialog,
  TablePropertiesDialog,
  BordersAndShadingDialog,
  TableOptionsDialog,
  CellOptionsDialog,
  StylesDialog
);

export class Default extends React.Component {
  selectionChanges() {
    // Check whether selection is in header
    if (
      this.documentEditor.selection.contextType.indexOf('Header') > -1 ||
      // Check whether selection is in Footer
      this.documentEditor.selection.contextType.indexOf('Footer') > -1
    ) {
      // Change the document to read only mode
      this.documentEditor.isReadOnly = true;
    } else {
      // Change the document to editable mode
      this.documentEditor.isReadOnly = false;
    }
  }
  render() {
    return (
      <DocumentEditorComponent
        id="documentEditor"
        ref={(scope) => {
          this.documentEditor = scope;
        }}
        height={'330px'}
        serviceUrl="https://ej2services.syncfusion.com/production/web-services/api/documenteditor/"
        isReadOnly={false}
        enablePrint={true}
        enableSelection={true}
        enableEditor={true}
        enableEditorHistory={true}
        enableContextMenu={true}
        enableSearch={true}
        enableOptionsPane={true}
        enableBookmarkDialog={true}
        enableBordersAndShadingDialog={true}
        enableFontDialog={true}
        enableTableDialog={true}
        enableParagraphDialog={true}
        enableHyperlinkDialog={true}
        enableImageResizer={true}
        enableListDialog={true}
        enablePageSetupDialog={true}
        enableSfdtExport={true}
        enableStyleDialog={true}
        enableTableOfContentsDialog={true}
        enableTableOptionsDialog={true}
        enableTablePropertiesDialog={true}
        enableTextExport={true}
        enableWordExport={true}
        selectionChange={this.selectionChanges.bind(this)}
      />
    );
  }
}
ReactDOM.render(<Default />, document.getElementById('sample'));