From a7f6224fb2df29dc620c25718a486b61f0b05164 Mon Sep 17 00:00:00 2001 From: Dolan Date: Tue, 21 Aug 2018 02:46:21 +0100 Subject: [PATCH] Made demo files into typescript files --- demo/demo1.js | 20 ------- demo/demo1.ts | 20 +++++++ demo/{demo10.js => demo10.ts} | 62 +++++++++++--------- demo/{demo11.js => demo11.ts} | 52 ++++++++++------- demo/{demo12.js => demo12.ts} | 19 +++--- demo/demo13.js | 26 --------- demo/demo13.ts | 28 +++++++++ demo/demo14.js | 24 -------- demo/demo14.ts | 28 +++++++++ demo/demo15.js | 14 ----- demo/demo15.ts | 18 ++++++ demo/demo16.js | 36 ------------ demo/demo16.ts | 40 +++++++++++++ demo/demo17.js | 17 ------ demo/demo17.ts | 21 +++++++ demo/{demo18.js => demo18.ts} | 18 +++--- demo/demo19.js | 20 ------- demo/demo19.ts | 20 +++++++ demo/demo2.js | 74 ----------------------- demo/demo2.ts | 78 +++++++++++++++++++++++++ demo/demo20.js | 17 ------ demo/demo20.ts | 21 +++++++ demo/demo21.js | 31 ---------- demo/demo21.ts | 37 ++++++++++++ demo/demo22.js | 26 --------- demo/demo22.ts | 27 +++++++++ demo/{demo23.js => demo23.ts} | 29 ++++----- demo/demo24.js | 15 ----- demo/demo24.ts | 18 ++++++ demo/demo25.js | 15 ----- demo/demo26.js | 21 ------- demo/demo3.js | 45 -------------- demo/demo3.ts | 46 +++++++++++++++ demo/demo4.js | 12 ---- demo/demo4.ts | 15 +++++ demo/demo5.js | 18 ------ demo/demo5.ts | 21 +++++++ demo/demo6.js | 25 -------- demo/demo6.ts | 29 +++++++++ demo/demo7.js | 14 ----- demo/demo7.ts | 18 ++++++ demo/demo8.js | 13 ----- demo/demo8.ts | 17 ++++++ demo/demo9.js | 14 ----- demo/demo9.ts | 17 ++++++ demo/index.js | 8 ++- package.json | 4 +- src/file/paragraph/formatting/indent.ts | 4 +- src/file/paragraph/paragraph.ts | 8 ++- tsconfig.json | 3 +- 50 files changed, 638 insertions(+), 585 deletions(-) delete mode 100644 demo/demo1.js create mode 100644 demo/demo1.ts rename demo/{demo10.js => demo10.ts} (83%) rename demo/{demo11.js => demo11.ts} (75%) rename demo/{demo12.js => demo12.ts} (50%) delete mode 100644 demo/demo13.js create mode 100644 demo/demo13.ts delete mode 100644 demo/demo14.js create mode 100644 demo/demo14.ts delete mode 100644 demo/demo15.js create mode 100644 demo/demo15.ts delete mode 100644 demo/demo16.js create mode 100644 demo/demo16.ts delete mode 100644 demo/demo17.js create mode 100644 demo/demo17.ts rename demo/{demo18.js => demo18.ts} (89%) delete mode 100644 demo/demo19.js create mode 100644 demo/demo19.ts delete mode 100644 demo/demo2.js create mode 100644 demo/demo2.ts delete mode 100644 demo/demo20.js create mode 100644 demo/demo20.ts delete mode 100644 demo/demo21.js create mode 100644 demo/demo21.ts delete mode 100644 demo/demo22.js create mode 100644 demo/demo22.ts rename demo/{demo23.js => demo23.ts} (82%) delete mode 100644 demo/demo24.js create mode 100644 demo/demo24.ts delete mode 100644 demo/demo25.js delete mode 100644 demo/demo26.js delete mode 100644 demo/demo3.js create mode 100644 demo/demo3.ts delete mode 100644 demo/demo4.js create mode 100644 demo/demo4.ts delete mode 100644 demo/demo5.js create mode 100644 demo/demo5.ts delete mode 100644 demo/demo6.js create mode 100644 demo/demo6.ts delete mode 100644 demo/demo7.js create mode 100644 demo/demo7.ts delete mode 100644 demo/demo8.js create mode 100644 demo/demo8.ts delete mode 100644 demo/demo9.js create mode 100644 demo/demo9.ts diff --git a/demo/demo1.js b/demo/demo1.js deleted file mode 100644 index fc8d4c31a1..0000000000 --- a/demo/demo1.js +++ /dev/null @@ -1,20 +0,0 @@ -const docx = require('../build'); -const fs = require('fs'); - -var doc = new docx.Document(); - -var paragraph = new docx.Paragraph("Hello World"); -var institutionText = new docx.TextRun("University College London").bold(); -var dateText = new docx.TextRun("5th Dec 2015").tab().bold(); -paragraph.addRun(institutionText); -paragraph.addRun(dateText); - -doc.addParagraph(paragraph); - -var packer = new docx.Packer(); - -packer.toBuffer(doc).then((buffer) => { - fs.writeFileSync('My Document.docx', buffer); -}); - -console.log('Document created successfully at project root!'); diff --git a/demo/demo1.ts b/demo/demo1.ts new file mode 100644 index 0000000000..ba3a0bcc2f --- /dev/null +++ b/demo/demo1.ts @@ -0,0 +1,20 @@ +// 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, Packer, Paragraph, TextRun } from "../build"; + +const doc = new Document(); + +const paragraph = new Paragraph("Hello World"); +const institutionText = new TextRun("Foo Bar").bold(); +const dateText = new TextRun("Github is the best").tab().bold(); +paragraph.addRun(institutionText); +paragraph.addRun(dateText); + +doc.addParagraph(paragraph); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo10.js b/demo/demo10.ts similarity index 83% rename from demo/demo10.js rename to demo/demo10.ts index 2b986dbf65..b55542b866 100644 --- a/demo/demo10.js +++ b/demo/demo10.ts @@ -1,8 +1,11 @@ -const docx = require("../build"); +// Add images to header and footer +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph, TextRun } from "../build"; const PHONE_NUMBER = "07534563401"; const PROFILE_URL = "https://www.linkedin.com/in/dolan1"; -const EMAIL = "docx@docx.com"; +const EMAIL = "docx@com"; const experiences = [ { @@ -122,13 +125,13 @@ const achievements = [ ]; class DocumentCreator { - create(data) { + public create(data): Document { const experiences = data[0]; const educations = data[1]; const skills = data[2]; const achivements = data[3]; - const document = new docx.Document(); - document.addParagraph(new docx.Paragraph("Dolan Miu").title()); + const document = new Document(); + document.addParagraph(new Paragraph("Dolan Miu").title()); document.addParagraph(this.createContactInfo(PHONE_NUMBER, PROFILE_URL, EMAIL)); document.addParagraph(this.createHeading("Education")); @@ -181,23 +184,23 @@ class DocumentCreator { document.addParagraph(this.createHeading("References")); document.addParagraph( - new docx.Paragraph( + new Paragraph( "Dr. Dean Mohamedally Director of Postgraduate Studies Department of Computer Science, University College London Malet Place, Bloomsbury, London WC1E d.mohamedally@ucl.ac.uk", ), ); - document.addParagraph(new docx.Paragraph("More references upon request")); + document.addParagraph(new Paragraph("More references upon request")); document.addParagraph( - new docx.Paragraph( + new Paragraph( "This CV was generated in real-time based on my Linked-In profile from my personal website www.dolan.bio.", ).center(), ); return document; } - createContactInfo(phoneNumber, profileUrl, email) { - const paragraph = new docx.Paragraph().center(); - const contactInfo = new docx.TextRun(`Mobile: ${phoneNumber} | LinkedIn: ${profileUrl} | Email: ${email}`); - const address = new docx.TextRun("Address: 58 Elm Avenue, Kent ME4 6ER, UK").break(); + createContactInfo(phoneNumber: string, profileUrl: string, email: string) { + const paragraph = new Paragraph().center(); + const contactInfo = new TextRun(`Mobile: ${phoneNumber} | LinkedIn: ${profileUrl} | Email: ${email}`); + const address = new TextRun("Address: 58 Elm Avenue, Kent ME4 6ER, UK").break(); paragraph.addRun(contactInfo); paragraph.addRun(address); @@ -206,17 +209,17 @@ class DocumentCreator { } createHeading(text) { - return new docx.Paragraph(text).heading1().thematicBreak(); + return new Paragraph(text).heading1().thematicBreak(); } createSubHeading(text) { - return new docx.Paragraph(text).heading2(); + return new Paragraph(text).heading2(); } createInstitutionHeader(institutionName, dateText) { - const paragraph = new docx.Paragraph().maxRightTabStop(); - const institution = new docx.TextRun(institutionName).bold(); - const date = new docx.TextRun(dateText).tab().bold(); + const paragraph = new Paragraph().maxRightTabStop(); + const institution = new TextRun(institutionName).bold(); + const date = new TextRun(dateText).tab().bold(); paragraph.addRun(institution); paragraph.addRun(date); @@ -225,8 +228,8 @@ class DocumentCreator { } createRoleText(roleText) { - const paragraph = new docx.Paragraph(); - const role = new docx.TextRun(roleText).italic(); + const paragraph = new Paragraph(); + const role = new TextRun(roleText).italic(); paragraph.addRun(role); @@ -234,32 +237,32 @@ class DocumentCreator { } createBullet(text) { - return new docx.Paragraph(text).bullet(); + return new Paragraph(text).bullet(); } createSkillList(skills) { - const paragraph = new docx.Paragraph(); + const paragraph = new Paragraph(); const skillConcat = skills.map((skill) => skill.name).join(", ") + "."; - paragraph.addRun(new docx.TextRun(skillConcat)); + paragraph.addRun(new TextRun(skillConcat)); return paragraph; } - createAchivementsList(achivements) { + public createAchivementsList(achivements): Paragraph { const arr = []; for (const achievement of achivements) { - arr.push(new docx.Paragraph(achievement.name).bullet()); + arr.push(new Paragraph(achievement.name).bullet()); } return arr; } createInterests(interests) { - const paragraph = new docx.Paragraph(); + const paragraph = new Paragraph(); - paragraph.addRun(new docx.TextRun(interests)); + paragraph.addRun(new TextRun(interests)); return paragraph; } @@ -308,7 +311,8 @@ const documentCreator = new DocumentCreator(); const doc = documentCreator.create([experiences, education, skills, achievements]); -var exporter = new docx.LocalPacker(doc); -exporter.pack("Dolan Miu CV"); +const packer = new Packer(); -console.log("Document created successfully at project root!"); +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo11.js b/demo/demo11.ts similarity index 75% rename from demo/demo11.js rename to demo/demo11.ts index f0597e0da4..3b0cf0b01f 100644 --- a/demo/demo11.js +++ b/demo/demo11.ts @@ -1,7 +1,9 @@ -const docx = require("../build"); -const fs = require('fs'); +// Setting styles with JavaScript configuration +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph, Table } from "../build"; -const doc = new docx.Document(undefined, { +const doc = new Document(undefined, { top: 700, right: 700, bottom: 700, @@ -52,7 +54,7 @@ doc.Styles.createParagraphStyle("normalPara", "Normal Para") .font("Calibri") .quickFormat() .leftTabStop(453.543307087) - .maxRightTabStop(453.543307087) + .maxRightTabStop() .size(26) .spacing({ line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 }); @@ -70,7 +72,7 @@ doc.Styles.createParagraphStyle("aside", "Aside") .next("Normal") .color("999999") .italics() - .indent(720) + .indent({ left: 720 }) .spacing({ line: 276 }); doc.Styles.createParagraphStyle("wellSpaced", "Well Spaced") @@ -105,29 +107,35 @@ doc.createParagraph("Sir,").style("normalPara"); doc.createParagraph("BRIEF DESCRIPTION").style("normalPara"); -var table = new docx.Table(4, 4); -var contentParagraph = table +const table = new Table(4, 4); +table .getRow(0) .getCell(0) - .addContent(new docx.Paragraph("Pole No.")); -table.properties.width = 10000; + .addContent(new Paragraph("Pole No.")); +table.Properties.width = 10000; doc.addTable(table); -var arrboth = [{ - image: "./demo/images/pizza.gif", - comment: "Test" -}, { - image: "./demo/images/pizza.gif", - comment: "Test 2" -}]; +const arrboth = [ + { + image: "./demo/images/pizza.gif", + comment: "Test", + }, + { + image: "./demo/images/pizza.gif", + comment: "Test 2", + }, +]; -arrboth.forEach(function(item) { +arrboth.forEach((item) => { const para = doc.createParagraph(); - para.createTextRun(doc.createImage(fs.readFileSync(item.image))); - para.properties.width = 60; - para.properties.height = 90; + para.addImage(doc.createImage(fs.readFileSync(item.image))); + para.Properties.width = 60; + para.Properties.height = 90; doc.createParagraph(item.comment).style("normalPara2"); }); -var exporter = new docx.LocalPacker(doc); -exporter.pack("My Document"); +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo12.js b/demo/demo12.ts similarity index 50% rename from demo/demo12.js rename to demo/demo12.ts index 87c7a405e0..a3479c3cbf 100644 --- a/demo/demo12.js +++ b/demo/demo12.ts @@ -1,9 +1,11 @@ -const docx = require("../build"); -const fs = require('fs'); +// Scaling images +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph } from "../build"; -var doc = new docx.Document(); +const doc = new Document(); -var paragraph = new docx.Paragraph("Hello World"); +const paragraph = new Paragraph("Hello World"); doc.addParagraph(paragraph); const image = doc.createImage(fs.readFileSync("./demo/images/pizza.gif")); @@ -12,11 +14,12 @@ const image3 = doc.createImage(fs.readFileSync("./demo/images/pizza.gif")); const image4 = doc.createImage(fs.readFileSync("./demo/images/pizza.gif")); image.scale(0.5); -image2.scale(1) +image2.scale(1); image3.scale(2.5); image4.scale(4); -var exporter = new docx.LocalPacker(doc); -exporter.pack("My Document"); +const packer = new Packer(); -console.log("Document created successfully at project root!"); +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo13.js b/demo/demo13.js deleted file mode 100644 index f141547d7a..0000000000 --- a/demo/demo13.js +++ /dev/null @@ -1,26 +0,0 @@ -// This example shows 3 styles -const fs = require('fs'); -const docx = require('../build'); - -const styles = fs.readFileSync('./demo/assets/custom-styles.xml', 'utf-8'); -const doc = new docx.Document({ - title: 'Title', - externalStyles: styles -}); - -doc.createParagraph('Cool Heading Text').heading1(); - -let paragraph = new docx.Paragraph('This is a custom named style from the template "MyFancyStyle"'); -paragraph.style('MyFancyStyle'); -doc.addParagraph(paragraph); - -doc.createParagraph('Some normal text') - -doc.createParagraph('MyFancyStyle again').style('MyFancyStyle'); -paragraph.style('MyFancyStyle'); -doc.addParagraph(paragraph); - -var exporter = new docx.LocalPacker(doc); -exporter.pack('My Document'); - -console.log('Document created successfully at project root!'); diff --git a/demo/demo13.ts b/demo/demo13.ts new file mode 100644 index 0000000000..b80b9fdc22 --- /dev/null +++ b/demo/demo13.ts @@ -0,0 +1,28 @@ +// This example shows 3 styles using XML styles +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph } from "../build"; + +const styles = fs.readFileSync("./demo/assets/custom-styles.xml", "utf-8"); +const doc = new Document({ + title: "Title", + externalStyles: styles, +}); + +doc.createParagraph("Cool Heading Text").heading1(); + +const paragraph = new Paragraph('This is a custom named style from the template "MyFancyStyle"'); +paragraph.style("MyFancyStyle"); +doc.addParagraph(paragraph); + +doc.createParagraph("Some normal text"); + +doc.createParagraph("MyFancyStyle again").style("MyFancyStyle"); +paragraph.style("MyFancyStyle"); +doc.addParagraph(paragraph); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo14.js b/demo/demo14.js deleted file mode 100644 index b2262f106a..0000000000 --- a/demo/demo14.js +++ /dev/null @@ -1,24 +0,0 @@ -const docx = require('../build'); - -var doc = new docx.Document(); - -doc.createParagraph("First Page").pageBreak() -doc.createParagraph("Second Page"); - -var pageNumber = new docx.TextRun().pageNumber() - -var pageoneheader = new docx.Paragraph("First Page Header ").right(); - -pageoneheader.addRun(pageNumber); -var firstPageHeader = doc.createFirstPageHeader(); -firstPageHeader.addParagraph(pageoneheader); - -var pagetwoheader = new docx.Paragraph("My Title ").right(); - -pagetwoheader.addRun(pageNumber) -doc.Header.addParagraph(pagetwoheader) - -var exporter = new docx.LocalPacker(doc); -exporter.pack('My Document'); - -console.log('Document created successfully at project root!'); diff --git a/demo/demo14.ts b/demo/demo14.ts new file mode 100644 index 0000000000..94d45a66a6 --- /dev/null +++ b/demo/demo14.ts @@ -0,0 +1,28 @@ +// Page numbers +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph, TextRun } from "../build"; + +const doc = new Document(); + +doc.createParagraph("First Page").pageBreak(); +doc.createParagraph("Second Page"); + +const pageNumber = new TextRun("Page ").pageNumber(); + +const pageoneheader = new Paragraph("First Page Header ").right(); + +pageoneheader.addRun(pageNumber); +const firstPageHeader = doc.createFirstPageHeader(); +firstPageHeader.addParagraph(pageoneheader); + +const pagetwoheader = new Paragraph("My Title ").right(); + +pagetwoheader.addRun(pageNumber); +doc.Header.addParagraph(pagetwoheader); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo15.js b/demo/demo15.js deleted file mode 100644 index 61d9351817..0000000000 --- a/demo/demo15.js +++ /dev/null @@ -1,14 +0,0 @@ -const docx = require('../build'); - -var doc = new docx.Document(); - -var paragraph = new docx.Paragraph("Hello World"); -var paragraph2 = new docx.Paragraph("Hello World on another page").pageBreakBefore(); - -doc.addParagraph(paragraph); -doc.addParagraph(paragraph2); - -var exporter = new docx.LocalPacker(doc); -exporter.pack('My Document'); - -console.log('Document created successfully at project root!'); diff --git a/demo/demo15.ts b/demo/demo15.ts new file mode 100644 index 0000000000..5da6c826b5 --- /dev/null +++ b/demo/demo15.ts @@ -0,0 +1,18 @@ +// Page break before example +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph } from "../build"; + +const doc = new Document(); + +const paragraph = new Paragraph("Hello World"); +const paragraph2 = new Paragraph("Hello World on another page").pageBreakBefore(); + +doc.addParagraph(paragraph); +doc.addParagraph(paragraph2); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo16.js b/demo/demo16.js deleted file mode 100644 index f850bc9b4b..0000000000 --- a/demo/demo16.js +++ /dev/null @@ -1,36 +0,0 @@ -const docx = require("../build"); - -var doc = new docx.Document(); - -var paragraph = new docx.Paragraph("Hello World").pageBreak(); - -doc.addParagraph(paragraph); - -var header = doc.createHeader(); -header.createParagraph("Header on another page"); -var footer = doc.createFooter(); -footer.createParagraph("Footer on another page"); - -doc.addSection({ - headerId: header.Header.ReferenceId, - footerId: footer.Footer.ReferenceId, - pageNumberStart: 1, - pageNumberFormatType: docx.PageNumberFormat.DECIMAL, -}); - -doc.createParagraph("hello"); - -doc.addSection({ - headerId: header.Header.ReferenceId, - footerId: footer.Footer.ReferenceId, - pageNumberStart: 1, - pageNumberFormatType: docx.PageNumberFormat.DECIMAL, - orientation: docx.PageOrientation.LANDSCAPE, -}); - -doc.createParagraph("hello in landscape"); - -var exporter = new docx.LocalPacker(doc); -exporter.pack("My Document"); - -console.log("Document created successfully at project root!"); diff --git a/demo/demo16.ts b/demo/demo16.ts new file mode 100644 index 0000000000..4b92dc0d7e --- /dev/null +++ b/demo/demo16.ts @@ -0,0 +1,40 @@ +// Multiple sections and headers +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, PageNumberFormat, PageOrientation, Paragraph } from "../build"; + +const doc = new Document(); + +const paragraph = new Paragraph("Hello World").pageBreak(); + +doc.addParagraph(paragraph); + +const header = doc.createHeader(); +header.createParagraph("Header on another page"); +const footer = doc.createFooter(); +footer.createParagraph("Footer on another page"); + +doc.addSection({ + headerId: header.Header.ReferenceId, + footerId: footer.Footer.ReferenceId, + pageNumberStart: 1, + pageNumberFormatType: PageNumberFormat.DECIMAL, +}); + +doc.createParagraph("hello"); + +doc.addSection({ + headerId: header.Header.ReferenceId, + footerId: footer.Footer.ReferenceId, + pageNumberStart: 1, + pageNumberFormatType: PageNumberFormat.DECIMAL, + orientation: PageOrientation.LANDSCAPE, +}); + +doc.createParagraph("hello in landscape"); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo17.js b/demo/demo17.js deleted file mode 100644 index 266e556b7d..0000000000 --- a/demo/demo17.js +++ /dev/null @@ -1,17 +0,0 @@ -const docx = require('../build'); - -var doc = new docx.Document(); - -var paragraph = new docx.Paragraph("Hello World").referenceFootnote(1); -var paragraph2 = new docx.Paragraph("Hello World").referenceFootnote(2); - -doc.addParagraph(paragraph); -doc.addParagraph(paragraph2); - -doc.createFootnote(new docx.Paragraph("Test")); -doc.createFootnote(new docx.Paragraph("My amazing reference")); - -var exporter = new docx.LocalPacker(doc); -exporter.pack('My Document'); - -console.log('Document created successfully at project root!'); diff --git a/demo/demo17.ts b/demo/demo17.ts new file mode 100644 index 0000000000..c48a2e67e8 --- /dev/null +++ b/demo/demo17.ts @@ -0,0 +1,21 @@ +// Footnotes +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph } from "../build"; + +const doc = new Document(); + +const paragraph = new Paragraph("Hello World").referenceFootnote(1); +const paragraph2 = new Paragraph("Hello World").referenceFootnote(2); + +doc.addParagraph(paragraph); +doc.addParagraph(paragraph2); + +doc.createFootnote(new Paragraph("Test")); +doc.createFootnote(new Paragraph("My amazing reference")); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo18.js b/demo/demo18.ts similarity index 89% rename from demo/demo18.js rename to demo/demo18.ts index 73a778a6c3..4a61488c1f 100644 --- a/demo/demo18.js +++ b/demo/demo18.ts @@ -1,15 +1,17 @@ // Insert image from a buffer -const docx = require('../build'); +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer } from "../build"; -var doc = new docx.Document(); +const doc = new Document(); - -const imageBase64Data = `iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAACzVBMVEUAAAAAAAAAAAAAAAA/AD8zMzMqKiokJCQfHx8cHBwZGRkuFxcqFSonJyckJCQiIiIfHx8eHh4cHBwoGhomGSYkJCQhISEfHx8eHh4nHR0lHBwkGyQjIyMiIiIgICAfHx8mHh4lHh4kHR0jHCMiGyIhISEgICAfHx8lHx8kHh4jHR0hHCEhISEgICAlHx8kHx8jHh4jHh4iHSIhHCEhISElICAkHx8jHx8jHh4iHh4iHSIhHSElICAkICAjHx8jHx8iHh4iHh4hHiEhHSEkICAjHx8iHx8iHx8hHh4hHiEkHSEjHSAjHx8iHx8iHx8hHh4kHiEkHiEjHSAiHx8hHx8hHh4kHiEjHiAjHSAiHx8iHx8hHx8kHh4jHiEjHiAjHiAiICAiHx8kHx8jHh4jHiEjHiAiHiAiHSAiHx8jHx8jHx8jHiAiHiAiHiAiHSAiHx8jHx8jHx8iHiAiHiAiHiAjHx8jHx8jHx8jHx8iHiAiHiAiHiAjHx8jHx8jHx8iHx8iHSAiHiAjHiAjHx8jHx8hHx8iHx8iHyAiHiAjHiAjHiAjHh4hHx8iHx8iHx8iHyAjHSAjHiAjHiAjHh4hHx8iHx8iHx8jHyAjHiAhHh4iHx8iHx8jHyAjHSAjHSAhHiAhHh4iHx8iHx8jHx8jHyAjHSAjHSAiHh4iHh4jHx8jHx8jHyAjHyAhHSAhHSAiHh4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qKBLgq3wqnofiTorF0Ax56Rg1J1elW+BBAsVe+My6iYq7IK6keBdOIseV2qn5Pb8f3MqkWAXf9ThM8c8lAOIotuFsF875lRrH5klRcG0+xcPwQ1oLxfeRAP4heQTnGL78X2rqlw2DK59SXAV/zKaiGMAuko5InCt68mcOan5+ohf+z1pP8lQY/GHZQMV4YD3FpXDp4qerqbF/lBWBswyi+AL+ia+maLgcRRQj4IYlY/UpauqKBsPJAxQF8NM1TRQ/RudSPAD34rK3scOuR8/HGcspxsJfOVS8NZbiGXiUtPgINU3v3WFDmx8pEuG3EiqKKVbCC1vm2iZqap5LAtCtleQf8F9sFYWDohzeJczYyQ4V2bEZFGsQgJRGqqqhS2phHTWn9lDkIhBTqWqxQZ+IsRvtdHY9AvI2VX2hW68nfqGmuQsCEl3JdjfCF8OW1bPdtwhQ0gm2mQzfRE3a7KCYj0BNZJs8+Kxf/r6WtTEI2FIqlsMfFgRB5A6KUnSe/vUkX0AnuvUIt8SjM1m6wWQymUwmk8lkMgXRf5vi8rLQxtUhAAAAAElFTkSuQmCC`; // doc.createImage(Buffer.from(imageBase64Data, 'base64')); -doc.createImage(Buffer.from(imageBase64Data, 'base64'), 100, 100); +doc.createImage(Buffer.from(imageBase64Data, "base64"), 100, 100); -var exporter = new docx.LocalPacker(doc); -exporter.pack('My Document'); +const packer = new Packer(); -console.log('Document created successfully at project root!'); +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo19.js b/demo/demo19.js deleted file mode 100644 index 3aafebc142..0000000000 --- a/demo/demo19.js +++ /dev/null @@ -1,20 +0,0 @@ -const fs = require("fs"); -const docx = require("../build"); - -var doc = new docx.Document(); - -var paragraph = new docx.Paragraph("Hello World"); -var institutionText = new docx.TextRun("Foo").bold(); -var dateText = new docx.TextRun("Bar").tab().bold(); -paragraph.addRun(institutionText); -paragraph.addRun(dateText); - -doc.addParagraph(paragraph); - -var exporter = new docx.BufferPacker(doc); -exporter.pack("My Document").then((buffer) => { - // At this point, you can do anything with the buffer, including casting it to a string etc. - console.log(buffer); - fs.writeFileSync('My Document.docx', buffer); - console.log("Document created successfully at project root!"); -}); diff --git a/demo/demo19.ts b/demo/demo19.ts new file mode 100644 index 0000000000..52b5fd6694 --- /dev/null +++ b/demo/demo19.ts @@ -0,0 +1,20 @@ +// Export to base64 string - Useful in a browser environment. +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph, TextRun } from "../build"; + +const doc = new Document(); + +const paragraph = new Paragraph("Hello World"); +const institutionText = new TextRun("Foo").bold(); +const dateText = new TextRun("Bar").tab().bold(); +paragraph.addRun(institutionText); +paragraph.addRun(dateText); + +doc.addParagraph(paragraph); + +const packer = new Packer(); + +packer.toBase64String(doc).then((str) => { + fs.writeFileSync("My Document.docx", str); +}); diff --git a/demo/demo2.js b/demo/demo2.js deleted file mode 100644 index 841e31b6bf..0000000000 --- a/demo/demo2.js +++ /dev/null @@ -1,74 +0,0 @@ -const docx = require('../build'); - -const doc = new docx.Document({ - creator: 'Clippy', - title: 'Sample Document', - description: 'A brief example of using docx', -}); - -doc.Styles.createParagraphStyle('Heading1', 'Heading 1') - .basedOn("Normal") - .next("Normal") - .quickFormat() - .size(28) - .bold() - .italics() - .spacing({after: 120}); - -doc.Styles.createParagraphStyle('Heading2', 'Heading 2') - .basedOn("Normal") - .next("Normal") - .quickFormat() - .size(26) - .bold() - .underline('double', 'FF0000') - .spacing({before: 240, after: 120}); - -doc.Styles.createParagraphStyle('aside', 'Aside') - .basedOn('Normal') - .next('Normal') - .color('999999') - .italics() - .indent(720) - .spacing({line: 276}); - -doc.Styles.createParagraphStyle('wellSpaced', 'Well Spaced') - .basedOn('Normal') - .spacing({line: 276, before: 20 * 72 * .1, after: 20 * 72 * .05}); - -doc.Styles.createParagraphStyle('ListParagraph', 'List Paragraph') - .quickFormat() - .basedOn('Normal'); - - -const numberedAbstract = doc.Numbering.createAbstractNumbering(); -numberedAbstract.createLevel(0, "lowerLetter", "%1)", "left"); - -doc.createParagraph('Test heading1, bold and italicized').heading1(); -doc.createParagraph('Some simple content'); -doc.createParagraph('Test heading2 with double red underline').heading2(); - -const letterNumbering = doc.Numbering.createConcreteNumbering(numberedAbstract); -const letterNumbering5 = doc.Numbering.createConcreteNumbering(numberedAbstract); -letterNumbering5.overrideLevel(0, 5); - -doc.createParagraph('Option1').setNumbering(letterNumbering, 0); -doc.createParagraph('Option5 -- override 2 to 5').setNumbering(letterNumbering5, 0); -doc.createParagraph('Option3').setNumbering(letterNumbering, 0); - -doc.createParagraph() - .createTextRun('Some monospaced content') - .font('Monospace'); - -doc.createParagraph('An aside, in light gray italics and indented').style('aside'); -doc.createParagraph('This is normal, but well-spaced text').style('wellSpaced'); -const para = doc.createParagraph(); -para.createTextRun('This is a bold run,').bold(); -para.createTextRun(' switching to normal '); -para.createTextRun('and then underlined ').underline(); -para.createTextRun('and back to normal.'); - -const exporter = new docx.LocalPacker(doc); -exporter.pack('My Document'); - -console.log('Document created successfully at project root!'); diff --git a/demo/demo2.ts b/demo/demo2.ts new file mode 100644 index 0000000000..f32779fa06 --- /dev/null +++ b/demo/demo2.ts @@ -0,0 +1,78 @@ +// Example on how to customise the look at feel using Styles +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer } from "../build"; + +const doc = new Document({ + creator: "Clippy", + title: "Sample Document", + description: "A brief example of using docx", +}); + +doc.Styles.createParagraphStyle("Heading1", "Heading 1") + .basedOn("Normal") + .next("Normal") + .quickFormat() + .size(28) + .bold() + .italics() + .spacing({ after: 120 }); + +doc.Styles.createParagraphStyle("Heading2", "Heading 2") + .basedOn("Normal") + .next("Normal") + .quickFormat() + .size(26) + .bold() + .underline("double", "FF0000") + .spacing({ before: 240, after: 120 }); + +doc.Styles.createParagraphStyle("aside", "Aside") + .basedOn("Normal") + .next("Normal") + .color("999999") + .italics() + .indent({ left: 720 }) + .spacing({ line: 276 }); + +doc.Styles.createParagraphStyle("wellSpaced", "Well Spaced") + .basedOn("Normal") + .spacing({ line: 276, before: 20 * 72 * 0.1, after: 20 * 72 * 0.05 }); + +doc.Styles.createParagraphStyle("ListParagraph", "List Paragraph") + .quickFormat() + .basedOn("Normal"); + +const numberedAbstract = doc.Numbering.createAbstractNumbering(); +numberedAbstract.createLevel(0, "lowerLetter", "%1)", "left"); + +doc.createParagraph("Test heading1, bold and italicized").heading1(); +doc.createParagraph("Some simple content"); +doc.createParagraph("Test heading2 with double red underline").heading2(); + +const letterNumbering = doc.Numbering.createConcreteNumbering(numberedAbstract); +const letterNumbering5 = doc.Numbering.createConcreteNumbering(numberedAbstract); +letterNumbering5.overrideLevel(0, 5); + +doc.createParagraph("Option1").setNumbering(letterNumbering, 0); +doc.createParagraph("Option5 -- override 2 to 5").setNumbering(letterNumbering5, 0); +doc.createParagraph("Option3").setNumbering(letterNumbering, 0); + +doc + .createParagraph() + .createTextRun("Some monospaced content") + .font("Monospace"); + +doc.createParagraph("An aside, in light gray italics and indented").style("aside"); +doc.createParagraph("This is normal, but well-spaced text").style("wellSpaced"); +const para = doc.createParagraph(); +para.createTextRun("This is a bold run,").bold(); +para.createTextRun(" switching to normal "); +para.createTextRun("and then underlined ").underline(); +para.createTextRun("and back to normal."); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo20.js b/demo/demo20.js deleted file mode 100644 index eef332c1ae..0000000000 --- a/demo/demo20.js +++ /dev/null @@ -1,17 +0,0 @@ -const docx = require("../build"); - -var doc = new docx.Document(); - -const table = doc.createTable(4, 4); -table - .getCell(2, 2) - .addContent(new docx.Paragraph("Hello")) - .CellProperties.Borders.addTopBorder(docx.BorderStyle.DASH_DOT_STROKED, 3, "red") - .addBottomBorder(docx.BorderStyle.DOUBLE, 3, "blue") - .addStartBorder(docx.BorderStyle.DOT_DOT_DASH, 3, "green") - .addEndBorder(docx.BorderStyle.DOT_DOT_DASH, 3, "#ff8000"); - -var exporter = new docx.LocalPacker(doc); -exporter.pack("My Document"); - -console.log("Document created successfully at project root!"); diff --git a/demo/demo20.ts b/demo/demo20.ts new file mode 100644 index 0000000000..f3f43edb26 --- /dev/null +++ b/demo/demo20.ts @@ -0,0 +1,21 @@ +// Add custom borders to table cell +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { BorderStyle, Document, Packer, Paragraph } from "../build"; + +const doc = new Document(); + +const table = doc.createTable(4, 4); +table + .getCell(2, 2) + .addContent(new Paragraph("Hello")) + .CellProperties.Borders.addTopBorder(BorderStyle.DASH_DOT_STROKED, 3, "red") + .addBottomBorder(BorderStyle.DOUBLE, 3, "blue") + .addStartBorder(BorderStyle.DOT_DOT_DASH, 3, "green") + .addEndBorder(BorderStyle.DOT_DOT_DASH, 3, "#ff8000"); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo21.js b/demo/demo21.js deleted file mode 100644 index fd74e61b31..0000000000 --- a/demo/demo21.js +++ /dev/null @@ -1,31 +0,0 @@ -/** This demo shows how to create bookmarks then link to them with internal hyperlinks */ - -const docx = require("../build"); - -const loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam mi velit, convallis convallis scelerisque nec, faucibus nec leo. Phasellus at posuere mauris, tempus dignissim velit. Integer et tortor dolor. Duis auctor efficitur mattis. Vivamus ut metus accumsan tellus auctor sollicitudin venenatis et nibh. Cras quis massa ac metus fringilla venenatis. Proin rutrum mauris purus, ut suscipit magna consectetur id. Integer consectetur sollicitudin ante, vitae faucibus neque efficitur in. Praesent ultricies nibh lectus. Mauris pharetra id odio eget iaculis. Duis dictum, risus id pellentesque rutrum, lorem quam malesuada massa, quis ullamcorper turpis urna a diam. Cras vulputate metus vel massa porta ullamcorper. Etiam porta condimentum nulla nec tristique. Sed nulla urna, pharetra non tortor sed, sollicitudin molestie diam. Maecenas enim leo, feugiat eget vehicula id, sollicitudin vitae ante."; - -const doc = new docx.Document({ - creator: 'Clippy', - title: 'Sample Document', - description: 'A brief example of using docx with bookmarks and internal hyperlinks', -}); - -const anchorId = "anchorID"; - -// First create the bookmark -const bookmark = doc.createBookmark(anchorId, "Lorem Ipsum"); -// That has header styling -doc.createParagraph().addBookmark(bookmark).heading1(); -doc.createParagraph("\n"); - -doc.createParagraph(loremIpsum); -doc.createParagraph().pageBreak(); - -// Now the link back up to the bookmark -const hyperlink = doc.createInternalHyperLink(anchorId, `Click me!`); -doc.createParagraph().addHyperLink(hyperlink); - -var exporter = new docx.LocalPacker(doc); -exporter.pack("My Document"); - -console.log("Document created successfully at project root!"); diff --git a/demo/demo21.ts b/demo/demo21.ts new file mode 100644 index 0000000000..b02da56065 --- /dev/null +++ b/demo/demo21.ts @@ -0,0 +1,37 @@ +// This demo shows how to create bookmarks then link to them with internal hyperlinks +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer } from "../build"; + +const loremIpsum = + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam mi velit, convallis convallis scelerisque nec, faucibus nec leo. Phasellus at posuere mauris, tempus dignissim velit. Integer et tortor dolor. Duis auctor efficitur mattis. Vivamus ut metus accumsan tellus auctor sollicitudin venenatis et nibh. Cras quis massa ac metus fringilla venenatis. Proin rutrum mauris purus, ut suscipit magna consectetur id. Integer consectetur sollicitudin ante, vitae faucibus neque efficitur in. Praesent ultricies nibh lectus. Mauris pharetra id odio eget iaculis. Duis dictum, risus id pellentesque rutrum, lorem quam malesuada massa, quis ullamcorper turpis urna a diam. Cras vulputate metus vel massa porta ullamcorper. Etiam porta condimentum nulla nec tristique. Sed nulla urna, pharetra non tortor sed, sollicitudin molestie diam. Maecenas enim leo, feugiat eget vehicula id, sollicitudin vitae ante."; + +const doc = new Document({ + creator: "Clippy", + title: "Sample Document", + description: "A brief example of using docx with bookmarks and internal hyperlinks", +}); + +const anchorId = "anchorID"; + +// First create the bookmark +const bookmark = doc.createBookmark(anchorId, "Lorem Ipsum"); +// That has header styling +doc + .createParagraph() + .addBookmark(bookmark) + .heading1(); +doc.createParagraph("\n"); + +doc.createParagraph(loremIpsum); +doc.createParagraph().pageBreak(); + +// Now the link back up to the bookmark +const hyperlink = doc.createInternalHyperLink(anchorId, `Click me!`); +doc.createParagraph().addHyperLink(hyperlink); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo22.js b/demo/demo22.js deleted file mode 100644 index 9bc7e9ac54..0000000000 --- a/demo/demo22.js +++ /dev/null @@ -1,26 +0,0 @@ -const docx = require('../build'); - -var doc = new docx.Document(); - - - -var paragraph1 = new docx.Paragraph().bidirectional(); -var textRun1 = new docx.TextRun("שלום עולם").rightToLeft(); -paragraph1.addRun(textRun1); -doc.addParagraph(paragraph1); - -var paragraph2 = new docx.Paragraph().bidirectional(); -var textRun2 = new docx.TextRun("שלום עולם").bold().rightToLeft(); -paragraph2.addRun(textRun2); -doc.addParagraph(paragraph2); - -var paragraph3 = new docx.Paragraph().bidirectional(); -var textRun3 = new docx.TextRun("שלום עולם").italic().rightToLeft(); -paragraph3.addRun(textRun3); -doc.addParagraph(paragraph3); - - -var exporter = new docx.LocalPacker(doc); -exporter.pack('My Document'); - -console.log('Document created successfully at project root!'); diff --git a/demo/demo22.ts b/demo/demo22.ts new file mode 100644 index 0000000000..c5051ab019 --- /dev/null +++ b/demo/demo22.ts @@ -0,0 +1,27 @@ +// This demo shows right to left for special languages +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph, TextRun } from "../build"; + +const doc = new Document(); + +const paragraph1 = new Paragraph().bidirectional(); +const textRun1 = new TextRun("שלום עולם").rightToLeft(); +paragraph1.addRun(textRun1); +doc.addParagraph(paragraph1); + +const paragraph2 = new Paragraph().bidirectional(); +const textRun2 = new TextRun("שלום עולם").bold().rightToLeft(); +paragraph2.addRun(textRun2); +doc.addParagraph(paragraph2); + +const paragraph3 = new Paragraph().bidirectional(); +const textRun3 = new TextRun("שלום עולם").italic().rightToLeft(); +paragraph3.addRun(textRun3); +doc.addParagraph(paragraph3); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo23.js b/demo/demo23.ts similarity index 82% rename from demo/demo23.js rename to demo/demo23.ts index 7fd5ebb6fb..47ee26a6f9 100644 --- a/demo/demo23.js +++ b/demo/demo23.ts @@ -1,19 +1,21 @@ // This demo adds an image to the Media cache, and then insert to the document afterwards -const docx = require("../build"); +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Media, Packer, Paragraph } from "../build"; -var doc = new docx.Document(); +const doc = new Document(); -var paragraph = new docx.Paragraph("Hello World"); +const paragraph = new Paragraph("Hello World"); doc.addParagraph(paragraph); -const image = docx.Media.addImage(doc, "./demo/images/image1.jpeg"); -const image2 = docx.Media.addImage(doc, "./demo/images/dog.png"); -const image3 = docx.Media.addImage(doc, "./demo/images/cat.jpg"); -const image4 = docx.Media.addImage(doc, "./demo/images/parrots.bmp"); -const image5 = docx.Media.addImage(doc, "./demo/images/pizza.gif"); +const image = Media.addImage(doc, "./demo/images/image1.jpeg"); +const image2 = Media.addImage(doc, "./demo/images/dog.png"); +const image3 = Media.addImage(doc, "./demo/images/cat.jpg"); +const image4 = Media.addImage(doc, "./demo/images/parrots.bmp"); +const image5 = Media.addImage(doc, "./demo/images/pizza.gif"); -const imageBase64Data = `iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAACzVBMVEUAAAAAAAAAAAAAAAA/AD8zMzMqKiokJCQfHx8cHBwZGRkuFxcqFSonJyckJCQiIiIfHx8eHh4cHBwoGhomGSYkJCQhISEfHx8eHh4nHR0lHBwkGyQjIyMiIiIgICAfHx8mHh4lHh4kHR0jHCMiGyIhISEgICAfHx8lHx8kHh4jHR0hHCEhISEgICAlHx8kHx8jHh4jHh4iHSIhHC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XSHFJHgVXUlxejCCbTvFAHiXdIJiXxyCK7KJ5FHoMZGK9xBcwyg2QpdlVMxEUM2iyIMuXXZQNF+HswxMsSAAJRQjoE//eoqDCXBSTO6f1xd+O0iyNRY6jaWi1ALNYCocZROj4JdEikroVkjFk9DcStXxpdfCD2MoXodu4RUU9ptxxmXssOfxnvDVcxRTod9FxyhqLoAqis5aPhwTDp9spRgEH2Q6KLbYoKqlaKTm6Isp0C/sJMnjFvhiERXPQvUNRe9p29lhR04CdBpC8Sl8YiuncIxEuzUUg4Dkgj+paVozygY9plPMh28SaymO9kabAopREGF3vt9MzeFFl8G7lRSZ8FFGK8XX4VA8QjEd7XrM3M0OXz8YCy+qKBLgq3wqnofiTorF0Ax56Rg1J1elW+BBAsVe+My6iYq7IK6keBdOIseV2qn5Pb8f3MqkWAXf9ThM8c8lAOIotuFsF875lRrH5klRcG0+xcPwQ1oLxfeRAP4heQTnGL78X2rqlw2DK59SXAV/zKaiGMAuko5InCt68mcOan5+ohf+z1pP8lQY/GHZQMV4YD3FpXDp4qerqbF/lBWBswyi+AL+ia+maLgcRRQj4IYlY/UpauqKBsPJAxQF8NM1TRQ/RudSPAD34rK3scOuR8/HGcspxsJfOVS8NZbiGXiUtPgINU3v3WFDmx8pEuG3EiqKKVbCC1vm2iZqap5LAtCtleQf8F9sFYWDohzeJczYyQ4V2bEZFGsQgJRGqqqhS2phHTWn9lDkIhBTqWqxQZ+IsRvtdHY9AvI2VX2hW68nfqGmuQsCEl3JdjfCF8OW1bPdtwhQ0gm2mQzfRE3a7KCYj0BNZJs8+Kxf/r6WtTEI2FIqlsMfFgRB5A6KUnSe/vUkX0AnuvUIt8SjM1m6wWQymUwmk8lkMgXRf5vi8rLQxtUhAAAAAElFTkSuQmCC` -const image6 = docx.Media.addImageFromBuffer(doc, Buffer.from(imageBase64Data, 'base64'), 100, 100); +const imageBase64Data = `iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAACzVBMVEUAAAAAAAAAA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Se/vUkX0AnuvUIt8SjM1m6wWQymUwmk8lkMgXRf5vi8rLQxtUhAAAAAElFTkSuQmCC`; +const image6 = Media.addImage(doc, Buffer.from(imageBase64Data, "base64"), 100, 100); // I am adding an image to the paragraph rather than the document to make the image inline paragraph.addImage(image5); @@ -25,7 +27,8 @@ doc.addImage(image4); doc.addImage(image5); doc.addImage(image6); -var exporter = new docx.LocalPacker(doc); -exporter.pack("My Document"); +const packer = new Packer(); -console.log("Document created successfully at project root!"); +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo24.js b/demo/demo24.js deleted file mode 100644 index a95189a6ed..0000000000 --- a/demo/demo24.js +++ /dev/null @@ -1,15 +0,0 @@ -// Add image to table cell -const docx = require('../build'); - -var doc = new docx.Document(); - -const table = doc.createTable(4, 4); -table.getCell(2, 2).addContent(new docx.Paragraph('Hello')); - -const image = docx.Media.addImage(doc, "./demo/images/image1.jpeg"); -table.getCell(1, 1).addContent(image); - -var exporter = new docx.LocalPacker(doc); -exporter.pack('My Document'); - -console.log('Document created successfully at project root!'); diff --git a/demo/demo24.ts b/demo/demo24.ts new file mode 100644 index 0000000000..23828aec65 --- /dev/null +++ b/demo/demo24.ts @@ -0,0 +1,18 @@ +// Add image to table cell +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Media, Packer, Paragraph } from "../build"; + +const doc = new Document(); + +const table = doc.createTable(4, 4); +table.getCell(2, 2).addContent(new Paragraph("Hello")); + +const image = Media.addImage(doc, "./demo/images/image1.jpeg"); +table.getCell(1, 1).addContent(image.Paragraph); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo25.js b/demo/demo25.js deleted file mode 100644 index 85dd29febe..0000000000 --- a/demo/demo25.js +++ /dev/null @@ -1,15 +0,0 @@ -const fs = require("fs"); -const docx = require("../build"); - -var doc = new docx.Document(); - -var paragraph = new docx.Paragraph("Hello World"); -var institutionText = new docx.TextRun("Foo").bold(); -var dateText = new docx.TextRun("Bar").tab().bold(); -paragraph.addRun(institutionText); -paragraph.addRun(dateText); - -doc.addParagraph(paragraph); - -var exporter = new docx.LocalPacker(doc); -exporter.packPdf("My Document"); diff --git a/demo/demo26.js b/demo/demo26.js deleted file mode 100644 index c88fdaea8e..0000000000 --- a/demo/demo26.js +++ /dev/null @@ -1,21 +0,0 @@ -const docx = require('../build'); -const fs = require('fs'); - -var doc = new docx.Document(); - -var paragraph = new docx.Paragraph("Hello World"); -var institutionText = new docx.TextRun("University College London").bold(); -var dateText = new docx.TextRun("5th Dec 2015").tab().bold(); -paragraph.addRun(institutionText); -paragraph.addRun(dateText); - -doc.addParagraph(paragraph); - -var packer = new docx.PdfPacker(); - -packer.toBuffer(doc).then((buffer) => { - console.log(buffer); - fs.writeFileSync('My Document.pdf', buffer); -}); - -console.log('Document created successfully at project root!'); diff --git a/demo/demo3.js b/demo/demo3.js deleted file mode 100644 index accd597905..0000000000 --- a/demo/demo3.js +++ /dev/null @@ -1,45 +0,0 @@ -const docx = require('../build'); - -var doc = new docx.Document(); - -const numbering = new docx.Numbering(); - -const abstractNum = numbering.createAbstractNumbering(); -abstractNum.createLevel(0, "upperRoman", "%1", "start") - .addParagraphProperty(new docx.Indent(720, 260)); -abstractNum.createLevel(1, "decimal", "%2.", "start") - .addParagraphProperty(new docx.Indent(1440, 980)); -abstractNum.createLevel(2, "lowerLetter", "%3)", "start") - .addParagraphProperty(new docx.Indent(2160, 1700)); - -const concrete = numbering.createConcreteNumbering(abstractNum); - -var topLevelP = new docx.Paragraph("Hey you"); -var subP = new docx.Paragraph("What's up fam"); -var secondSubP = new docx.Paragraph("Hello World 2"); -var subSubP = new docx.Paragraph("Yeah boi"); - -topLevelP.setNumbering(concrete, 0); -subP.setNumbering(concrete, 1); -secondSubP.setNumbering(concrete, 1); -subSubP.setNumbering(concrete, 2); - -doc.addParagraph(topLevelP); -doc.addParagraph(subP); -doc.addParagraph(secondSubP); -doc.addParagraph(subSubP); - -var bullet1 = new docx.Paragraph("Hey you").bullet(); -var bullet2 = new docx.Paragraph("What's up fam").bullet(1); -var bullet3 = new docx.Paragraph("Hello World 2").bullet(2); -var bullet4 = new docx.Paragraph("Yeah boi").bullet(3); - -doc.addParagraph(bullet1); -doc.addParagraph(bullet2); -doc.addParagraph(bullet3); -doc.addParagraph(bullet4); - -var exporter = new docx.LocalPacker(doc); -exporter.pack('My Document'); - -console.log('Document created successfully at project root!'); diff --git a/demo/demo3.ts b/demo/demo3.ts new file mode 100644 index 0000000000..ac3680ef32 --- /dev/null +++ b/demo/demo3.ts @@ -0,0 +1,46 @@ +// Numbering and bullet points example +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Indent, Numbering, Packer, Paragraph } from "../build"; + +const doc = new Document(); + +const numbering = new Numbering(); + +const abstractNum = numbering.createAbstractNumbering(); +abstractNum.createLevel(0, "upperRoman", "%1", "start").addParagraphProperty(new Indent({ left: 720, hanging: 260 })); +abstractNum.createLevel(1, "decimal", "%2.", "start").addParagraphProperty(new Indent({ left: 1440, hanging: 980 })); +abstractNum.createLevel(2, "lowerLetter", "%3)", "start").addParagraphProperty(new Indent({ left: 14402160, hanging: 1700 })); + +const concrete = numbering.createConcreteNumbering(abstractNum); + +const topLevelP = new Paragraph("Hey you"); +const subP = new Paragraph("What's up fam"); +const secondSubP = new Paragraph("Hello World 2"); +const subSubP = new Paragraph("Yeah boi"); + +topLevelP.setNumbering(concrete, 0); +subP.setNumbering(concrete, 1); +secondSubP.setNumbering(concrete, 1); +subSubP.setNumbering(concrete, 2); + +doc.addParagraph(topLevelP); +doc.addParagraph(subP); +doc.addParagraph(secondSubP); +doc.addParagraph(subSubP); + +const bullet1 = new Paragraph("Hey you").bullet(); +const bullet2 = new Paragraph("What's up fam").bullet(1); +const bullet3 = new Paragraph("Hello World 2").bullet(2); +const bullet4 = new Paragraph("Yeah boi").bullet(3); + +doc.addParagraph(bullet1); +doc.addParagraph(bullet2); +doc.addParagraph(bullet3); +doc.addParagraph(bullet4); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo4.js b/demo/demo4.js deleted file mode 100644 index 6ded74254c..0000000000 --- a/demo/demo4.js +++ /dev/null @@ -1,12 +0,0 @@ -const docx = require('../build'); - -var doc = new docx.Document(); - -const table = doc.createTable(4, 4); -table.getCell(2, 2).addContent(new docx.Paragraph('Hello')); - - -var exporter = new docx.LocalPacker(doc); -exporter.pack('My Document'); - -console.log('Document created successfully at project root!'); diff --git a/demo/demo4.ts b/demo/demo4.ts new file mode 100644 index 0000000000..eb6f5b8c09 --- /dev/null +++ b/demo/demo4.ts @@ -0,0 +1,15 @@ +// Example of how you would create a table and add data to it +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph } from "../build"; + +const doc = new Document(); + +const table = doc.createTable(4, 4); +table.getCell(2, 2).addContent(new Paragraph("Hello")); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo5.js b/demo/demo5.js deleted file mode 100644 index fbb2cd0eaf..0000000000 --- a/demo/demo5.js +++ /dev/null @@ -1,18 +0,0 @@ -const docx = require("../build"); -const fs = require('fs'); - -var doc = new docx.Document(); - -var paragraph = new docx.Paragraph("Hello World"); -doc.addParagraph(paragraph); - -doc.createImage(fs.readFileSync("./demo/images/image1.jpeg")); -doc.createImage(fs.readFileSync("./demo/images/dog.png")); -doc.createImage(fs.readFileSync("./demo/images/cat.jpg")); -doc.createImage(fs.readFileSync("./demo/images/parrots.bmp")); -doc.createImage(fs.readFileSync("./demo/images/pizza.gif")); - -var exporter = new docx.LocalPacker(doc); -exporter.pack("My Document"); - -console.log("Document created successfully at project root!"); diff --git a/demo/demo5.ts b/demo/demo5.ts new file mode 100644 index 0000000000..e56cdfc01e --- /dev/null +++ b/demo/demo5.ts @@ -0,0 +1,21 @@ +// Example of how to add images to the document - You can use Buffers, UInt8Arrays or Base64 strings +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph } from "../build"; + +const doc = new Document(); + +const paragraph = new Paragraph("Hello World"); +doc.addParagraph(paragraph); + +doc.createImage(fs.readFileSync("./demo/images/image1.jpeg")); +doc.createImage(fs.readFileSync("./demo/images/dog.png").toString("base64")); +doc.createImage(fs.readFileSync("./demo/images/cat.jpg")); +doc.createImage(fs.readFileSync("./demo/images/parrots.bmp")); +doc.createImage(fs.readFileSync("./demo/images/pizza.gif")); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo6.js b/demo/demo6.js deleted file mode 100644 index 939f590d05..0000000000 --- a/demo/demo6.js +++ /dev/null @@ -1,25 +0,0 @@ -const docx = require("../build"); - -var doc = new docx.Document(undefined, { - top: 0, - right: 0, - bottom: 0, - left: 0, -}); - -var paragraph = new docx.Paragraph("Hello World"); -var institutionText = new docx.TextRun("University College London").bold(); -var dateText = new docx.TextRun("5th Dec 2015").tab().bold(); -paragraph.addRun(institutionText); -paragraph.addRun(dateText); - -doc.addParagraph(paragraph); - -doc.createParagraph("Hello World").heading1(); -doc.createParagraph("University College London"); -doc.createParagraph("5th Dec 2015"); - -var exporter = new docx.LocalPacker(doc); -exporter.pack("My Document"); - -console.log("Document created successfully at project root!"); diff --git a/demo/demo6.ts b/demo/demo6.ts new file mode 100644 index 0000000000..5c9ff2798f --- /dev/null +++ b/demo/demo6.ts @@ -0,0 +1,29 @@ +// Example of how to change page borders +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, Paragraph, TextRun } from "../build"; + +const doc = new Document(undefined, { + top: 0, + right: 0, + bottom: 0, + left: 0, +}); + +const paragraph = new Paragraph("Hello World"); +const institutionText = new TextRun("Foo bar").bold(); +const dateText = new TextRun("Github is the best").tab().bold(); +paragraph.addRun(institutionText); +paragraph.addRun(dateText); + +doc.addParagraph(paragraph); + +doc.createParagraph("Hello World").heading1(); +doc.createParagraph("Foo bar"); +doc.createParagraph("Github is the best"); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo7.js b/demo/demo7.js deleted file mode 100644 index f60dcd1911..0000000000 --- a/demo/demo7.js +++ /dev/null @@ -1,14 +0,0 @@ -const docx = require("../build"); - -var doc = new docx.Document(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 successfully at project root!"); diff --git a/demo/demo7.ts b/demo/demo7.ts new file mode 100644 index 0000000000..fe9fccf928 --- /dev/null +++ b/demo/demo7.ts @@ -0,0 +1,18 @@ +// Example of how to set the document to landscape +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, PageOrientation, Paragraph } from "../build"; + +const doc = new Document(undefined, { + orientation: PageOrientation.LANDSCAPE, +}); + +const paragraph = new Paragraph("Hello World"); + +doc.addParagraph(paragraph); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo8.js b/demo/demo8.js deleted file mode 100644 index 03ba944473..0000000000 --- a/demo/demo8.js +++ /dev/null @@ -1,13 +0,0 @@ -const docx = require('../build'); - -var doc = new docx.Document(); - -doc.createParagraph("Hello World"); - -doc.Header.createParagraph("Header text"); -doc.Footer.createParagraph("Footer text"); - -var exporter = new docx.LocalPacker(doc); -exporter.pack('My Document'); - -console.log('Document created successfully at project root!'); \ No newline at end of file diff --git a/demo/demo8.ts b/demo/demo8.ts new file mode 100644 index 0000000000..c4fb96a78c --- /dev/null +++ b/demo/demo8.ts @@ -0,0 +1,17 @@ +// Add text to header and footer +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer } from "../build"; + +const doc = new Document(); + +doc.createParagraph("Hello World"); + +doc.Header.createParagraph("Header text"); +doc.Footer.createParagraph("Footer text"); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/demo9.js b/demo/demo9.js deleted file mode 100644 index c0a57cf8f6..0000000000 --- a/demo/demo9.js +++ /dev/null @@ -1,14 +0,0 @@ -const docx = require('../build'); -const fs = require('fs'); - -var doc = new docx.Document(); - -doc.createParagraph("Hello World"); - -doc.Header.createImage(fs.readFileSync("./demo/images/pizza.gif")); -doc.Footer.createImage(fs.readFileSync("./demo/images/pizza.gif")); - -var exporter = new docx.LocalPacker(doc); -exporter.pack('My Document'); - -console.log('Document created successfully at project root!'); diff --git a/demo/demo9.ts b/demo/demo9.ts new file mode 100644 index 0000000000..87fe9b5192 --- /dev/null +++ b/demo/demo9.ts @@ -0,0 +1,17 @@ +// Add images to header and footer +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer } from "../build"; + +const doc = new Document(); + +doc.createParagraph("Hello World"); + +doc.Header.createImage(fs.readFileSync("./demo/images/pizza.gif")); +doc.Footer.createImage(fs.readFileSync("./demo/images/pizza.gif")); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/demo/index.js b/demo/index.js index 8ae9aac536..e6c7d926d0 100644 --- a/demo/index.js +++ b/demo/index.js @@ -18,12 +18,16 @@ prompt.start(); prompt.get(schema, function (err, result) { var demoNumber = result.number; - var filePath = `./demo/demo${demoNumber}.js`; + var filePath = `./demo/demo${demoNumber}.ts`; if (!fs.existsSync(filePath)) { console.error(`demo${demoNumber} does not exist: ${filePath}`); return; } console.log(`Running demo ${demoNumber}`); - shelljs.exec(`node ${filePath}`); + if (shelljs.exec(`npm run ts-node -- ${filePath}`).code === 0) { + console.log("Document created successfully"); + } else { + console.error('Something went wrong with the demo'); + } }); diff --git a/package.json b/package.json index 98acfbfe79..3132ae6445 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ "typedoc": "typedoc src/index.ts", "style": "prettier -l \"src/**/*.ts\"", "style.fix": "prettier \"src/**/*.ts\" --write", - "fix-types": "node types-absolute-fixer.js" + "fix-types": "node types-absolute-fixer.js", + "ts-node": "ts-node" }, "pre-commit": [ "style", @@ -76,6 +77,7 @@ "rimraf": "^2.5.2", "shelljs": "^0.7.7", "sinon": "^5.0.7", + "ts-node": "^7.0.1", "tslint": "^5.11.0", "typedoc": "^0.11.1", "typescript": "2.9.2", diff --git a/src/file/paragraph/formatting/indent.ts b/src/file/paragraph/formatting/indent.ts index 0b42246689..cd7528d2b8 100644 --- a/src/file/paragraph/formatting/indent.ts +++ b/src/file/paragraph/formatting/indent.ts @@ -1,7 +1,7 @@ // http://officeopenxml.com/WPindentation.php import { XmlAttributeComponent, XmlComponent } from "file/xml-components"; -interface IIndentAttributesProperties { +export interface IIndentAttributesProperties { left?: number; hanging?: number; firstLine?: number; @@ -20,7 +20,7 @@ class IndentAttributes extends XmlAttributeComponent