Search code examples
javascriptnewlinedocx

new line \n not registered when generating docx with js


I have javascript code to generate a docx document. This js code creates a Paragraph. With TextRun() I am adding text inside this paragraph:

 new docx.Paragraph({
    style: "text",
    children: [
        new docx.TextRun({
            text: 'line 1\n',
        }),
        new docx.TextRun({
            text: 'line 2\n',
        }),
        new docx.TextRun({
            text: 'line 3',
        })
    ]
})

\n gets ignored and all TextRun() are in the same line. Does anyone have an idea how to fix this?


Solution

  • Adding a break: element works fine, e.g.:

    new docx.Paragraph({
        style: "text",
        children: [
            new docx.TextRun({
                text: 'line 1',
                break: 1
            })
        ]
    })
    

    If the text: String is much longer, with more characters I can add a new TextRun() with an empty text as workaround:

    
    new docx.Paragraph({
        style: "text",
        children: [
            new docx.TextRun({
                text: 'This text is so long that the break is not at the end of the line'
            }),
            new docx.TextRun({
                text: '',
                break: 1
            })
        ]
    })