dolanmiu / docx

Easily generate and modify .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.
https://docx.js.org/
MIT License
4.24k stars 479 forks source link

setting the heading to TITLE in first paragraph causes other paragraph to inherit the heading style of first paragraph #2722

Open shyamayadav154 opened 3 weeks ago

shyamayadav154 commented 3 weeks ago

issue : setting the heading to TITLE in first paragraph causes other paragraph to inherit the heading style of first paragraph

code to generate the error:

const docx = require('docx');
const fs = require('fs');

// Create a new document
const doc = new docx.Document({
    sections: [{
        properties: {},
        children: [
            new docx.Paragraph({
                text: "Document Title",
                heading: docx.HeadingLevel.TITLE,
                alignment: docx.AlignmentType.CENTER,
            }),
            new docx.Paragraph({
                text: "",
                spacing: {
                    before: 2000, // Adjust this value to move the content vertically
                },
            }),
            new docx.Paragraph({
                children: [
                    new docx.TextRun({
                        text: "This is the first centered paragraph.",
                    }),
                ],
                alignment: docx.AlignmentType.CENTER,
            }),
            new docx.Paragraph({
                children: [
                    new docx.TextRun({
                        text: "This is the second centered paragraph.",
                    }),
                ],
                alignment: docx.AlignmentType.CENTER,
                spacing: {
                    before: 200, // Add some space between paragraphs
                },
            }),
            new docx.Paragraph({
                children: [
                    new docx.TextRun({
                        text: "This is the third centered paragraph.",
                    }),
                ],
                alignment: docx.AlignmentType.CENTER,
                spacing: {
                    before: 200,
                },
            }),
        ],
    }],
});

// Generate the document
docx.Packer.toBuffer(doc).then((buffer) => {
    fs.writeFileSync("centered_paragraphs_with_title.docx", buffer);
});
image