Search code examples
javascriptreactjsdocx

new line not registered in docx.js


I am using the docxjs library. I am trying to add line breaks into the doc im generating but I'm unsure of how to do this. \n character gets ignored. How can i add line breaks to my doc?

The paragraph I want to add line breaks to:

new Paragraph( {
        text: '\n',
        children: [
          ...Object.entries( dataItem ).map( ([fieldName, fieldValue]) => new Paragraph( {
            text: fieldName,
            children: Object.entries( fieldValue ).map( ([dataSource, dataValue]) => new Paragraph( {
              text: dataSource,
              children: Object.values( dataValue ).map( value => new Paragraph(value) )
            }) )
          }) ),
          new Paragraph( {text: '_', children: [new PageBreak()]} )
        ]
      } )

Solution

  • I think that paragraph is equal newline in text. Isn't it?

    doc.addSection({
        properties: {},
        children: [
            new Paragraph({
                children: [
                    new TextRun("First line"),
                ],
            }),
            new Paragraph({
              children: [],  // Just newline without text
            }),
            new Paragraph({
                children: [
                    new TextRun("Second line"),
                ],
            }),
        ],
    });