毫无疑问GO的生态就是一坨大便。老子英文水平小学啊。
实现简单文本格式 文本字体颜色、大小、突出显示等。
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))
}
// 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.io
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
if err != nil {
panic(err)
}
}
func main() {
//创建doc
doc := document.New()
//关闭doc
defer 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
}