Using the docx.js library, inserting the numbering attribute into the Paragraph objects causes the Word document it generates to become unopenable in Microsoft Word. It throws the error "Word experienced an error trying to open the file. Try these suggestions."
This is the code I used:
import { Document, Packer, Paragraph, TextRun, Numbering, convertInchesToTwip, LevelFormat, AlignmentType } from "docx";
function DocGen() {
const doc = new Document({
config: [
{
reference: "style-one",
levels: [
{
level: 1,
format: LevelFormat.UPPER_ROMAN,
text: "%1",
alignment: AlignmentType.START,
style: {
paragraph: {
indent: {left: convertInchesToTwip(5), hanging: convertInchesToTwip(3)},
},
},
},
],
},
],
sections: [
{
children: [
new Paragraph({
text: "Hi",
numbering: {
reference: "style-one",
level: 1,
},
}),
],
},
],
});
Packer.toBlob(doc).then((blob) => {
saveAs(blob, "My Document.docx");
});
}
I can open the Word document with other apps (like Pages or Preview), except the numbering does not show up in the document.
If I remove the numbering attribute, the Word documents generated will be openable again by Microsoft Word.