Skip to content

Examples

Felipe edited this page Mar 10, 2017 · 6 revisions

In practice

I used this library in my personal portfolio/CV website. Click generate CV for a demonstration. http://www.dolan.bio

General

Simple paragraph

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.addText(institutionText);
paragraph.addText(dateText);

doc.addParagraph(paragraph);
var exporter = new docx.LocalPacker(doc);
exporter.pack('My Document');

Using Express Packer

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.addText(institutionText);
paragraph.addText(dateText);

var exporter = new docx.ExpressPacker(doc, res);
exporter.pack('My Document');

Would produce:

University College London

5th Dec 2015


Complete example

Here is a complete example:

const docx = require('docx');
const { LocalPacker } = require('docx/build/export/packer/local.js');

const styles = new docx.Styles();
styles.createParagraphStyle('Heading1', 'Heading 1')
    .basedOn("Normal")
    .next("Normal")
    .quickFormat()
    .size(28)
    .bold()
    .italics()
    .spacing({after: 120});

styles.createParagraphStyle('Heading2', 'Heading 2')
    .basedOn("Normal")
    .next("Normal")
    .quickFormat()
    .size(26)
    .bold()
    .underline('double', 'FF0000')
    .spacing({before: 240, after: 120});

styles.createParagraphStyle('aside', 'Aside')
    .basedOn('Normal')
    .next('Normal')
    .color('999999')
    .italics()
    .indent(720)
    .spacing({line: 276});

styles.createParagraphStyle('wellSpaced', 'Well Spaced')
    .basedOn('Normal')
    .spacing({line: 276, before: 20 * 72 * .1, after: 20 * 72 * .05});

styles.createParagraphStyle('ListParagraph', 'List Paragraph')
    .quickFormat()
    .basedOn('Normal');


const numbering = new docx.Numbering();
const numberedAbstract = numbering.createAbstractNumbering();

numberedAbstract.createLevel(0, "lowerLetter", "%1)", "left");

const letterNumbering = numbering.createConcreteNumbering(numberedAbstract);


const doc = new docx.Document({
    creator: 'Clippy',
    title: 'Sample Document',
    description: 'A brief example of using docx',
});

doc.addParagraph(new docx.Paragraph('Test heading1, bold and italicized').heading1());
doc.addParagraph(new docx.Paragraph('Some simple content'));
doc.addParagraph(new docx.Paragraph('Test heading2 with double red underline').heading2());
doc.addParagraph(new docx.Paragraph('Option 1').setNumbering(letterNumbering, 0));
doc.addParagraph(new docx.Paragraph('Option 2').setNumbering(letterNumbering, 0));
doc.addParagraph(new docx.Paragraph('Option 3').setNumbering(letterNumbering, 0));
doc.addParagraph(new docx.Paragraph().addText(new docx.TextRun('Some monospaced content').font('Monospace')));
doc.addParagraph(new docx.Paragraph('An aside, in light gray italics and indented').style('aside'));
doc.addParagraph(new docx.Paragraph('This is normal, but well-spaced text').style('wellSpaced'));
doc.addParagraph(new docx.Paragraph('This is normal'));


const exporter = new LocalPacker(doc, styles, undefined, numbering);
exporter.pack('test.docx');
Clone this wiki locally