当前位置: 首页 > news >正文

GO-实现简单文本格式 文本字体颜色、大小、突出

毫无疑问GO的生态就是一坨大便。老子英文水平小学啊。

实现简单文本格式 文本字体颜色、大小、突出显示等。

创建要给docx文件容器【我估算的】

doc := document.New()
defer doc.Close()

doc.SaveToFile("simple.docx")  把容器保存为文件

设置标题

创建自然段Paragraph

run设置文本内容

para := doc.AddParagraph()
run := para.AddRun()
para.SetStyle("Title")
run.AddText("Simple Document Formatting")

效果图

设置缩进

para = doc.AddParagraph()
para.Properties().SetFirstLineIndent(0.5 * measurement.Inch)run = para.AddRun()
run.AddText("A run is a string of characters with the same formatting. ")

设置粗体、字体、大小、颜色

run = para.AddRun()
run.Properties().SetBold(true)
run.Properties().SetFontFamily("Courier")
run.Properties().SetSize(15)
run.Properties().SetColor(color.Red)
run.AddText("Multiple runs with different formatting can exist in the same paragraph. ")

换行

run.AddBreak()

run = para.AddRun()
run.AddText("Adding breaks to a run will insert line breaks after the run. ")
run.AddBreak()
run.AddBreak()

输入文本

run = createParaRun(doc, "Runs support styling options:")

大写

run = createParaRun(doc, "small caps")run.Properties().SetSmallCaps(true)

画线和画两条

	run = createParaRun(doc, "strike")run.Properties().SetStrikeThrough(true)run = createParaRun(doc, "double strike")run.Properties().SetDoubleStrikeThrough(true)

其他

