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


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

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

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

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

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

    设置标题

    创建自然段Paragraph

    run设置文本内容

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

    效果图

    设置缩进

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

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

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

    换行

    run.AddBreak()

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

    输入文本

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

    大写

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

    画线和画两条

    1. run = createParaRun(doc, "strike")
    2. run.Properties().SetStrikeThrough(true)
    3. run = createParaRun(doc, "double strike")
    4. run.Properties().SetDoubleStrikeThrough(true)

    其他

    1. run = createParaRun(doc, "outline")
    2. run.Properties().SetOutline(true)
    3. run = createParaRun(doc, "emboss")
    4. run.Properties().SetEmboss(true)
    5. run = createParaRun(doc, "shadow")
    6. run.Properties().SetShadow(true)
    7. run = createParaRun(doc, "imprint")
    8. run.Properties().SetImprint(true)
    9. run = createParaRun(doc, "highlighting")
    10. run.Properties().SetHighlight(wml.ST_HighlightColorYellow)
    11. run = createParaRun(doc, "underline")
    12. run.Properties().SetUnderline(wml.ST_UnderlineWavyDouble, color.Red)
    13. run = createParaRun(doc, "text effects")
    14. run.Properties().SetEffect(wml.ST_TextEffectAntsRed)
    15. //选择编号样式?
    16. nd := doc.Numbering.Definitions()[0]
    17. for i := 1; i < 5; i++ {
    18. p := doc.AddParagraph()
    19. //设置编号等级?
    20. p.SetNumberingLevel(i - 1)
    21. //设置编号样式
    22. p.SetNumberingDefinition(nd)
    23. run := p.AddRun()
    24. run.AddText(fmt.Sprintf("Level %d", i))
    25. }

    完整DEMO代码

    1. // Copyright 2017 FoxyUtils ehf. All rights reserved.
    2. package main
    3. //导包
    4. import (
    5. "fmt"
    6. "os"
    7. "github.com/unidoc/unioffice/color"
    8. "github.com/unidoc/unioffice/common/license"
    9. "github.com/unidoc/unioffice/document"
    10. "github.com/unidoc/unioffice/measurement"
    11. "github.com/unidoc/unioffice/schema/soo/wml"
    12. )
    13. //资本家的密钥
    14. func init() {
    15. // Make sure to load your metered License API key prior to using the library.
    16. // If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
    17. err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
    18. if err != nil {
    19. panic(err)
    20. }
    21. }
    22. func main() {
    23. //创建doc
    24. doc := document.New()
    25. //关闭doc
    26. defer doc.Close()
    27. para := doc.AddParagraph()
    28. run := para.AddRun()
    29. para.SetStyle("Title")
    30. run.AddText("Simple Document Formatting")
    31. para = doc.AddParagraph()
    32. para.SetStyle("Heading1")
    33. run = para.AddRun()
    34. run.AddText("Some Heading Text")
    35. para = doc.AddParagraph()
    36. para.SetStyle("Heading2")
    37. run = para.AddRun()
    38. run.AddText("Some Heading Text")
    39. para = doc.AddParagraph()
    40. para.SetStyle("Heading3")
    41. run = para.AddRun()
    42. run.AddText("Some Heading Text")
    43. para = doc.AddParagraph()
    44. para.Properties().SetFirstLineIndent(0.5 * measurement.Inch)
    45. run = para.AddRun()
    46. run.AddText("A run is a string of characters with the same formatting. ")
    47. run = para.AddRun()
    48. run.Properties().SetBold(true)
    49. run.Properties().SetFontFamily("Courier")
    50. run.Properties().SetSize(15)
    51. run.Properties().SetColor(color.Red)
    52. run.AddText("Multiple runs with different formatting can exist in the same paragraph. ")
    53. run = para.AddRun()
    54. run.AddText("Adding breaks to a run will insert line breaks after the run. ")
    55. run.AddBreak()
    56. run.AddBreak()
    57. run = createParaRun(doc, "Runs support styling options:")
    58. run = createParaRun(doc, "small caps")
    59. run.Properties().SetSmallCaps(true)
    60. run = createParaRun(doc, "strike")
    61. run.Properties().SetStrikeThrough(true)
    62. run = createParaRun(doc, "double strike")
    63. run.Properties().SetDoubleStrikeThrough(true)
    64. run = createParaRun(doc, "outline")
    65. run.Properties().SetOutline(true)
    66. run = createParaRun(doc, "emboss")
    67. run.Properties().SetEmboss(true)
    68. run = createParaRun(doc, "shadow")
    69. run.Properties().SetShadow(true)
    70. run = createParaRun(doc, "imprint")
    71. run.Properties().SetImprint(true)
    72. run = createParaRun(doc, "highlighting")
    73. run.Properties().SetHighlight(wml.ST_HighlightColorYellow)
    74. run = createParaRun(doc, "underline")
    75. run.Properties().SetUnderline(wml.ST_UnderlineWavyDouble, color.Red)
    76. run = createParaRun(doc, "text effects")
    77. run.Properties().SetEffect(wml.ST_TextEffectAntsRed)
    78. nd := doc.Numbering.Definitions()[0]
    79. for i := 1; i < 5; i++ {
    80. p := doc.AddParagraph()
    81. p.SetNumberingLevel(i - 1)
    82. p.SetNumberingDefinition(nd)
    83. run := p.AddRun()
    84. run.AddText(fmt.Sprintf("Level %d", i))
    85. }
    86. doc.SaveToFile("simple.docx")
    87. }
    88. func createParaRun(doc *document.Document, s string) document.Run {
    89. para := doc.AddParagraph()
    90. run := para.AddRun()
    91. run.AddText(s)
    92. return run
    93. }

  • 相关阅读:
    css 块级元素与内联元素
    谈一谈程序化交易接口的优势是什么?
    Mybatis懒加载
    pcan二次开发文档 | PEAK-System Documentation
    SAP UI5 SimpleForm 的 two-column layout 布局
    HZOJ-271: 滑动窗口
    web前端面试高频考点——Vue原理(diff算法、模板编译、组件渲染和更新、JS实现路由)
    【微服务|Sentinel】热点规则|授权规则|集群流控|机器列表
    会议通知和代开会议
    【Linux】线程互斥与同步
  • 原文地址:https://blog.csdn.net/ghc_kailei/article/details/133870006