dt_automate/wps/word.go
2025-02-19 18:30:19 +08:00

182 lines
5.8 KiB
Go

package wps
import (
"fmt"
"io/ioutil"
"log"
"github.com/unidoc/unioffice/v2/color"
"github.com/unidoc/unioffice/v2/common"
"github.com/unidoc/unioffice/v2/common/license"
"github.com/unidoc/unioffice/v2/document"
"github.com/unidoc/unioffice/v2/measurement"
"github.com/unidoc/unioffice/v2/schema/soo/wml"
)
// 设置unidoc key
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(`4d568da2fb25ed2477068464ffde96b7e7fa091595e6af3610eb4aee1b539a24`)
if err != nil {
panic(err)
}
}
func Word() {
// err := license.SetMeteredKey("4d568da2fb25ed2477068464ffde96b7e7fa091595e6af3610eb4aee1b539a24")
// if err != nil {
// panic(err)
// }
doc := document.New()
defer doc.Close()
para := doc.AddParagraph() //添加段落
run := para.AddRun() //添加运行
para.SetStyle("Title") //标题
run.AddText("Simple Document Formatting") //添加文本
para = doc.AddParagraph()
para.SetStyle("Heading1") //标题1
run = para.AddRun()
run.AddText("Some Heading Text")
para = doc.AddParagraph()
para.SetStyle("Heading2") //标题2
run = para.AddRun()
run.AddText("Some Heading Text")
para = doc.AddParagraph()
para.SetStyle("Heading3") //标题3
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
}
var lorem = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin lobortis, lectus dictum feugiat tempus, sem neque finibus enim, sed eleifend sem nunc ac diam. Vestibulum tempus sagittis elementum`
func Word_img() {
doc := document.New()
img1, err := common.ImageFromFile("gophercolor.png")
if err != nil {
log.Fatalf("unable to create image: %s", err)
}
img2data, err := ioutil.ReadFile("gophercolor.png")
if err != nil {
log.Fatalf("unable to read file: %s", err)
}
img2, err := common.ImageFromBytes(img2data)
if err != nil {
log.Fatalf("unable to create image: %s", err)
}
img1ref, err := doc.AddImage(img1)
if err != nil {
log.Fatalf("unable to add image to document: %s", err)
}
img2ref, err := doc.AddImage(img2)
if err != nil {
log.Fatalf("unable to add image to document: %s", err)
}
para := doc.AddParagraph()
anchored, err := para.AddRun().AddDrawingAnchored(img1ref) //添加绘图锚定
if err != nil {
log.Fatalf("unable to add anchored image: %s", err)
}
anchored.SetName("Gopher")
anchored.SetSize(2*measurement.Inch, 2*measurement.Inch)
anchored.SetOrigin(wml.WdST_RelFromHPage, wml.WdST_RelFromVTopMargin) //设置原点
anchored.SetHAlignment(wml.WdST_AlignHCenter) //设置校准
anchored.SetYOffset(3 * measurement.Inch) //设置偏移量
anchored.SetTextWrapSquare(wml.WdST_WrapTextBothSides) //设置文本环绕正方形
run := para.AddRun()
for i := 0; i < 16; i++ {
run.AddText(lorem)
// drop an inline image in
if i == 13 {
inl, err := run.AddDrawingInline(img1ref)
if err != nil {
log.Fatalf("unable to add inline image: %s", err)
}
inl.SetSize(1*measurement.Inch, 1*measurement.Inch)
}
if i == 15 {
inl, err := run.AddDrawingInline(img2ref)
if err != nil {
log.Fatalf("unable to add inline image: %s", err)
}
inl.SetSize(1*measurement.Inch, 1*measurement.Inch)
}
}
doc.SaveToFile("image.docx")
}