diff --git a/demo/26-paragraph-borders.ts b/demo/26-paragraph-borders.ts index 788bbfafcf..6b81954456 100644 --- a/demo/26-paragraph-borders.ts +++ b/demo/26-paragraph-borders.ts @@ -1,7 +1,7 @@ // Creates two paragraphs, one with a border and one without // Import from 'docx' rather than '../build' if you install from npm import * as fs from "fs"; -import { BorderStyle, Document, Packer, Paragraph } from "../build"; +import { BorderStyle, Document, Packer, Paragraph, TextRun } from "../build"; const doc = new Document({ sections: [ @@ -25,6 +25,25 @@ const doc = new Document({ }, }, }), + new Paragraph({ + children: [ + new TextRun({ + text: "This will ", + }), + new TextRun({ + text: "have a border.", + border: { + color: "auto", + space: 1, + style: BorderStyle.SINGLE, + size: 6, + }, + }), + new TextRun({ + text: " This will not.", + }), + ], + }), ], }, ], diff --git a/src/file/paragraph/run/properties.ts b/src/file/paragraph/run/properties.ts index 7b5d2418cd..29e67479a8 100644 --- a/src/file/paragraph/run/properties.ts +++ b/src/file/paragraph/run/properties.ts @@ -1,5 +1,6 @@ import { ChangeAttributes, IChangedAttributesProperties } from "../../track-revision/track-revision"; +import { BorderElement, IBorderOptions } from "file/border"; import { IShadingAttributesProperties, Shading } from "file/shading"; import { HpsMeasureElement, IgnoreIfEmptyXmlComponent, OnOffElement, StringValueElement, XmlComponent } from "file/xml-components"; import { EmphasisMark, EmphasisMarkType } from "./emphasis-mark"; @@ -43,6 +44,7 @@ export interface IRunStylePropertiesOptions { readonly emboss?: boolean; readonly imprint?: boolean; readonly revision?: IRunPropertiesChangeOptions; + readonly border?: IBorderOptions; } export interface IRunPropertiesOptions extends IRunStylePropertiesOptions { @@ -209,6 +211,10 @@ export class RunProperties extends IgnoreIfEmptyXmlComponent { if (options.revision) { this.push(new RunPropertiesChange(options.revision)); } + + if (options.border) { + this.push(new BorderElement("w:bdr", options.border)); + } } public push(item: XmlComponent): void { diff --git a/src/file/paragraph/run/run.spec.ts b/src/file/paragraph/run/run.spec.ts index 3e67e88793..09af5a524d 100644 --- a/src/file/paragraph/run/run.spec.ts +++ b/src/file/paragraph/run/run.spec.ts @@ -1,6 +1,7 @@ import { expect } from "chai"; import { Formatter } from "export/formatter"; +import { BorderStyle } from "file/border"; // import { FootnoteReferenceRun } from "file/footnotes/footnote/run/reference-run"; import { ShadingType } from "file/shading"; @@ -488,4 +489,36 @@ describe("Run", () => { }); }); }); + + describe("#border", () => { + it("should correctly set the border", () => { + const run = new Run({ + border: { + color: "auto", + space: 1, + style: BorderStyle.SINGLE, + size: 6, + }, + }); + const tree = new Formatter().format(run); + expect(tree).to.deep.equal({ + "w:r": [ + { + "w:rPr": [ + { + "w:bdr": { + _attr: { + "w:color": "auto", + "w:space": 1, + "w:sz": 6, + "w:val": "single", + }, + }, + }, + ], + }, + ], + }); + }); + }); });