dt_automate/wps/damo.go

113 lines
3.0 KiB
Go
Raw Normal View History

2025-02-25 18:36:33 +08:00
// // Copyright 2017 FoxyUtils ehf. All rights reserved.
package wps
// import (
// "fmt"
// "log"
// "time"
// "github.com/unidoc/unioffice/v2/common/license"
// "github.com/unidoc/unioffice/v2/document"
// )
// 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 Damo() {
// doc, err := document.Open("document.docx")
// if err != nil {
// log.Fatalf("error opening document: %s", err)
// }
// defer doc.Close()
// paragraphs := []document.Paragraph{}
// for _, p := range doc.Paragraphs() {
// paragraphs = append(paragraphs, p)
// }
// // This sample document uses structured document tags, which are not common
// // except for in document templates. Normally you can just iterate over the
// // document's paragraphs.
// for _, sdt := range doc.StructuredDocumentTags() {
// for _, p := range sdt.Paragraphs() {
// paragraphs = append(paragraphs, p)
// }
// }
// for _, p := range paragraphs {
// for _, r := range p.Runs() {
// switch r.Text() {
// case "FIRST NAME":
// // ClearContent clears both text and line breaks within a run,
// // so we need to add the line break back
// r.ClearContent()
// r.AddText("John ")
// r.AddBreak()
// para := doc.InsertParagraphBefore(p)
// para.AddRun().AddText("Mr.")
// para.SetStyle("Name") // Name is a default style in this template file
// para = doc.InsertParagraphAfter(p)
// para.AddRun().AddText("III")
// para.SetStyle("Name")
// case "LAST NAME":
// r.ClearContent()
// r.AddText("Smith")
// case "Address | Phone | Email":
// r.ClearContent()
// r.AddText("111 Rustic Rd | 123-456-7890 | jsmith@smith.com")
// case "Date":
// r.ClearContent()
// r.AddText(time.Now().Format("Jan 2, 2006"))
// case "Recipient Name":
// r.ClearContent()
// r.AddText("Mrs. Smith")
// r.AddBreak()
// case "Title":
// // we remove the title content entirely
// p.RemoveRun(r)
// case "Company":
// r.ClearContent()
// r.AddText("Smith Enterprises")
// r.AddBreak()
// case "Address":
// r.ClearContent()
// r.AddText("112 Rustic Rd")
// r.AddBreak()
// case "City, ST ZIP Code":
// r.ClearContent()
// r.AddText("San Francisco, CA 94016")
// r.AddBreak()
// case "Dear Recipient:":
// r.ClearContent()
// r.AddText("Dear Mrs. Smith:")
// r.AddBreak()
// case "Your Name":
// r.ClearContent()
// r.AddText("John Smith")
// r.AddBreak()
// run := p.InsertRunBefore(r)
// run.AddText("---Before----")
// run.AddBreak()
// run = p.InsertRunAfter(r)
// run.AddText("---After----")
// default:
// fmt.Println("not modifying", r.Text())
// }
// }
// }
// doc.SaveToFile("edit-document.docx")
// }