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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright 2017 FoxyUtils ehf. All rights reserved.
package main

import (
	"fmt"
	"log"
	"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 := document.New()
	defer doc.Close()

	// First Table
	{
		table := doc.AddTable()
		// width of the page
		table.Properties().SetWidthPercent(100)
		// with thick borers
		borders := table.Properties().Borders()
		borders.SetAll(wml.ST_BorderSingle, color.Auto, 2*measurement.Point)

		row := table.AddRow()
		run := row.AddCell().AddParagraph().AddRun()
		run.AddText("Name")
		run.Properties().SetHighlight(wml.ST_HighlightColorYellow)
		row.AddCell().AddParagraph().AddRun().AddText("John Smith")
		row = table.AddRow()
		row.AddCell().AddParagraph().AddRun().AddText("Street Address")
		row.AddCell().AddParagraph().AddRun().AddText("111 Country Road")
	}

	doc.AddParagraph() // break up the consecutive tables

	// Second Table
	{
		table := doc.AddTable()
		// 4 inches wide
		table.Properties().SetWidth(4 * measurement.Inch)
		borders := table.Properties().Borders()
		// thin borders
		borders.SetAll(wml.ST_BorderSingle, color.Auto, measurement.Zero)

		row := table.AddRow()
		cell := row.AddCell()
		// column span / merged cells
		cell.Properties().SetColumnSpan(2)

		run := cell.AddParagraph().AddRun()
		run.AddText("Cells can span multiple columns")

		row = table.AddRow()
		cell = row.AddCell()
		cell.Properties().SetVerticalMerge(wml.ST_MergeRestart)
		cell.AddParagraph().AddRun().AddText("Vertical Merge")
		row.AddCell().AddParagraph().AddRun().AddText("")

		row = table.AddRow()
		cell = row.AddCell()
		cell.Properties().SetVerticalMerge(wml.ST_MergeContinue)
		cell.AddParagraph().AddRun().AddText("Vertical Merge 2")
		row.AddCell().AddParagraph().AddRun().AddText("")

		row = table.AddRow()
		row.AddCell().AddParagraph().AddRun().AddText("Street Address")
		row.AddCell().AddParagraph().AddRun().AddText("111 Country Road")
	}

	doc.AddParagraph()

	// Third Table
	{
		table := doc.AddTable()
		table.Properties().SetWidthPercent(100)
		borders := table.Properties().Borders()
		borders.SetAll(wml.ST_BorderSingle, color.Auto, 1*measurement.Point)

		hdrRow := table.AddRow()

		cell := hdrRow.AddCell()
		cell.Properties().SetShading(wml.ST_ShdSolid, color.LightGray, color.Auto)
		cellPara := cell.AddParagraph()
		cellPara.Properties().SetAlignment(wml.ST_JcLeft)
		cellPara.AddRun().AddText("Left Align")

		cell = hdrRow.AddCell()
		cell.Properties().SetShading(wml.ST_ShdThinDiagStripe, color.Red, color.LightGray)
		cellPara = cell.AddParagraph()
		cellPara.Properties().SetAlignment(wml.ST_JcCenter)
		cellPara.AddRun().AddText("Center Align")

		cell = hdrRow.AddCell()
		cell.Properties().SetShading(wml.ST_ShdPct20, color.Red, color.LightGray)
		cellPara = cell.AddParagraph()
		cellPara.Properties().SetAlignment(wml.ST_JcRight)
		cellPara.AddRun().AddText("Right Align")

		veryLightGray := color.RGB(240, 240, 240)
		for i := 0; i < 5; i++ {
			row := table.AddRow()
			for j := 0; j < 3; j++ {
				cell := row.AddCell()
				// shade every other row
				if i%2 == 0 {
					cell.Properties().SetShading(wml.ST_ShdSolid, veryLightGray, color.Auto)
				}
				cell.AddParagraph().AddRun()
			}
		}
	}

	doc.AddParagraph()
	// Fourth Table
	{
		table := doc.AddTable()
		table.Properties().SetWidthPercent(50)
		table.Properties().SetAlignment(wml.ST_JcTableCenter)
		borders := table.Properties().Borders()
		borders.SetAll(wml.ST_BorderSingle, color.Auto, 1*measurement.Point)

		row := table.AddRow()
		row.Properties().SetHeight(2*measurement.Inch, wml.ST_HeightRuleExact)

		cell := row.AddCell()
		cell.Properties().SetVerticalAlignment(wml.ST_VerticalJcCenter)

		para := cell.AddParagraph()
		para.Properties().SetAlignment(wml.ST_JcCenter)
		run := para.AddRun()
		run.AddText("hello world")
	}

	doc.AddParagraph()
	// Fifth Table
	{
		table := doc.AddTable()
		table.Properties().SetWidthPercent(90)
		table.Properties().SetAlignment(wml.ST_JcTableCenter)
		borders := table.Properties().Borders()
		borders.SetAll(wml.ST_BorderSingle, color.Auto, 1*measurement.Point)

		row := table.AddRow()

		cell := row.AddCell()
		cell.Properties().SetWidthPercent(25)
		para := cell.AddParagraph()
		run := para.AddRun()
		run.AddText("hello")

		cell = row.AddCell()
		cell.Properties().SetWidthPercent(75)
		para = cell.AddParagraph()
		run = para.AddRun()
		run.AddText("world")

		// start a new table
		doc.AddParagraph()
		table = doc.AddTable()
		table.Properties().SetWidthPercent(90)
		table.Properties().SetAlignment(wml.ST_JcTableCenter)
		borders = table.Properties().Borders()
		borders.SetAll(wml.ST_BorderSingle, color.Auto, 1*measurement.Point)

		row = table.AddRow()

		cell = row.AddCell()
		cell.Properties().SetWidth(0.25 * measurement.Inch)
		para = cell.AddParagraph()
		run = para.AddRun()
		run.AddText("hello")

		cell = row.AddCell()
		cell.Properties().SetWidth(2.5 * measurement.Inch)
		para = cell.AddParagraph()
		run = para.AddRun()
		run.AddText("world")

	}
	doc.AddParagraph()
	// Seventh Table - Styled
	{
		// construct a table style
		ts := doc.Styles.AddStyle("MyTableStyle", wml.ST_StyleTypeTable, false)
		tp := ts.TableProperties()
		tp.SetRowBandSize(1)
		tp.SetColumnBandSize(1)
		tp.SetTableIndent(measurement.Zero)

		// horizomntal banding
		s := ts.TableConditionalFormatting(wml.ST_TblStyleOverrideTypeBand1Horz)
		s.CellProperties().SetShading(wml.ST_ShdSolid, color.LightBlue, color.Red)

		// first row bold
		s = ts.TableConditionalFormatting(wml.ST_TblStyleOverrideTypeFirstRow)
		s.RunProperties().SetBold(true)

		// last row bold
		s = ts.TableConditionalFormatting(wml.ST_TblStyleOverrideTypeLastRow)
		s.RunProperties().SetBold(true)
		cb := s.CellProperties().Borders()
		cb.SetTop(wml.ST_BorderDouble, color.Black, 0.5*measurement.Point)

		tp.Borders().SetAll(wml.ST_BorderSingle, color.Blue, 0.5*measurement.Point)

		table := doc.AddTable()

		table.Properties().SetLayout(wml.ST_TblLayoutTypeFixed)
		table.Properties().SetWidthPercent(90)
		table.Properties().SetStyle("MyTableStyle")
		look := table.Properties().TableLook()
		look.SetFirstColumn(true)
		look.SetFirstRow(true)
		look.SetHorizontalBanding(true)

		for r := 0; r < 5; r++ {
			row := table.AddRow()
			for c := 0; c < 5; c++ {
				cell := row.AddCell()
				cell.AddParagraph().AddRun().AddText(fmt.Sprintf("row %d col %d", r+1, c+1))
			}
		}
	}

	// Sixth Table - Insert
	{
		doc.InsertParagraphBefore(doc.Paragraphs()[5])
		table := doc.InsertTableBefore(doc.Paragraphs()[5])
		table.Properties().SetWidthPercent(90)
		table.Properties().SetAlignment(wml.ST_JcTableCenter)
		borders := table.Properties().Borders()
		borders.SetAll(wml.ST_BorderSingle, color.Auto, 1*measurement.Point)

		row := table.AddRow()

		cell := row.AddCell()
		cell.Properties().SetWidth(0.25 * measurement.Inch)
		para := cell.AddParagraph()
		run := para.AddRun()
		run.AddText("Insert")
		cell = row.AddCell()
		cell.Properties().SetWidth(0.25 * measurement.Inch)
		para = cell.AddParagraph()
		run = para.AddRun()
		run.AddText("new")
		cell = row.AddCell()
		cell.Properties().SetWidth(2.75 * measurement.Inch)
		para = cell.AddParagraph()
		run = para.AddRun()
		run.AddText("table")
	}
	if err := doc.Validate(); err != nil {
		log.Fatalf("error during validation: %s", err)
	}
	doc.SaveToFile("tables.docx")

}