Docx: Prevent default Header creation

Created on 16 Jan 2019  路  7Comments  路  Source: dolanmiu/docx

Hi!
Using your library, we find out that it creates by default a header unless a template with headers array is specified.
Maybe could be possible to create the document without the default header (and also default footer) or add an option to remove it?
Thanks

Most helpful comment

@parweb77 , @gytisgreitai,

Workaround for this issue is set 'headers' and 'footers' in margins to null, in this way:

import * as fs from 'fs'
import { Document, TextRun, Paragraph, Packer } from 'docx'

const doc = new Document()

doc.addSection({
  margins: {
    top: 0,
    right: 0,
    bottom: 0,
    left: 0,
    headers: null,
    footers: null
  },
  children: [
    new Paragraph({
      children: [
        new TextRun('Hello World')
      ]
    })
  ]
})

Packer.toBuffer(doc)
  .then(buffer => {
    fs.writeFileSync('My Document.docx', buffer)
  })

All 7 comments

Yes correct, it creates a header and footer by default

What is the use-case for not creating a default header/footer?

Can you explain

Because the default header has margin, and even if I left the header empty, the printed file has extra margin.
Now, as workaround I overwrite the default header style setting the spacing to 0, but I was wondering if it could be possible to remove it or define an option to prevent its creation.

Ah yes, you have a good point there

I will think of a solution for v5

Can you suggest how to set the header margin to 0 ? I am creating a document with no header or footer and I don't want the extra margin on the document.

@Mitiryl how do you overwrite default header style?

@parweb77 , @gytisgreitai,

Workaround for this issue is set 'headers' and 'footers' in margins to null, in this way:

import * as fs from 'fs'
import { Document, TextRun, Paragraph, Packer } from 'docx'

const doc = new Document()

doc.addSection({
  margins: {
    top: 0,
    right: 0,
    bottom: 0,
    left: 0,
    headers: null,
    footers: null
  },
  children: [
    new Paragraph({
      children: [
        new TextRun('Hello World')
      ]
    })
  ]
})

Packer.toBuffer(doc)
  .then(buffer => {
    fs.writeFileSync('My Document.docx', buffer)
  })

@arthurstelmach Thanks for your trick. Very usefull.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sleepinzombie picture sleepinzombie  路  3Comments

jacwright picture jacwright  路  6Comments

davideickhoff picture davideickhoff  路  6Comments

hmouhtar picture hmouhtar  路  6Comments

acgrama picture acgrama  路  5Comments