diff --git a/demo/demo7.js b/demo/demo7.js new file mode 100644 index 0000000000..e9cf0a6fec --- /dev/null +++ b/demo/demo7.js @@ -0,0 +1,14 @@ +const docx = require("../build"); + +var doc = new docx.File(undefined, { + orientation: "landscape", +}); + +var paragraph = new docx.Paragraph("Hello World"); + +doc.addParagraph(paragraph); + +var exporter = new docx.LocalPacker(doc); +exporter.pack("My Document"); + +console.log("Document created succesfully at project root!"); diff --git a/src/file/document/body/section-properties/page-size/page-size-attributes.ts b/src/file/document/body/section-properties/page-size/page-size-attributes.ts index 9664c48ef9..5a3cd90907 100644 --- a/src/file/document/body/section-properties/page-size/page-size-attributes.ts +++ b/src/file/document/body/section-properties/page-size/page-size-attributes.ts @@ -3,11 +3,13 @@ import { XmlAttributeComponent } from "file/xml-components"; export interface IPageSizeAttributes { width?: number; height?: number; + orientation?: string; } export class PageSizeAttributes extends XmlAttributeComponent { protected xmlKeys = { width: "w:w", height: "w:h", + orientation: "w:orient", }; } diff --git a/src/file/document/body/section-properties/page-size/page-size.ts b/src/file/document/body/section-properties/page-size/page-size.ts index a70584a896..ea93e1ebc0 100644 --- a/src/file/document/body/section-properties/page-size/page-size.ts +++ b/src/file/document/body/section-properties/page-size/page-size.ts @@ -2,13 +2,16 @@ import { XmlComponent } from "file/xml-components"; import { PageSizeAttributes } from "./page-size-attributes"; export class PageSize extends XmlComponent { - constructor(width: number, height: number) { + constructor(width: number, height: number, orientation: string) { super("w:pgSz"); + const flip = orientation === "landscape"; + this.root.push( new PageSizeAttributes({ - width: width, - height: height, + width: flip ? height : width, + height: flip ? width : height, + orientation: orientation, }), ); } diff --git a/src/file/document/body/section-properties/section-properties.ts b/src/file/document/body/section-properties/section-properties.ts index 991f8c893a..b10d9be3c7 100644 --- a/src/file/document/body/section-properties/section-properties.ts +++ b/src/file/document/body/section-properties/section-properties.ts @@ -27,6 +27,7 @@ export class SectionProperties extends XmlComponent { gutter: 0, space: 708, linePitch: 360, + orientation: "portrait", }; const mergedOptions = { @@ -34,7 +35,7 @@ export class SectionProperties extends XmlComponent { ...options, }; - this.root.push(new PageSize(mergedOptions.width, mergedOptions.height)); + this.root.push(new PageSize(mergedOptions.width, mergedOptions.height, mergedOptions.orientation)); this.root.push(new PageMargin(mergedOptions.top, mergedOptions.right, mergedOptions.bottom, mergedOptions.left, mergedOptions.header, mergedOptions.footer, mergedOptions.gutter)); this.root.push(new Columns(mergedOptions.space)); this.root.push(new DocumentGrid(mergedOptions.linePitch));