> ## Documentation Index
> Fetch the complete documentation index at: https://docs.reducto.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Office.js in Node

Import from `@reductoai/docx/node`. It gives a `WordDocument` that reads and writes files and renders with the fonts on the machine.

## Open, edit, save

```ts theme={null}
import { WordDocument } from "@reductoai/docx/node";

const doc = await WordDocument.open("contract.docx");

for (const hit of doc.body.search("ACME Corp", { matchCase: true }).items) {
  hit.insertText("Globex Ltd", "Replace");
}

const heading = doc.body.insertParagraph("Schedule A", "End");
heading.styleBuiltIn = "Heading1";
doc.body.insertParagraph("The services are listed below.", "End");

await doc.save("contract-edited.docx");
```

`save` always needs a path. OfficeJS never writes over the input file by itself.

## Create a new document

```ts theme={null}
const doc = await WordDocument.create();
doc.setDefaultFont("Arial");

// A new document has one empty paragraph. Use it first.
const title = doc.body.paragraphs.items[0];
title.insertText("Quarterly report", "Replace");
title.styleBuiltIn = "Title";

const table = doc.body.insertTable(3, 2, "End", [
  ["Region", "Revenue"],
  ["EMEA", "4.2M"],
  ["APAC", "3.1M"],
]);
table.getBorder("All").type = "Single";
table.getCell(0, 0).body.paragraphs.items[0].font.bold = true;

await doc.save("report.docx");
```

## Bytes in, bytes out

When the document comes from a request or a bucket, use bytes:

```ts theme={null}
const doc = await WordDocument.fromBytes(new Uint8Array(await file.arrayBuffer()));
// ... edit ...
const bytes = await doc.toBytes();
```

## Render

```ts theme={null}
await doc.toPdf("contract.pdf");            // writes the file, also returns the bytes
const pngs = await doc.toPng({ scale: 2 });  // [{ page, png, width, height }]
```

Both render the **current** edit state. You do not need to save first. See [PDF](https://docs.reducto.ai/office/docx/conversion/pdf.md) and [PNG](https://docs.reducto.ai/office/docx/conversion/png.md).

## Read what is on a page

`readPage` gives a short text sketch of one laid-out page. It is useful for checks and for LLM agents.

```ts theme={null}
console.log(await doc.readPage(1));
// page 1/1  sparse
//    11%  p:0        Master Services Agreement
//    15%  p:1        The Supplier shall deliver the Services within 30 days.
```

## Common tasks

```ts theme={null}
const p = doc.body.paragraphs.items[0];

// Formatting
p.font.set({ name: "Arial", size: 11, bold: false, color: "#000000" });
p.set({ alignment: "Left", spaceBefore: 0, spaceAfter: 6 });
p.setLineSpacing({ rule: "Exactly", points: 13 });
p.getRange("Whole").font.bold = true;

// Page setup, headers and footers
const section = doc.sections.items[0];
section.pageSetup.set({ pageWidth: 612, pageHeight: 792, topMargin: 54, bottomMargin: 54 });
section.getHeader("Primary").insertParagraph("Confidential", "Replace");

// Lists
const list = p.startNewList("decimal");
list.insertParagraph("Second item", "End");

// Links, footnotes, pictures
p.insertHyperlink({ text: "Website", url: "https://example.com" });
p.getRange("End").insertFootnote("Source note");
p.insertInlinePictureFromBase64(base64Png, "End", { width: 120 });

// Fields (TOC, page numbers) need layout
const report = await doc.updateFields();
```

> [!TIP]
>
> `insertText("\n")` makes new paragraphs. For a line break inside a paragraph, use `insertBreak("Line")`.
