Edit Word Documents in Node.js and Java

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Editor MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-editor -- dnx GroupDocs.Editor.Mcp --yes
Folders, licensing and more →

How to edit a Word document?

GroupDocs.Editor for Node.js and Java supports editing WordProcessing formats such as DOC, DOCX, DOT, DOCM, DOTX, ODT, and RTF. It allows two processing modes:

  • float (default)
  • paged (also known as paginal)

In float mode, the document appears as a single page when loaded into the WYSIWYG editor. In paged mode, the document is displayed with its original page breaks, similar to Microsoft Word.

Loading WordProcessing documents

Start by loading the document into the Editor class. Here’s an example of loading a password-protected DOCX file:

const groupdocs = require('groupdocs-editor');
const Editor = groupdocs.Editor;
const WordProcessingLoadOptions = groupdocs.options.WordProcessingLoadOptions;

// Load options for a password-protected document
const loadOptions = new WordProcessingLoadOptions();
loadOptions.password = "some_password_to_open_document";

// Load the document into the Editor
const inputPath = "C://input_path/document.docx";
const editor = new Editor(inputPath, loadOptions);

---

## How to edit a Word document?

GroupDocs.Editor for Node.js supports editing WordProcessing formats such as DOC, DOCX, DOT, DOCM, DOTX, ODT, and RTF. It allows two processing modes:

- **float** (default)
- **paged** (also known as **paginal**)

In float mode, the document appears as a single page when loaded into the WYSIWYG editor. In paged mode, the document is displayed with its original page breaks, similar to Microsoft Word.

### Loading WordProcessing documents

Start by loading the document into the `Editor` class. Here’s an example of loading a password-protected DOCX file:

```javascript
const groupdocs = require('groupdocs-editor');
const Editor = groupdocs.Editor;
const WordProcessingLoadOptions = groupdocs.options.WordProcessingLoadOptions;

// Load options for a password-protected document
const loadOptions = new WordProcessingLoadOptions();
loadOptions.password = "some_password_to_open_document";

// Load the document into the Editor
const inputPath = "C://input_path/document.docx";
const editor = new Editor(inputPath, loadOptions);

If the document is unprotected, the password will be ignored. If the document is protected but no password is provided, an error will be thrown during editing.

Editing WordProcessing documents

Once the document is loaded, you can edit it by creating WordProcessingEditOptions:

const WordProcessingEditOptions = groupdocs.options.WordProcessingEditOptions;

let editOptions = new WordProcessingEditOptions();
editOptions.fontExtraction = groupdocs.options.FontExtractionOptions.ExtractEmbeddedWithoutSystem;
editOptions.enableLanguageInformation = true;
editOptions.enablePagination = true;

Explanation:

  • fontExtraction: Extracts fonts used in the document.
  • enableLanguageInformation: Enables language information extraction for spell-checking.
  • enablePagination: Switches from the default float mode to paged mode.

Next, edit the document using the prepared options:

let editableDocument = editor.edit(editOptions);

To extract the HTML markup and resources separately:

let originalContent = editableDocument.getContent();
let allResources = editableDocument.getAllResources();

Modifying document content

After editing the document in the WYSIWYG-editor on the client side, pass the modified content back to the server:

let editedContent = originalContent.replace("document", "edited document");
let updatedDocument = groupdocs.EditableDocument.fromMarkup(editedContent, allResources);

Saving the Word document after editing

To save the edited document, you first need to create WordProcessingSaveOptions:

const WordProcessingSaveOptions = groupdocs.options.WordProcessingSaveOptions;

let saveOptions = new WordProcessingSaveOptions(groupdocs.formats.WordProcessingFormats.Docm);
saveOptions.password = "password";
saveOptions.enablePagination = true;
saveOptions.locale = "en-US";
saveOptions.optimizeMemoryUsage = true;
saveOptions.protection = new groupdocs.options.WordProcessingProtection(
    groupdocs.options.WordProcessingProtectionType.ReadOnly, 
    "write_password"
);

Explanation:

  1. WordProcessingFormats.Docm: Sets the format to DOCM.
  2. password: Encrypts the document with a password.
  3. enablePagination: Preserves pagination in the saved document.
  4. locale: Sets the locale.
  5. optimizeMemoryUsage: Optimizes memory for large documents.
  6. protection: Adds write protection to the document.

Finally, save the document:

let outputPath = "C://output_path/edited_document.docm";
editor.save(updatedDocument, outputPath, saveOptions);