> ## Documentation Index
> Fetch the complete documentation index at: https://superdoc-caio-pizzol-docs-ai-core-preset.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Document extension

export const SourceCodeLink = ({extension, path}) => {
  const githubPath = path || `packages/super-editor/src/editors/v1/extensions/${extension.toLowerCase()}`;
  const githubUrl = `https://github.com/superdoc-dev/superdoc/tree/main/${githubPath}`;
  return <div>
      <p>
        <a href={githubUrl} target="_blank" rel="noopener noreferrer">
          View on GitHub →
        </a>
      </p>
    </div>;
};

The root container for all document content.

Every editor starts with a Document node that contains all blocks like paragraphs, headings, and lists.

## Structure

```xml theme={null}
<doc>
  <p>First paragraph</p>
  <p>Second paragraph</p>
  <!-- All block content -->
</doc>
```

## Why it's essential

* **Required root** - Every editor needs exactly one document node
* **Content validation** - Ensures at least one block element exists
* **Document operations** - Enables document-level commands and queries

## Commands

### `getDocumentStats`

Get document statistics

<Note>
  Returns word count, character count, and paragraph count
</Note>

**Example:**

<CodeGroup>
  ```javascript Usage theme={null}
  // Get word and character count
  const stats = editor.commands.getDocumentStats()
  console.log(`${stats.words} words, ${stats.characters} characters`)
  ```

  ```javascript Full Example theme={null}
  import { SuperDoc } from 'superdoc';
  import 'superdoc/style.css';

  const superdoc = new SuperDoc({
    selector: '#editor',
    document: yourFile,
    onReady: (superdoc) => {
      const editor = superdoc.activeEditor;
      // Get word and character count
      const stats = editor.commands.getDocumentStats()
      console.log(`${stats.words} words, ${stats.characters} characters`)
    },
  });
  ```
</CodeGroup>

### `clearDocument`

Clear entire document

<Note>
  Replaces all content with an empty paragraph
</Note>

**Example:**

<CodeGroup>
  ```javascript Usage theme={null}
  editor.commands.clearDocument()
  ```

  ```javascript Full Example theme={null}
  import { SuperDoc } from 'superdoc';
  import 'superdoc/style.css';

  const superdoc = new SuperDoc({
    selector: '#editor',
    document: yourFile,
    onReady: (superdoc) => {
      const editor = superdoc.activeEditor;
      editor.commands.clearDocument()
    },
  });
  ```
</CodeGroup>

### `setSectionPageMarginsAtSelection`

Set page margins for the section at the current cursor position.

**Example:**

<CodeGroup>
  ```javascript Usage theme={null}
  editor.commands.setSectionPageMarginsAtSelection({
    topInches: 1,
    rightInches: 1,
    bottomInches: 1,
    leftInches: 1
  })
  ```

  ```javascript Full Example theme={null}
  import { SuperDoc } from 'superdoc';
  import 'superdoc/style.css';

  const superdoc = new SuperDoc({
    selector: '#editor',
    document: yourFile,
    onReady: (superdoc) => {
      const editor = superdoc.activeEditor;
      editor.commands.setSectionPageMarginsAtSelection({
        topInches: 1,
        rightInches: 1,
        bottomInches: 1,
        leftInches: 1
      })
    },
  });
  ```
</CodeGroup>

**Parameters:**

<ParamField path="options" type="Object">
  Object with optional `topInches`, `rightInches`, `bottomInches`, `leftInches` (numbers in inches)
</ParamField>

## Source code

<SourceCodeLink path="packages/super-editor/src/editors/v1/extensions/document/document.js" />
