diff --git a/demo/62-paragraph-spacing.ts b/demo/62-paragraph-spacing.ts new file mode 100644 index 0000000000..0322a1729e --- /dev/null +++ b/demo/62-paragraph-spacing.ts @@ -0,0 +1,34 @@ +// Simple example to add text to a document +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, LineRuleType, Packer, Paragraph, TextRun } from "../build"; + +const doc = new Document(); + +doc.addSection({ + properties: {}, + children: [ + new Paragraph({ + spacing: { + after: 5000, + before: 5000, + }, + children: [new TextRun("Hello World")], + }), + new Paragraph({ + spacing: { + line: 2000, + lineRule: LineRuleType.AUTO, + }, + children: [ + new TextRun( + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed turpis ex, aliquet et faucibus quis, euismod in odio. Fusce gravida tempor nunc sed lacinia. Nulla sed dolor fringilla, fermentum libero ut, egestas ex. Donec pellentesque metus non orci lacinia bibendum. Cras porta ex et mollis hendrerit. Suspendisse id lectus suscipit, elementum lacus eu, convallis felis. Fusce sed bibendum dolor, id posuere ligula. Aliquam eu elit ut urna eleifend vestibulum. Praesent condimentum at turpis sed scelerisque. Suspendisse porttitor metus nec vestibulum egestas. Sed in eros sapien. Morbi efficitur placerat est a consequat. Nunc bibendum porttitor mi nec tempus. Morbi dictum augue porttitor nisi sodales sodales.", + ), + ], + }), + ], +}); + +Packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/docs/usage/paragraph.md b/docs/usage/paragraph.md index f824d9d44b..2e3ebc5e3d 100644 --- a/docs/usage/paragraph.md +++ b/docs/usage/paragraph.md @@ -176,12 +176,12 @@ Adding spacing between paragraphs ### ISpacingProperties -| Property | Type | Notes | -| -------- | -------- | -------- | -| after | `number` | Optional | -| before | `number` | Optional | -| line | `number` | Optional | -| lineRule | `string` | Optional | +| Property | Type | Notes | +| -------- | -------------- | -------- | +| after | `number` | Optional | +| before | `number` | Optional | +| line | `number` | Optional | +| lineRule | `LineRuleType` | Optional | **Example:** diff --git a/src/file/footnotes/footnotes.ts b/src/file/footnotes/footnotes.ts index 05af80b6e7..d0ac12a99f 100644 --- a/src/file/footnotes/footnotes.ts +++ b/src/file/footnotes/footnotes.ts @@ -1,6 +1,6 @@ import { XmlComponent } from "file/xml-components"; -import { Paragraph } from "../paragraph"; +import { LineRuleType, Paragraph } from "../paragraph"; import { Footnote, FootnoteType } from "./footnote/footnote"; import { ContinuationSeperatorRun } from "./footnote/run/continuation-seperator-run"; import { SeperatorRun } from "./footnote/run/seperator-run"; @@ -40,7 +40,7 @@ export class FootNotes extends XmlComponent { spacing: { after: 0, line: 240, - lineRule: "auto", + lineRule: LineRuleType.AUTO, }, children: [new SeperatorRun()], }), @@ -57,7 +57,7 @@ export class FootNotes extends XmlComponent { spacing: { after: 0, line: 240, - lineRule: "auto", + lineRule: LineRuleType.AUTO, }, children: [new ContinuationSeperatorRun()], }), diff --git a/src/file/paragraph/formatting/spacing.ts b/src/file/paragraph/formatting/spacing.ts index 4f646b33a6..7eac5cea72 100644 --- a/src/file/paragraph/formatting/spacing.ts +++ b/src/file/paragraph/formatting/spacing.ts @@ -1,11 +1,18 @@ // http://officeopenxml.com/WPspacing.php import { Attributes, XmlAttributeComponent, XmlComponent } from "file/xml-components"; +export enum LineRuleType { + AT_LEAST = "atLeast", + EXACTLY = "exactly", + AUTO = "auto", +} export interface ISpacingProperties { readonly after?: number; readonly before?: number; readonly line?: number; - readonly lineRule?: string; + readonly lineRule?: LineRuleType; + readonly beforeAutoSpacing?: boolean; + readonly afterAutoSpacing?: boolean; } class SpacingAttributes extends XmlAttributeComponent { diff --git a/src/file/styles/style/default-styles.ts b/src/file/styles/style/default-styles.ts index f8be7572ee..779f06b6c0 100644 --- a/src/file/styles/style/default-styles.ts +++ b/src/file/styles/style/default-styles.ts @@ -1,5 +1,7 @@ import { UnderlineType } from "file/paragraph/run/underline"; +import { LineRuleType } from "file/paragraph"; + import { IBaseCharacterStyleOptions, StyleForCharacter } from "./character-style"; import { IBaseParagraphStyleOptions, IParagraphStyleOptions, StyleForParagraph } from "./paragraph-style"; @@ -121,7 +123,7 @@ export class FootnoteText extends StyleForParagraph { spacing: { after: 0, line: 240, - lineRule: "auto", + lineRule: LineRuleType.AUTO, }, }, run: {