First, if you didn't have it yet, get access to the officegen module:
const officegen = require('officegen')
Now you have few ways to use it to create a docx based document. The simple way is to use this code:
let docx = officegen('docx')
But if you want to pass some settings then you should use the following format:
let docx = officegen({
type: 'docx', // We want to create a Microsoft Word document.
... // Extra options goes here.
})
You can always change some of these settings after creating the docx object using there methods:
docx.setDocTitle('...')
docx.setDocSubject('...')
docx.setDocKeywords('...')
docx.setDescription( '...')
docx.setDocCategory('...')
docx.setDocStatus('...')
To create a new paragraph in your document you need to create a parahpaph object from your main docx object:
let pObj = docx.createP(options)
When the options are:
pObj.addText(textString, options)
When the options are:
All the text data in Word is saved in paragraphs. To add a new paragraph:
var pObj = docx.createP ();
Paragraph options:
pObj.options.align = 'center'; // Also 'right' or 'justify'.
pObj.options.indentLeft = 1440; // Indent left 1 inch
pObj.options.indentFirstLine = 440; // Indent first line
Every list item is also a paragraph so:
var pObj = docx.createListOfDots ();
var pObj = docx.createListOfNumbers ();
Now you can fill the paragraph object with one or more text strings using the addText method:
pObj.addText ( 'Simple' );
pObj.addText ( ' with color', { color: '000088' } );
pObj.addText ( ' and back color.', { color: '00ffff', back: '000088' } );
pObj.addText ( 'Bold + underline', { bold: true, underline: true } );
pObj.addText ( 'Fonts face only.', { font_face: 'Arial' } );
pObj.addText ( ' Fonts face and size. ', { font_face: 'Arial', font_size: 40 } );
pObj.addText ( 'External link', { link: 'https://github.com' } );
// Hyperlinks to bookmarks also supported:
pObj.addText ( 'Internal link', { hyperlink: 'myBookmark' } );
// ...
// Start somewhere a bookmark:
pObj.startBookmark ( 'myBookmark' );
// ...
// You MUST close your bookmark:
pObj.endBookmark ();
Add an image to a paragraph:
var path = require('path');
pObj.addImage ( path.resolve(__dirname, 'myFile.png' ) );
pObj.addImage ( path.resolve(__dirname, 'myFile.png', { cx: 300, cy: 200 } ) );
To add a line break;
var pObj = docx.createP ();
pObj.addLineBreak ();
To add a page break:
docx.putPageBreak ();
To add a horizontal line:
var pObj = docx.createP ();
pObj.addHorizontalLine ();
To add a back line:
var pObj = docx.createP ({ backline: 'E0E0E0' });
pObj.addText ( 'Backline text1' );
pObj.addText ( ' text2' );
To add a table:
var table = [
[{
val: "No.",
opts: {
cellColWidth: 4261,
b:true,
sz: '48',
spacingBefor: 120,
spacingAfter: 120,
spacingLine: 240,
spacingLineRule: 'atLeast',
shd: {
fill: "7F7F7F",
themeFill: "text1",
"themeFillTint": "80"
},
fontFamily: "Avenir Book"
}
},{
val: "Title1",
opts: {
b:true,
color: "A00000",
align: "right",
shd: {
fill: "92CDDC",
themeFill: "text1",
"themeFillTint": "80"
}
}
},{
val: "Title2",
opts: {
align: "center",
vAlign: "center",
cellColWidth: 42,
b:true,
sz: '48',
shd: {
fill: "92CDDC",
themeFill: "text1",
"themeFillTint": "80"
}
}
}],
[1,'All grown-ups were once children',''],
[2,'there is no harm in putting off a piece of work until another day.',''],
[3,'But when it is a matter of baobabs, that always means a catastrophe.',''],
[4,'You can include CR-LF inline\r\nfor multiple lines.',''],
[5,['Or you can provide lines within', 'a cell in an array'],''],
[6,'But when it is a matter of baobabs, that always means a catastrophe.',''],
[7,'watch out for the baobabs!','END'],
]
var tableStyle = {
tableColWidth: 4261,
tableSize: 24,
tableColor: "ada",
tableAlign: "left",
tableFontFamily: "Comic Sans MS",
spacingBefor: 120, // default is 100
spacingAfter: 120, // default is 100
spacingLine: 240, // default is 240
spacingLineRule: 'atLeast', // default is atLeast
indent: 100, // table indent, default is 0
fixedLayout: true, // default is false
borders: true, // default is false. if true, default border size is 4
borderSize: 2, // To use this option, the 'borders' must set as true, default is 4
}
docx.createTable (table, tableStyle);
If you want to customize the border style, you can use the 'borderStyle' option:javascript const style = { '@w:val': 'single', '@w:sz': '3', '@w:space': '1', '@w:color': 'DF0000' } const borderStyle = { 'w:top': style, 'w:bottom': style, 'w:left': style, 'w:right': style, 'w:insideH': style, 'w:insideV': style, } const tableStyle = { tableColWidth: 4261, tableSize: 24, tableColor: 'ada', tableAlign: 'left', tableFontFamily: 'Comic Sans MS', borderStyle: borderStyle }
Header and footer:
// Add a header:
var header = docx.getHeader ().createP ();
header.addText ( 'This is the header' );
// Please note that the object header here is a paragraph object so you can use ANY of the paragraph API methods also for header and footer.
// The getHeader () method excepting a string parameter:
// getHeader ( 'even' ) - change the header for even pages.
// getHeader ( 'first' ) - change the header for the first page only.
// to do all of that for the footer, use the getFooter instead of getHeader.
// and sorry, right now only createP is supported (so only creating a paragraph) so no tables, etc.
To Create Word Document by json:
var table = [
[{
val: "No.",
opts: {
cellColWidth: 4261,
b:true,
sz: '48',
shd: {
fill: "7F7F7F",
themeFill: "text1",
"themeFillTint": "80"
},
fontFamily: "Avenir Book"
}
},{
val: "Title1",
opts: {
b:true,
color: "A00000",
align: "right",
shd: {
fill: "92CDDC",
themeFill: "text1",
"themeFillTint": "80"
}
}
},{
val: "Title2",
opts: {
align: "center",
cellColWidth: 42,
b:true,
sz: '48',
shd: {
fill: "92CDDC",
themeFill: "text1",
"themeFillTint": "80"
}
}
}],
[1,'All grown-ups were once children',''],
[2,'there is no harm in putting off a piece of work until another day.',''],
[3,'But when it is a matter of baobabs, that always means a catastrophe.',''],
[4,'watch out for the baobabs!','END'],
]
var tableStyle = {
tableColWidth: 4261,
tableSize: 24,
tableColor: "ada",
tableAlign: "left",
tableFontFamily: "Comic Sans MS"
}
var data = [[{
type: "text",
val: "Simple"
}, {
type: "text",
val: " with color",
opt: { color: '000088' }
}, {
type: "text",
val: " and back color.",
opt: { color: '00ffff', back: '000088' }
}, {
type: "linebreak"
}, {
type: "text",
val: "Bold + underline",
opt: { bold: true, underline: true }
}], {
type: "horizontalline"
}, [{ backline: 'EDEDED' }, {
type: "text",
val: " backline text1.",
opt: { bold: true }
}, {
type: "text",
val: " backline text2.",
opt: { color: '000088' }
}], {
type: "text",
val: "Left this text.",
lopt: { align: 'left' }
}, {
type: "text",
val: "Center this text.",
lopt: { align: 'center' }
}, {
type: "text",
val: "Right this text.",
lopt: { align: 'right' }
}, {
type: "text",
val: "Fonts face only.",
opt: { font_face: 'Arial' }
}, {
type: "text",
val: "Fonts face and size.",
opt: { font_face: 'Arial', font_size: 40 }
}, {
type: "table",
val: table,
opt: tableStyle
}, [{ // arr[0] is common option.
align: 'right'
}, {
type: "image",
path: path.resolve(__dirname, 'images_for_examples/sword_001.png')
},{
type: "image",
path: path.resolve(__dirname, 'images_for_examples/sword_002.png')
}], {
type: "pagebreak"
}
]
docx.createByJson(data);