PatchDocument: patch paragraph heading is not applied
When adding paragraph to patches the heading is completely ignored. Example code:
var patchChildren = [];
this.textViewData.forEach((item) => {
let title = `${item.code} ${item.title}`;
patchChildren.push(
new Paragraph({
text: title,
heading: item.type == 'CHAPTER' ? HeadingLevel.HEADING_1 : HeadingLevel.HEADING_2,
})
);
});
console.log(patchChildren);
await patchDocument(blobTemplate, {
outputType: 'blob',
keepOriginalStyles: true,
patches: {
paragraph_replace: {
type: PatchType.PARAGRAPH,
children: patchChildren,
},
},
}).then((doc) => {
//fs.writeFileSync('My Document.docx', doc);
const blob = new Blob([doc], {
type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
});
saveAs(blob, 'patchedfile.docx'); // save as docx file
});
Can you try now on 9.0.0? I have released a version with some fixes
Nope, same output... Updated code for v9.0.0 to:
var blobTemplate = await fetch('/simple-template.docx').then((response) => {
if (!response.ok) {
throw new Error('Failed to fetch STUDIO-template.docx');
}
return response.blob();
});
console.log(blobTemplate);
//console.log(blobData);
/* EXPORT from docx-library*/
var patchChildren = [];
this.textViewData.forEach((item) => {
let title = `${item.code} ${item.title}`;
patchChildren.push(
new Paragraph({
text: title,
heading: item.type == 'CHAPTER' ? HeadingLevel.HEADING_1 : HeadingLevel.HEADING_2,
})
);
});
console.log(patchChildren);
await patchDocument({
outputType: 'blob',
data: blobTemplate,
keepOriginalStyles: true,
patches: {
paragraph_replace: {
type: PatchType.DOCUMENT,
children: patchChildren,
},
},
}).then((blob) => {
saveAs(blob, 'patchedfile.docx'); // save as docx file
});
Can you give me a sample Word document along with code so i can reproduce it?
When I run https://github.com/dolanmiu/docx/blob/master/demo/85-template-document.ts
It seems to work fine
Here is the code from the 85 template converted to browser-code:
async exportDocx() {
// Patch a document with patches
var blobTemplate = await fetch('/simple-template.docx').then((response) => {
if (!response.ok) {
throw new Error('Failed to fetch simple-template.docx');
}
return response.blob();
});
patchDocument({
outputType: 'blob',
data: blobTemplate,
patches: {
name: {
type: PatchType.PARAGRAPH,
children: [new TextRun('Sir. '), new TextRun('John Doe'), new TextRun('(The Conqueror)')],
},
table_heading_1: {
type: PatchType.PARAGRAPH,
children: [new TextRun('Heading wow!')],
},
item_1: {
type: PatchType.PARAGRAPH,
children: [
new TextRun('#657'),
new ExternalHyperlink({
children: [
new TextRun({
text: 'BBC News Link',
}),
],
link: 'https://www.bbc.co.uk/news',
}),
],
},
paragraph_replace: {
type: PatchType.DOCUMENT,
children: [
new Paragraph({
text: 'Lorem ipsum Heading 1?',
heading: HeadingLevel.HEADING_1,
}),
/*new Paragraph('Lorem ipsum paragraph'),
new Paragraph('Another paragraph'),
new Paragraph({
children: [
new TextRun('This is a '),
new ExternalHyperlink({
children: [
new TextRun({
text: 'Google Link',
}),
],
link: 'https://www.google.co.uk',
}),
new ImageRun({
type: 'png',
data: fs.readFileSync('./demo/images/dog.png'),
transformation: { width: 100, height: 100 },
}),
],
}),*/
],
},
header_adjective: {
type: PatchType.PARAGRAPH,
children: [new TextRun('Delightful Header')],
},
footer_text: {
type: PatchType.PARAGRAPH,
children: [
new TextRun('replaced just as'),
new TextRun(' well'),
new ExternalHyperlink({
children: [
new TextRun({
text: 'BBC News Link',
}),
],
link: 'https://www.bbc.co.uk/news',
}),
],
},
/*image_test: {
type: PatchType.PARAGRAPH,
children: [
new ImageRun({
type: 'jpg',
data: fs.readFileSync('./demo/images/image1.jpeg'),
transformation: { width: 100, height: 100 },
}),
],
},*/
table: {
type: PatchType.DOCUMENT,
children: [
new Table({
rows: [
new TableRow({
children: [
new TableCell({
children: [new Paragraph({}), new Paragraph({})],
verticalAlign: VerticalAlign.CENTER,
}),
new TableCell({
children: [new Paragraph({}), new Paragraph({})],
verticalAlign: VerticalAlign.CENTER,
}),
new TableCell({
children: [new Paragraph({ text: 'bottom to top' }), new Paragraph({})],
textDirection: TextDirection.BOTTOM_TO_TOP_LEFT_TO_RIGHT,
}),
new TableCell({
children: [new Paragraph({ text: 'top to bottom' }), new Paragraph({})],
textDirection: TextDirection.TOP_TO_BOTTOM_RIGHT_TO_LEFT,
}),
],
}),
new TableRow({
children: [
new TableCell({
children: [
new Paragraph({
text: 'Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah Blah',
heading: HeadingLevel.HEADING_1,
}),
],
}),
new TableCell({
children: [
new Paragraph({
text: 'This text should be in the middle of the cell',
}),
],
verticalAlign: VerticalAlign.CENTER,
}),
new TableCell({
children: [
new Paragraph({
text: 'Text above should be vertical from bottom to top',
}),
],
verticalAlign: VerticalAlign.CENTER,
}),
new TableCell({
children: [
new Paragraph({
text: 'Text above should be vertical from top to bottom',
}),
],
verticalAlign: VerticalAlign.CENTER,
}),
],
}),
],
}),
],
},
},
}).then((blob) => {
saveAs(blob, 'My Document.docx'); // save as docx file
});
},
Result: paragraph is not a header, and the table is scrambled in office-cloud-version.
Result file: My Document(2).docx
Got it.
The table scrambling can be explained here:
https://github.com/dolanmiu/docx/issues/2723
Cool. I will look into the heading issue
Cool. I will look into the heading issue
Any progress?
Hi @dolanmiu , thanks for this awesome lib !
Have you been able to look into the issue ? The heading is not kept when using patcher.
If you can point me where I should look I can try to fix it. Thanks.