-
-
Notifications
You must be signed in to change notification settings - Fork 494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PatchDocument: patch paragraph heading is not applied #2735
Comments
Can you try now on |
Nope, same output... 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: |
Got it. The table scrambling can be explained here: 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 ? If you can point me where I should look I can try to fix it. Thanks. |
When adding paragraph to patches the heading is completely ignored. Example code:
The text was updated successfully, but these errors were encountered: