main.go hosted by Oembed Proxy for GitHub
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2017 FoxyUtils ehf. All rights reserved.

package main

import (
	"fmt"
	"log"
	"os"
	"time"

	"github.com/unidoc/unioffice/common/license"
	"github.com/unidoc/unioffice/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(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
	if err != nil {
		panic(err)
	}
}

func main() {
	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")
}