run = createParaRun(doc, "outline")run.Properties().SetOutline(true)run = createParaRun(doc, "emboss")run.Properties().SetEmboss(true)run = createParaRun(doc, "shadow")run.Properties().SetShadow(true)run = createParaRun(doc, "imprint")run.Properties().SetImprint(true)run = createParaRun(doc, "highlighting")run.Properties().SetHighlight(wml.ST_HighlightColorYellow)run = createParaRun(doc, "underline")run.Properties().SetUnderline(wml.ST_UnderlineWavyDouble, color.Red)run = createParaRun(doc, "text effects")run.Properties().SetEffect(wml.ST_TextEffectAntsRed)//选择编号样式?
nd := doc.Numbering.Definitions()[0]for i := 1; i < 5; i++ {p := doc.AddParagraph()
//设置编号等级?p.SetNumberingLevel(i - 1)
//设置编号样式p.SetNumberingDefinition(nd)run := p.AddRun()run.AddText(fmt.Sprintf("Level %d", i))}

完整DEMO代码

// Copyright 2017 FoxyUtils ehf. All rights reserved.
package main
//导包
import ( "fmt""os""github.com/unidoc/unioffice/color""github.com/unidoc/unioffice/common/license""github.com/unidoc/unioffice/document""github.com/unidoc/unioffice/measurement""github.com/unidoc/unioffice/schema/soo/wml"
)
//资本家的密钥
func init() {// Make sure to load your metered License API key prior to using the library.// If you need a key, you can sign up and create a free one at https://cloud.unidoc.ioerr := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))if err != nil {panic(err)}
}func main() {//创建docdoc := document.New()
//关闭docdefer doc.Close()para := doc.AddParagraph()run := para.AddRun()para.SetStyle("Title")run.AddText("Simple Document Formatting")para = doc.AddParagraph()para.SetStyle("Heading1")run = para.AddRun()run.AddText("Some Heading Text")para = doc.AddParagraph()para.SetStyle("Heading2")run = para.AddRun()run.AddText("Some Heading Text")para = doc.AddParagraph()para.SetStyle("Heading3")run = para.AddRun()run.AddText("Some Heading Text")para = doc.AddParagraph()para.Properties().SetFirstLineIndent(0.5 * measurement.Inch)run = para.AddRun()run.AddText("A run is a string of characters with the same formatting. ")run = para.AddRun()run.Properties().SetBold(true)run.Properties().SetFontFamily("Courier")run.Properties().SetSize(15)run.Properties().SetColor(color.Red)run.AddText("Multiple runs with different formatting can exist in the same paragraph. ")run = para.AddRun()run.AddText("Adding breaks to a run will insert line breaks after the run. ")run.AddBreak()run.AddBreak()run = createParaRun(doc, "Runs support styling options:")run = createParaRun(doc, "small caps")run.Properties().SetSmallCaps(true)run = createParaRun(doc, "strike")run.Properties().SetStrikeThrough(true)run = createParaRun(doc, "double strike")run.Properties().SetDoubleStrikeThrough(true)run = createParaRun(doc, "outline")run.Properties().SetOutline(true)run = createParaRun(doc, "emboss")run.Properties().SetEmboss(true)run = createParaRun(doc, "shadow")run.Properties().SetShadow(true)run = createParaRun(doc, "imprint")run.Properties().SetImprint(true)run = createParaRun(doc, "highlighting")run.Properties().SetHighlight(wml.ST_HighlightColorYellow)run = createParaRun(doc, "underline")run.Properties().SetUnderline(wml.ST_UnderlineWavyDouble, color.Red)run = createParaRun(doc, "text effects")run.Properties().SetEffect(wml.ST_TextEffectAntsRed)nd := doc.Numbering.Definitions()[0]for i := 1; i < 5; i++ {p := doc.AddParagraph()p.SetNumberingLevel(i - 1)p.SetNumberingDefinition(nd)run := p.AddRun()run.AddText(fmt.Sprintf("Level %d", i))}doc.SaveToFile("simple.docx")
}func createParaRun(doc *document.Document, s string) document.Run {para := doc.AddParagraph()run := para.AddRun()run.AddText(s)return run
}

http://www.lryc.cn/news/195610.html

相关文章:

  • 铅华洗尽,粉黛不施,人工智能AI基于ProPainter技术去除图片以及视频水印(Python3.10)
  • latex,不带行号的algorithm
  • RocketMQ高性能核心原理与源码架构剖析
  • MATLAB中zp2tf函数用法
  • 解决:uniapp项目中调用小程序的chooseAddress() API失效
  • 2023 项目组总结(待完善)
  • Chrome浏览器 键盘快捷键整理
  • 【JAVA】集合与背后的逻辑框架,包装类,List,Map,Set,静态内部类
  • mac电脑版数字图像处理软件:ACDSee Photo Studio 9最新 for Mac
  • 酷开系统 | 酷开科技让你放肆嗨唱,聆听内心最真实的声音
  • PC电脑 VMware安装的linux CentOs7如何扩容磁盘?
  • redis极速的奥秘
  • three.js之初识three.js
  • 二维码智慧门牌管理系统:地址管理的现代革命
  • BricsCAD 23 for Mac:轻松驾驭CAD建模的强大工具
  • 如何利用Web应用防火墙应对未知威胁
  • 四、多线程服务器
  • 基于vue实现滑块动画效果
  • 探寻蓝牙的未来:从蓝牙1.0到蓝牙5.4,如何引领无线连接革命?
  • openssl 之 RSA加密数据设置OAEP SHA256填充方式
  • js将带标签的内容转为纯文本
  • 如何通过内网穿透实现远程连接NAS群晖drive并挂载电脑硬盘?
  • 4.2 抽象类
  • ITextRenderer将PDF转换为HTML详细教程
  • c#设计模式-行为型模式 之 备忘录模式
  • ffmpeg+安卓+yolo+RK3399部署
  • 发电机教程:小白必学的柴油发电机技巧
  • 基础课1——人工智能的分类和层次
  • C语言复杂表达式与指针高级
  • 【Python从入门到进阶】39、使用Selenium自动验证滑块登录