docx
docx copied to clipboard
\t not being recognized in google docs
Code:
footers: {
default: new Footer({
children: [
new Paragraph({
style: 'footer',
children: [
new TextRun("Test\t"),
new TextRun({
children: ['Page ', PageNumber.CURRENT, ' of ', PageNumber.TOTAL_PAGES],
}),
],
tabStops: [
{
type: TabStopType.RIGHT,
position: TabStopPosition.MAX,
}
],
})
],
}),
}
Result:

Checking the docx
file in Google Docs
Hi there!
I think the understanding of tabs should be so that \t
affects only one textRun, so this works for me:
const fs = require('fs');
const { Document, Packer, Paragraph, TextRun, Footer, TabStopType, TabStopPosition, PageNumber} = require('docx');
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new TextRun('Lorem \t ipsum dolor sit amet'),
],
}),
],
footers: {
default: new Footer({
children: [
new Paragraph({
style: 'footer',
children: [
new TextRun({
children: ['Test \t', 'Page ', PageNumber.CURRENT, ' of ', PageNumber.TOTAL_PAGES],
}),
],
tabStops: [{
type: TabStopType.RIGHT,
position: TabStopPosition.MAX,
}],
})
],
}),
}
},
],
});
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("tabTest.docx", buffer);
});
And just in case, if you try to produce page number on the right via tabs then the footer text length has to be smaller, than page size otherwise it goes fancy looking (not on the same line as top line of the text)
Thanks @anti-the-social! but I'm still getting the same result
Seems like a compatibility issue. It works in MS Word but not in Google docs @anti-the-social
I think this issue should be sort of risen to the google docs, not to this particular library. And for you, I'd suggest using iframe for page numbering https://docx.js.org/#/usage/text-frames?id=intro Be careful with it, since alignment to the right isn't working yet https://github.com/dolanmiu/docx/issues/933 And one more related issue https://github.com/dolanmiu/docx/issues/1082
I still can't get the right tab to be recognized in google docs
Fixed in a PR
Now there is a dedicated Tab
object which has perfect compatability
Example of usage:
https://github.com/dolanmiu/docx/blob/master/demo/1-basic.ts