Welcome folks today in this blog post we will be creating the word docx
files and add text and images
inside it using the officegen
library in javascript. All the full source code of the application is shown below.
Get Started
In order to get started you need to make a new node.js
project using the below command
npm init -y
And now you need to install the below libraries
using the npm command as shown below
npm i officegen
npm i docx
And after that you need to create the index.js
file and copy paste the following code
index.js
1 2 3 4 5 6 7 |
const officegen = require('officegen'); const fs = require('fs'); const docx = officegen('docx'); // Create a new paragraph const p = docx.createP(); |
As you can see we are first of all importing the officegen
library and also the built in library of fs
to save the doc
file and inside it we are calling the createP()
method to create a new paragraph
of text inside the docx
document.
1 2 3 4 5 6 7 |
p.addText('Hello, world!'); // Add some formatted text to the paragraph p.addText('Formatted text:', { bold: true, underline: true }); // Add some more text to the paragraph p.addText('This is some more text.'); |
And now we are adding the text
using the addText()
method inside the paragraph and then we are having the line
breaks after the paragraph using the putPageBreak()
method. And also we are passing some options to the paragraph such as bold
and underline
the text.
Styling the Text inside DOCX Document
You can even style
and highlight
the text which is inserted in the paragraph as shown below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
let pObj = docx.createP() pObj.addText('Simple') pObj.addText(' with color', { color: '000088' }) pObj.addText(' and back color.', { color: '00ffff', back: '000088' }) pObj = docx.createP() pObj.addText('Since ') pObj.addText('officegen 0.2.12', { back: '00ffff', shdType: 'pct12', shdColor: 'ff0000' }) // Use pattern in the background. pObj.addText(' you can do ') pObj.addText('more cool ', { highlight: true }) // Highlight! pObj.addText('stuff!', { highlight: 'darkGreen' }) // Different highlight color. |
As you can see we are passing the style
options such as the background and foreground color
and also we are highlighting the text. We can even add hyperlinks
inside the text as shown below
1 |
pObj.addText('external link', { link: 'https://github.com' }) |
Adding Multiple Pages inside DOCX Document
You can even add multiple
pages inside the word document
using the putPageBreak()
method as shown below. This will add a new page
inside the docx document.
1 |
docx.putPageBreak() |
Adding Images inside DOCX Document
Now we can use the addImage()
method to add the local
image which is stored inside the file system to the docx document as shown below
1 2 |
const img = fs.readFileSync('image.jpg'); docx.createP().addImage(img); |
You can even use another approach
by using directly the addImage()
method as shown below
1 2 3 4 |
pObj = docx.createP() // We can even add images: pObj.addImage('Screenshot_2.png') |
As you can see we are loading the image.jpg
image file and then adding it inside the paragraph
using the addImage()
method.
Saving the DOCX Document
Now lastly we will be saving the docx
file inside the file system using the generate()
method as shown below
1 2 3 |
// Save the document to a file const out = fs.createWriteStream('mydocument.docx'); docx.generate(out); |
FULL SOURCE CODE
index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
const officegen = require('officegen') const fs = require('fs') // Create an empty Word object: let docx = officegen('docx') // Officegen calling this function after finishing to generate the docx document: docx.on('finalize', function(written) { console.log( 'Finish to create a Microsoft Word document.' ) }) // Officegen calling this function to report errors: docx.on('error', function(err) { console.log(err) }) // Create a new paragraph: let pObj = docx.createP() pObj.addText('Simple') pObj.addText(' with color', { color: '000088' }) pObj.addText(' and back color.', { color: '00ffff', back: '000088' }) pObj = docx.createP() pObj.addText('Since ') pObj.addText('officegen 0.2.12', { back: '00ffff', shdType: 'pct12', shdColor: 'ff0000' }) // Use pattern in the background. pObj.addText(' you can do ') pObj.addText('more cool ', { highlight: true }) // Highlight! pObj.addText('stuff!', { highlight: 'darkGreen' }) // Different highlight color. pObj = docx.createP() pObj.addText('Even add ') pObj.addText('external link', { link: 'https://github.com' }) pObj.addText('!') pObj = docx.createP() pObj.addText('Bold + underline', { bold: true, underline: true }) pObj = docx.createP({ align: 'center' }) pObj.addText('Center this text', { border: 'dotted', borderSize: 12, borderColor: '88CCFF' }) pObj = docx.createP() pObj.options.align = 'right' pObj.addText('Align this text to the right.') pObj = docx.createP() pObj.addText('Those two lines are in the same paragraph,') pObj.addLineBreak() pObj.addText('but they are separated by a line break.') docx.putPageBreak() pObj = docx.createP() pObj.addText('Fonts face only.', { font_face: 'Arial' }) pObj.addText(' Fonts face and size.', { font_face: 'Arial', font_size: 40 }) docx.putPageBreak() pObj = docx.createP() // We can even add images: pObj.addImage('Screenshot_2.png') // Let's generate the Word document into a file: let out = fs.createWriteStream('example.docx') out.on('error', function(err) { console.log(err) }) // Async call to generate the output file: docx.generate(out) |