Docx: How to maintain header distance from top

Created on 17 Sep 2020  路  5Comments  路  Source: dolanmiu/docx

The default header distance from top is 1.25cm. My task requires it to be tinier. I couldn't find anything in manual nor in templates. Does such function exist?

Most helpful comment

Side Remark: I think it would be cool to include these "convenience functions". Likely everyone is implementing them 馃槅 at some point.

Screenshot 2020-11-23 at 12 15 28

All 5 comments

We can change header and footer distance in addSection
doc.addSection({
margins: {
header: 400, //header distance from top
footer: 400, //footer distance
},
});

The default header distance from top is 1.25cm. My task requires it to be tinier. I couldn't find anything in manual nor in templates. Does such function exist?

I found a solution. Do it like the code below. 馃槃
You can ignore that function calculateMargin cuz I made it for easy coding.

doc.addSection({
    margins: {
        top: this.calculateMargin(3.06),
        right: this.calculateMargin(2.2),
        bottom: this.calculateMargin(2.5),
        left: this.calculateMargin(2.2),
        header: this.calculateMargin(1.5),
    },
    ...
});
...
calculateMargin(cm) {
    return Math.floor(cm / 2.54 * 72 * 20);
}

Side Remark: I think it would be cool to include these "convenience functions". Likely everyone is implementing them 馃槅 at some point.

Screenshot 2020-11-23 at 12 15 28

Great idea @thojansen! Created a PR to add Convenience functions into docx

super cool !!!

Was this page helpful?
0 / 5 - 0 ratings