Document
The
Documentobject is the starting point of your.docxjourney, this is the literal Word Document. You add all your content such asParagraphsto thisDocument, and at the end export it however you like.
To create a new document, it is very easy:
const doc = new docx.Document();Document properties
You can add properties to the Word document by specifying options, for example:
const doc = new docx.Document({
creator: "Dolan Miu",
description: "My extremely interesting document",
title: "My Document",
});Full list of options:
| Property | Type | Notes |
|---|---|---|
| sections | ISectionOptions[] | Optional |
| title | string | Optional |
| subject | string | Optional |
| creator | string | Optional |
| keywords | string | Optional |
| description | string | Optional |
| lastModifiedBy | string | Optional |
| revision | number | Optional |
| externalStyles | string | Optional |
| styles | IStylesOptions | Optional |
| numbering | INumberingOptions | Optional |
| comments | ICommentsOptions | Optional |
| footnotes | Record<string, { children: Paragraph[] }> | Optional |
| background | IDocumentBackgroundOptions | Optional. Page background color — see below |
| features | { trackRevisions?: boolean; updateFields?: boolean; } | Optional. See Features |
| compatabilityModeVersion | number | Optional |
| compatibility | ICompatibilityOptions | Optional |
| customProperties | ICustomPropertyOptions[] | Optional. Arbitrary name/value document properties — see Custom Properties |
| evenAndOddHeaderAndFooters | boolean | Optional |
| defaultTabStop | number | Optional |
| fonts | FontOptions[] | Optional. Embed custom font files in the document — see Fonts |
| hyphenation | IHyphenationOptions | Optional. See Hyphenation |
Change background color of Document
Set the hex value in the document like so:
const doc = new docx.Document({
background: {
color: "C45911",
},
});Instead of a fixed hex color, you can reference a theme color, optionally darkened or lightened:
| Property | Type | Notes |
|---|---|---|
| color | string | Optional. Hex color value, e.g. "C45911" |
| themeColor | string | Optional. Theme color name, e.g. "accent1", "background2" |
| themeShade | string | Optional. Shade applied to the theme color, as a hex byte, e.g. "BF" |
| themeTint | string | Optional. Tint applied to the theme color, as a hex byte, e.g. "99" |
You can mix and match whatever properties you want, or provide no properties.
Features
The features option toggles document-wide behaviors:
trackRevisions— opens the document with the 'Track Changes' feature enabled, so any edits made in Word are recorded as revisions.updateFields— instructs Word to update all fields when the document is opened. This is required for a Table of Contents to be populated, since fields are calculated by the word processor rather than at export time. Word will show a prompt asking the user to allow the update.
const doc = new docx.Document({
features: {
updateFields: true,
},
sections: [],
});Hyphenation
The hyphenation option controls automatic hyphenation across the document:
| Property | Type | Notes |
|---|---|---|
| autoHyphenation | boolean | Optional. Automatically hyphenate words at the end of lines |
| hyphenationZone | number | Optional. Hyphenation zone, in twips (emitted as w:hyphenationZone) |
| consecutiveHyphenLimit | number | Optional. Maximum number of consecutive lines that can end with a hyphenated word |
| doNotHyphenateCaps | boolean | Optional. Skip hyphenation for words in all capital letters |
const doc = new docx.Document({
hyphenation: {
autoHyphenation: true,
consecutiveHyphenLimit: 2,
doNotHyphenateCaps: true,
},
sections: [],
});Units for positioning
Various parts of the API require positioning arguments. The units are "20ths of a point" from the OOXML specification. See Lars Corneliussen's blog post for more information and how to convert units.
Compatibility
Compatibility Settings are optional settings used to preserve visual fidelity of documents created in earlier word processing applications. Some of these settings provide ability for specific behaviors, described in detail below; and others simply instruct applications to mimic the behavior of an existing word processing application.
const doc = new docx.Document({
compatibility: {
version: 15,
doNotExpandShiftReturn: true,
},
});Compatibility Options
| Property | Type | Notes | Possible Values |
|---|---|---|---|
| version | number | Optional | 15, 16, 17 |
| useSingleBorderforContiguousCells | boolean | Optional | true, false, undefined |
| wordPerfectJustification | boolean | Optional | true, false, undefined |
| noTabStopForHangingIndent | boolean | Optional | true, false, undefined |
| noLeading | boolean | Optional | true, false, undefined |
| spaceForUnderline | boolean | Optional | true, false, undefined |
| noColumnBalance | boolean | Optional | true, false, undefined |
| balanceSingleByteDoubleByteWidth | boolean | Optional | true, false, undefined |
| noExtraLineSpacing | boolean | Optional | true, false, undefined |
| doNotLeaveBackslashAlone | boolean | Optional | true, false, undefined |
| underlineTrailingSpaces | boolean | Optional | true, false, undefined |
| doNotExpandShiftReturn | boolean | Optional | true, false, undefined |
| spacingInWholePoints | boolean | Optional | true, false, undefined |
| lineWrapLikeWord6 | boolean | Optional | true, false, undefined |
| printBodyTextBeforeHeader | boolean | Optional | true, false, undefined |
| printColorsBlack | boolean | Optional | true, false, undefined |
| spaceWidth | boolean | Optional | true, false, undefined |
| showBreaksInFrames | boolean | Optional | true, false, undefined |
| subFontBySize | boolean | Optional | true, false, undefined |
| suppressBottomSpacing | boolean | Optional | true, false, undefined |
| suppressTopSpacing | boolean | Optional | true, false, undefined |
| suppressSpacingAtTopOfPage | boolean | Optional | true, false, undefined |
| suppressTopSpacingWP | boolean | Optional | true, false, undefined |
| suppressSpBfAfterPgBrk | boolean | Optional | true, false, undefined |
| swapBordersFacingPages | boolean | Optional | true, false, undefined |
| convertMailMergeEsc | boolean | Optional | true, false, undefined |
| truncateFontHeightsLikeWP6 | boolean | Optional | true, false, undefined |
| macWordSmallCaps | boolean | Optional | true, false, undefined |
| usePrinterMetrics | boolean | Optional | true, false, undefined |
| doNotSuppressParagraphBorders | boolean | Optional | true, false, undefined |
| wrapTrailSpaces | boolean | Optional | true, false, undefined |
| footnoteLayoutLikeWW8 | boolean | Optional | true, false, undefined |
| shapeLayoutLikeWW8 | boolean | Optional | true, false, undefined |
| alignTablesRowByRow | boolean | Optional | true, false, undefined |
| forgetLastTabAlignment | boolean | Optional | true, false, undefined |
| adjustLineHeightInTable | boolean | Optional | true, false, undefined |
| autoSpaceLikeWord95 | boolean | Optional | true, false, undefined |
| noSpaceRaiseLower | boolean | Optional | true, false, undefined |
| doNotUseHTMLParagraphAutoSpacing | boolean | Optional | true, false, undefined |
| layoutRawTableWidth | boolean | Optional | true, false, undefined |
| layoutTableRowsApart | boolean | Optional | true, false, undefined |
| useWord97LineBreakRules | boolean | Optional | true, false, undefined |
| doNotBreakWrappedTables | boolean | Optional | true, false, undefined |
| doNotSnapToGridInCell | boolean | Optional | true, false, undefined |
| selectFieldWithFirstOrLastCharacter | boolean | Optional | true, false, undefined |
| applyBreakingRules | boolean | Optional | true, false, undefined |
| doNotWrapTextWithPunctuation | boolean | Optional | true, false, undefined |
| doNotUseEastAsianBreakRules | boolean | Optional | true, false, undefined |
| useWord2002TableStyleRules | boolean | Optional | true, false, undefined |
| growAutofit | boolean | Optional | true, false, undefined |
| useFELayout | boolean | Optional | true, false, undefined |
| useNormalStyleForList | boolean | Optional | true, false, undefined |
| doNotUseIndentAsNumberingTabStop | boolean | Optional | true, false, undefined |
| useAlternateEastAsianLineBreakRules | boolean | Optional | true, false, undefined |
| allowSpaceOfSameStyleInTable | boolean | Optional | true, false, undefined |
| doNotSuppressIndentation | boolean | Optional | true, false, undefined |
| doNotAutofitConstrainedTables | boolean | Optional | true, false, undefined |
| autofitToFirstFixedWidthCell | boolean | Optional | true, false, undefined |
| underlineTabInNumberingList | boolean | Optional | true, false, undefined |
| displayHangulFixedWidth | boolean | Optional | true, false, undefined |
| splitPgBreakAndParaMark | boolean | Optional | true, false, undefined |
| doNotVerticallyAlignCellWithSp | boolean | Optional | true, false, undefined |
| doNotBreakConstrainedForcedTable | boolean | Optional | true, false, undefined |
| ignoreVerticalAlignmentInTextboxes | boolean | Optional | true, false, undefined |
| useAnsiKerningPairs | boolean | Optional | true, false, undefined |
| cachedColumnBalance | boolean | Optional | true, false, undefined |