From ee6db1429a411dddf48351eb5467c2298d5cc867 Mon Sep 17 00:00:00 2001 From: Serey Roth <88986106+serey-roth@users.noreply.github.com> Date: Sun, 16 Feb 2025 10:33:21 -0800 Subject: [PATCH] Add shading and border settings to paragraph style options. (#2955) * Add shading and border settings to paragraph style options. * Fix prettier --------- Co-authored-by: Dolan Miu --- ...aragraph-style-with-shading-and-borders.ts | 60 +++++++++++++++++++ src/file/paragraph/properties.ts | 4 +- 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 demo/95-paragraph-style-with-shading-and-borders.ts diff --git a/demo/95-paragraph-style-with-shading-and-borders.ts b/demo/95-paragraph-style-with-shading-and-borders.ts new file mode 100644 index 0000000000..d6b0cf8b33 --- /dev/null +++ b/demo/95-paragraph-style-with-shading-and-borders.ts @@ -0,0 +1,60 @@ +import * as fs from "fs"; +import { BorderStyle, Document, Packer, Paragraph, TextRun } from "docx"; + +const doc = new Document({ + styles: { + paragraphStyles: [ + { + id: "withSingleBlackBordersAndYellowShading", + name: "Paragraph Style with Black Borders and Yellow Shading", + basedOn: "Normal", + paragraph: { + shading: { + color: "#fff000", + type: "solid", + }, + border: { + top: { + style: BorderStyle.SINGLE, + color: "#000000", + size: 4, + }, + bottom: { + style: BorderStyle.SINGLE, + color: "#000000", + size: 4, + }, + left: { + style: BorderStyle.SINGLE, + color: "#000000", + size: 4, + }, + right: { + style: BorderStyle.SINGLE, + color: "#000000", + size: 4, + }, + }, + }, + }, + ], + }, + sections: [ + { + children: [ + new Paragraph({ + style: "withSingleBlackBordersAndYellowShading", + children: [ + new TextRun({ + text: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", + }), + ], + }), + ], + }, + ], +}); + +Packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/src/file/paragraph/properties.ts b/src/file/paragraph/properties.ts index b8d050c6a2..360050db4e 100644 --- a/src/file/paragraph/properties.ts +++ b/src/file/paragraph/properties.ts @@ -38,6 +38,8 @@ export type ILevelParagraphStylePropertiesOptions = { }; export type IParagraphStylePropertiesOptions = { + readonly border?: IBordersOptions; + readonly shading?: IShadingAttributesProperties; readonly numbering?: | { readonly reference: string; @@ -49,7 +51,6 @@ export type IParagraphStylePropertiesOptions = { } & ILevelParagraphStylePropertiesOptions; export type IParagraphPropertiesOptions = { - readonly border?: IBordersOptions; readonly heading?: (typeof HeadingLevel)[keyof typeof HeadingLevel]; readonly bidirectional?: boolean; readonly pageBreakBefore?: boolean; @@ -58,7 +59,6 @@ export type IParagraphPropertiesOptions = { readonly bullet?: { readonly level: number; }; - readonly shading?: IShadingAttributesProperties; readonly widowControl?: boolean; readonly frame?: IFrameOptions; readonly suppressLineNumbers?: boolean;