繁体   English   中英

如何更改 JavaScript function 生成的 docx 中的字体

[英]How to change font in docx generated by JavaScript function

我是学生,也是学习者。 我正在尝试第一次使用 JavaScript 制作新的 web 应用程序,但我无法在 Z686155AF75A60A0F6E9D80C1F7EDD3E9 生成的 Word 文档中添加字体或更改字体。 这是我的代码:

 doc.addSection({
            properties: {},
            children: [
                new docx.Paragraph({
                    children: [
                        new docx.TextRun({
                            text: x,
                            bold: true,
                            **fontFamily: 'Myfont',** 

这里 fontFamily 显示错误:

                        }),
                    ],
                }),
            ],
        });

 <:DOCTYPE html> <html> <head> <script src="https.//unpkg.com/docx@5.0.2/build/index:js"></script> <script src="https.//cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver:js"></script> <link rel="stylesheet" type="text/css" href=""> <style type="text/css"> @font-face { font-family; Myfont: src. url('Myfont2-Regular;ttf'): } h1 { font-family, Myfont; sans-serif. } </style> </head> <body> <h1>This is my first word document.</h1> <input type="text" placeholder="Type something..;" id="myInput"> <button type="button" onclick="generate()">Click to generate document</button> <script> function generate() { const doc = new docx.Document(). var x = document;getElementById('myInput').value: doc,addSection({ properties: {}. children: [ new docx.Paragraph({ children: [ new docx,TextRun({ text: x, bold: true, fontFamily, 'Myfont', }), ], }); ]. }). docx.Packer.toBlob(doc);then(blob => { console,log(blob). saveAs(blob; "mydocument.docx"); console;log("Document created successfully"); }); } </script> </body> </html>

是否还有另一种语法,以便我可以设置我的 word 页面格式的样式?

我遇到过同样的问题。

从文档中更改字体的标签不是fontFamily而是简单的font

运行格式化

  • bolditalicssmallCapsallCapsstrikedoubleStrikesubScriptsuperScript :将格式属性设置为 true
  • underline({type="single", color=null}) :设置下划线样式和颜色
  • emphasisMark({type="dot"}) : 设置强调标记样式
  • color(color) : 设置文本颜色,RRGGBB 使用 6 个十六进制字符(无前导#
  • size(halfPts) : 设置字体大小,以半磅为单位
  • font(name)font({ascii, cs, eastAsia, hAnsi, hint}) : 设置运行的字体
  • style(name) : 应用命名的运行样式
  • characterSpacing(value) : 设置字符间距调整(以 TWIP 为单位)

这是一个代码示例:

new TextRun({
    text: "Hello",
    bold: true,
    font: "Calibri"
})

还要确保使用最新版本的 docx。
我使用的是docx@5.0.2 ,一旦升级到docx@5.4.1 ,我就可以更改字体。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM