简体   繁体   English

如何使用 docx 从 angular 将图像添加到 Word 文档?

[英]How to add images to a Word document from angular using docx?

I am trying to display an image in a Word document generated from angular with the docx npm package, but i get an empty image with a red X and a text "The picture can't be displayed".我正在尝试在从 angular 生成的 Word 文档中显示图像,其中包含 docx npm package,但我得到一个带有红色 X 的空图像和文本“无法显示”。

I know docx supports jpeg, jpg, bmp, gif and png, and I have tried with png.我知道 docx 支持 jpeg、jpg、bmp、gif 和 png,并且我尝试过使用 png。 Also I tried to convert to a byte array, but I'm still getting the same result.我也尝试转换为字节数组,但我仍然得到相同的结果。

Any help on this would be great, thanks!对此的任何帮助都会很棒,谢谢!

const image = Media.addImage(document, "data:./assets/images/team/add.png;base64,", 400, 400);

document.addSection({
        children: [new Paragraph(image)],
    });

You have to transform the picture in base64.您必须在 base64 中转换图片。 Found the answer finally.终于找到了答案。 Thank you!谢谢!

Try this one试试这个

 const base64data = "ashvbly3458absfjbscldvsbvsVslfghdf...";//Base64 Image String const imageb64 = Media.addImage(document, Uint8Array.from(atob(base64data), c => c.charCodeAt(0)), 80, 80); document.addSection({ children: [new Paragraph(image)], });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将带有 npm 'docx' 包的图像添加到 Word 文档? - How to add images with npm 'docx' package to word document? 如何使用angular在HTML中呈现Word文档(.doc,.docx) - How do I render a Word document (.doc, .docx) in the HTML using angular 添加更多图像以使用 docx 节点库生成 word 文档时出现内存空间问题 - Memory Space issue while adding more images for generating word document using docx node library 使用循环添加到 docx.js 文件 - Using loop to add to docx.js Document 如何通过使用 Word JavaScript API 构建的插件从 Word 文档中提取页码 - How to extract page numbers from Word document via add-in built using the Word JavaScript API 如何使用docx4j阅读word文档并使用所有样式获取部分文档 - How to read word document and get parts of it with all styles using docx4j 如何使用 JavaScript 在浏览器中呈现 Word 文档(.doc、.docx)? - How do I render a Word document (.doc, .docx) in the browser using JavaScript? 将网页下载为Word文档(docx) - Download webpage as Word document (docx) 如何从一组URL中添加图像列表到文档? - How to add a list of images to the document from an array of URLs? 使用Docx.js在JavaScript中生成Word文档? - Generate a Word document in JavaScript with Docx.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM