package wps import ( "io/ioutil" "log" "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().SetAlignment(wml.ST_JcCenter) run := para.AddRun() run.Properties().SetBold(true) run.Properties().SetFontFamily("等线 (正文)") run.Properties().SetSize(22) run.AddText("安全运维日报") run.AddBreak() //正文 para = doc.AddParagraph() para.Properties().SetFirstLineIndent(0.5 * measurement.Inch) //控制段落中第一行的缩进 run = para.AddRun() run.Properties().SetFontFamily("等线 (正文)") run.Properties().SetSize(16) //设置字体大小 run.AddText(`2025年在政府信息化中心领导的指导、要求下,我科室和运维公司将严格加强网络安全巡检,每日做好巡检工作。 2025年2月20日对大同信创云平台互联网防火墙(*.*.68.146)、互联网流量清洗(*.*.65.19)、互联网态势感知(*.*.65.21)、互联网堡垒机(*.*.65.23)、政务外网防火墙(*.*.68.145)政务外网态势感知(*.*.65.20)、政务外网堡垒机(*.*.65.22)进行安全巡检,未发现安全入侵事件,安全设备运行正常。检测到攻击事件如下:`) //段落 para = doc.AddParagraph() para.Properties().SetFirstLineIndent(0.5 * measurement.Inch) //控制段落中第一行的缩进 run = para.AddRun() run.Properties().SetFontFamily("等线 (正文)") run.Properties().SetSize(16) run.AddText(`121.30.199.80:80、121.30.199.79:80服务受到wget 命令注入(HTTP_URI)攻击攻击,已对攻击源:91.224.92.10进行封锁;`) para = doc.AddParagraph() para.Properties().SetFirstLineIndent(0.5 * measurement.Inch) //控制段落中第一行的缩进 run = para.AddRun() run.Properties().SetFontFamily("等线 (正文)") run.Properties().SetSize(16) run.AddText("121.30.199.82:80服务受到wget 命令注入(HTTP_URI)、Trojan.Win.Njrat变体出站连接、恶意软件流量_AndroxGh0st、访问敏感文件.git_config通信流量攻击攻击,已对攻击源:66.240.205.34、45.125.66.249、195.178.110.163进行封锁;") run.AddBreak() run.AddText("如下为安全巡检详情:") run.AddPageBreak() para = doc.AddParagraph() para.Properties().SetAlignment(wml.ST_JcCenter) run = para.AddRun() run.Properties().SetBold(true) run.Properties().SetFontFamily("等线 (正文)") run.Properties().SetSize(22) run.AddText("大同信创云平台安全运维日报") run.AddBreak() table := doc.AddTable() row := table.AddRow() // 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") }