-
Notifications
You must be signed in to change notification settings - Fork 101
/
pdf_draw_lines.go
360 lines (305 loc) · 14.8 KB
/
pdf_draw_lines.go
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* This example showcases the capabilities of creator lines.
*
* Run as: go run pdf_draw_lines.go
*/
package main
import (
"log"
"os"
"github.com/unidoc/unipdf/v3/common/license"
"github.com/unidoc/unipdf/v3/contentstream/draw"
"github.com/unidoc/unipdf/v3/creator"
)
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() {
c := creator.New()
// Create front page.
c.CreateFrontPage(func(args creator.FrontpageFunctionArgs) {
sp := c.NewStyledParagraph()
sp.SetTextAlignment(creator.TextAlignmentCenter)
sp.SetMargins(0, 0, 250, 0)
style := &sp.Append("Quick guide to drawing lines \nusing the creator package.").Style
style.FontSize = 24
if err := c.Draw(sp); err != nil {
log.Fatal(err)
}
})
// Create customized table of contents.
c.AddTOC = true
c.CreateTableOfContents(func(toc *creator.TOC) error {
// Set style of TOC heading just before render.
style := c.NewTextStyle()
style.FontSize = 18
toc.SetHeading("Table of Contents", style)
// Set style of TOC lines just before render.
lines := toc.Lines()
for _, line := range lines {
line.Page.Style.FontSize = 15
}
return nil
})
// Showcase lines using absolute positioning.
if err := drawLinesPositionAbsolute(c); err != nil {
log.Fatal(err)
}
// Showcase lines using relative positioning.
if err := drawLinesPositionRelative(c); err != nil {
log.Fatal(err)
}
// Showcase lines inside divisions.
if err := drawLinesInsideDivision(c); err != nil {
log.Fatal(err)
}
// Showcase lines inside tables.
if err := drawLinesInsideTable(c); err != nil {
log.Fatal(err)
}
// Write output file.
if err := c.WriteToFile("unipdf-draw-lines.pdf"); err != nil {
log.Fatal(err)
}
}
func drawLinesPositionAbsolute(c *creator.Creator) error {
c.NewPage()
// Draw chapter.
subtitleText := "In absolute positioning mode, the absolute coordinates of the lines must be specified by the user. Let's draw some lines with different styles."
chapter := c.NewChapter("Lines using absolute positioning")
subtitle := newParagraph(c, subtitleText, 15, creator.TextAlignmentJustify, newMargins(0, 0, 8, 15))
if err := chapter.Add(subtitle); err != nil {
return err
}
if err := c.Draw(chapter); err != nil {
return err
}
// Create page content.
contents := []creator.VectorDrawable{
newLine(c, 60, 150, 350, 150, true, false, 3, creator.ColorBlack, false, nil, 0, 1.0, newMargins(0, 0, 0, 0)),
newLine(c, 500, 150, 500, 350, true, false, 10, creator.ColorRed, false, nil, 0, 1.0, newMargins(0, 0, 0, 0)),
newLine(c, 60, 200, 400, 400, true, false, 2, creator.ColorBlue, true, []int64{6, 1}, 0, 1.0, newMargins(0, 0, 0, 0)),
newLine(c, 60, 450, 550, 450, true, false, 15, creator.ColorGreen, false, nil, 0, 0.5, newMargins(0, 0, 0, 0)),
newLine(c, 60, 650, 550, 480, true, false, 3, creator.ColorRed, false, nil, 0, 1.0, newMargins(0, 0, 0, 0)),
newLine(c, 60, 700, 550, 700, true, false, 10, creator.ColorBlack, true, []int64{10, 2}, 0, 0.3, newMargins(0, 0, 0, 0)),
}
// Draw page content.
for _, content := range contents {
if err := c.Draw(content); err != nil {
return err
}
}
return nil
}
func drawLinesPositionRelative(c *creator.Creator) error {
c.NewPage()
// Draw chapter.
subtitleText := "In relative positioning mode, lines are positioned relative to the current context. The specified coordinates are only used to determine the orientation and size of the line. Furthermore, relative lines can be made to fill the entire width available to them. They can also have margins."
chapter := c.NewChapter("Lines using relative positioning")
subtitle := newParagraph(c, subtitleText, 15, creator.TextAlignmentJustify, newMargins(0, 0, 8, 15))
if err := chapter.Add(subtitle); err != nil {
return err
}
if err := c.Draw(chapter); err != nil {
return err
}
// Create page content.
contents := []creator.VectorDrawable{
newParagraph(c, "The line is always positioned relative to the current context. First, the size and orientation of the lines are determined from the provided coordinates. Then, the line is translated at the current context position. They can have any orientation.", 12, creator.TextAlignmentJustify, newMargins(0, 0, 10, 5)),
newLine(c, 0, 0, 200, 0, false, false, 3, creator.ColorBlue, false, nil, 0, 1.0, newMargins(0, 0, 5, 5)),
newLine(c, 0, 0, 0, 50, false, false, 5, creator.ColorYellow, false, nil, 0, 1.0, newMargins(150, 0, 5, 5)),
newLine(c, 0, 0, 400, 30, false, false, 5, creator.ColorRed, false, nil, 0, 0.5, newMargins(0, 0, 5, 5)),
newLine(c, 0, 50, 200, 0, false, false, 2, creator.ColorGreen, true, []int64{7, 2}, 0, 1.0, newMargins(0, 0, 5, 10)),
newParagraph(c, "In full-width mode, the size of the line is calculated so that the line fills the entire available context width. The provided coordinates are only used to determine the orientation.", 12, creator.TextAlignmentJustify, newMargins(0, 0, 20, 5)),
newLine(c, 0, 0, 0, 0, false, true, 3, creator.ColorRed, false, nil, 0, 1.0, newMargins(0, 0, 5, 5)),
newLine(c, 0, 0, 0, 50, false, true, 5, creator.ColorBlue, true, []int64{10, 3}, 0, 1.0, newMargins(0, 0, 5, 5)),
newLine(c, 0, 50, 0, 0, false, true, 2, creator.ColorGreen, false, nil, 0, 0.5, newMargins(0, 0, 5, 5)),
newParagraph(c, "Full-width horizontal lines are very useful as content separators. For this use case, all coordinates can be 0. Let's draw a couple of them, with and without horizontal margins.", 12, creator.TextAlignmentJustify, newMargins(0, 0, 20, 5)),
newLine(c, 0, 0, 0, 0, false, true, 1, creator.ColorBlue, false, nil, 0, 1.0, newMargins(0, 0, 5, 10)),
newLine(c, 0, 0, 0, 0, false, true, 2, creator.ColorRed, true, []int64{5, 1}, 0, 1.0, newMargins(0, 0, 5, 5)),
newLine(c, 0, 0, 0, 0, false, true, 10, creator.ColorGreen, false, nil, 0, 0.5, newMargins(0, 0, 5, 5)),
newLine(c, 0, 0, 0, 0, false, true, 4, creator.ColorYellow, true, []int64{2, 4}, 0, 1.0, newMargins(0, 0, 5, 5)),
newLine(c, 0, 0, 0, 0, false, true, 3, creator.ColorRed, false, nil, 0, 1.0, newMargins(150, 150, 5, 5)),
newLine(c, 0, 0, 0, 0, false, true, 4, creator.ColorBlue, true, []int64{3}, 0, 1.0, newMargins(100, 100, 5, 5)),
newLine(c, 0, 0, 0, 0, false, true, 5, creator.ColorBlack, false, nil, 0, 1.0, newMargins(50, 50, 5, 5)),
}
// Draw page content.
for _, content := range contents {
if err := c.Draw(content); err != nil {
return err
}
}
return nil
}
func drawLinesInsideDivision(c *creator.Creator) error {
c.NewPage()
var (
lightGray = creator.ColorRGBFromHex("#f3f3f3")
darkGray = creator.ColorRGBFromHex("#555555")
sampleText = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged."
subtitleText = "All styles of lines are supported inside divisions. They can be useful as section separators or to emphasize parts of the content."
)
// Draw chapter.
chapter := c.NewChapter("Lines inside divisions")
subtitle := newParagraph(c, subtitleText, 15, creator.TextAlignmentJustify, newMargins(0, 0, 8, 15))
if err := chapter.Add(subtitle); err != nil {
return err
}
if err := c.Draw(chapter); err != nil {
return err
}
// Create division content.
contents := []creator.VectorDrawable{
newLine(c, 0, 0, 0, 0, false, true, 2, darkGray, false, nil, 0, 1.0, newMargins(125, 100, 10, 10)),
newParagraph(c, "Sample section title", 16, creator.TextAlignmentCenter, newMargins(50, 50, 0, 0)),
newLine(c, 0, 0, 0, 0, false, true, 2, darkGray, false, nil, 0, 1.0, newMargins(125, 100, 10, 20)),
newParagraph(c, sampleText, 12, creator.TextAlignmentJustify, newMargins(0, 0, 5, 5)),
newLine(c, 0, 0, 0, 0, false, true, 1, darkGray, false, nil, 0, 1.0, newMargins(0, 0, 5, 5)),
newParagraph(c, sampleText, 12, creator.TextAlignmentJustify, newMargins(0, 0, 5, 5)),
newLine(c, 0, 0, 0, 0, false, true, 1, darkGray, false, nil, 0, 1.0, newMargins(0, 0, 5, 5)),
newParagraph(c, sampleText, 12, creator.TextAlignmentJustify, newMargins(0, 0, 5, 5)),
newParagraph(c, sampleText, 12, creator.TextAlignmentJustify, newMargins(0, 0, 5, 5)),
newLine(c, 0, 0, 0, 0, false, true, 1, creator.ColorRed, true, []int64{3}, 0, 1.0, newMargins(0, 0, 5, 5)),
newParagraph(c, sampleText, 12, creator.TextAlignmentJustify, newMargins(0, 0, 5, 5)),
newParagraph(c, sampleText, 12, creator.TextAlignmentJustify, newMargins(0, 0, 5, 5)),
newLine(c, 0, 0, 0, 0, false, true, 3, creator.ColorBlue, false, nil, 0, 1.0, newMargins(25, 25, 10, 10)),
}
// Create division.
div := c.NewDivision()
div.SetPadding(10, 10, 10, 10)
div.SetBackground(&creator.Background{
FillColor: lightGray,
BorderRadiusTopLeft: 5,
BorderRadiusTopRight: 5,
BorderRadiusBottomLeft: 5,
BorderRadiusBottomRight: 5,
})
// Draw division content.
for _, content := range contents {
if err := div.Add(content); err != nil {
return err
}
}
// Draw division.
return c.Draw(div)
}
func drawLinesInsideTable(c *creator.Creator) error {
c.NewPage()
var (
subtitleText = "Lines of all styles can be rendered inside table cells. Here is a quick reference of the customizable line attributes."
blue = creator.ColorBlue
yellow = creator.ColorYellow
red = creator.ColorRed
black = creator.ColorBlack
)
// Draw chapter.
chapter := c.NewChapter("Lines inside tables")
subtitle := newParagraph(c, subtitleText, 15, creator.TextAlignmentJustify, newMargins(0, 0, 8, 15))
if err := chapter.Add(subtitle); err != nil {
return err
}
if err := c.Draw(chapter); err != nil {
return err
}
// Create table content.
contents := []creator.VectorDrawable{
// Solid lines.
newParagraph(c, "Solid lines", 10, creator.TextAlignmentLeft, newMargins(5, 0, 0, 0)),
newLine(c, 0, 0, 0, 0, false, true, 1, blue, false, nil, 0, 1.0, newMargins(5, 5, 5, 5)),
newLine(c, 0, 0, 0, 0, false, true, 3, yellow, false, nil, 0, 1.0, newMargins(5, 5, 5, 5)),
newLine(c, 0, 0, 0, 0, false, true, 5, red, false, nil, 0, 1.0, newMargins(5, 5, 5, 5)),
newLine(c, 0, 0, 0, 0, false, true, 10, black, false, nil, 0, 1.0, newMargins(5, 5, 5, 5)),
// Dashed lines.
newParagraph(c, "Dashed lines", 10, creator.TextAlignmentLeft, newMargins(5, 0, 0, 0)),
newLine(c, 0, 0, 0, 0, false, true, 1, blue, true, []int64{3}, 0, 1.0, newMargins(5, 5, 5, 5)),
newLine(c, 0, 0, 0, 0, false, true, 3, yellow, true, []int64{3}, 0, 1.0, newMargins(5, 5, 5, 5)),
newLine(c, 0, 0, 0, 0, false, true, 5, red, true, []int64{3}, 0, 1.0, newMargins(5, 5, 5, 5)),
newLine(c, 0, 0, 0, 0, false, true, 10, black, true, []int64{3}, 0, 1.0, newMargins(5, 5, 5, 5)),
// Transparent solid lines.
newParagraph(c, "Transparent solid lines", 10, creator.TextAlignmentLeft, newMargins(5, 0, 0, 0)),
newLine(c, 0, 0, 0, 0, false, true, 1, blue, false, nil, 0, 0.5, newMargins(5, 5, 5, 5)),
newLine(c, 0, 0, 0, 0, false, true, 3, yellow, false, nil, 0, 0.5, newMargins(5, 5, 5, 5)),
newLine(c, 0, 0, 0, 0, false, true, 5, red, false, nil, 0, 0.5, newMargins(5, 5, 5, 5)),
newLine(c, 0, 0, 0, 0, false, true, 10, black, false, nil, 0, 0.5, newMargins(5, 5, 5, 5)),
// Transparent dashed lines.
newParagraph(c, "Transparent dashed lines", 9, creator.TextAlignmentLeft, newMargins(5, 0, 0, 0)),
newLine(c, 0, 0, 0, 0, false, true, 1, blue, true, []int64{6, 3}, 0, 0.5, newMargins(5, 5, 5, 5)),
newLine(c, 0, 0, 0, 0, false, true, 3, yellow, true, []int64{6, 3}, 0, 0.5, newMargins(5, 5, 5, 5)),
newLine(c, 0, 0, 0, 0, false, true, 5, red, true, []int64{6, 3}, 0, 0.5, newMargins(5, 5, 5, 5)),
newLine(c, 0, 0, 0, 0, false, true, 10, black, true, []int64{6, 3}, 0, 0.5, newMargins(5, 5, 5, 5)),
// Solid lines with horizontal margins.
newParagraph(c, "Solid lines - horizontal margins", 10, creator.TextAlignmentLeft, newMargins(5, 0, 0, 0)),
newLine(c, 0, 0, 0, 0, false, true, 1, blue, false, nil, 0, 1.0, newMargins(20, 20, 5, 5)),
newLine(c, 0, 0, 0, 0, false, true, 3, yellow, false, nil, 0, 1.0, newMargins(20, 20, 5, 5)),
newLine(c, 0, 0, 0, 0, false, true, 5, red, false, nil, 0, 1.0, newMargins(20, 20, 5, 5)),
newLine(c, 0, 0, 0, 0, false, true, 10, black, false, nil, 0, 1.0, newMargins(20, 20, 5, 5)),
// Solid lines with vertical margins.
newParagraph(c, "Solid lines - vertical margins", 10, creator.TextAlignmentLeft, newMargins(5, 0, 0, 0)),
newLine(c, 0, 0, 0, 0, false, true, 1, blue, false, nil, 0, 1.0, newMargins(5, 5, 10, 10)),
newLine(c, 0, 0, 0, 0, false, true, 3, yellow, false, nil, 0, 1.0, newMargins(5, 5, 10, 10)),
newLine(c, 0, 0, 0, 0, false, true, 5, red, false, nil, 0, 1.0, newMargins(5, 5, 10, 10)),
newLine(c, 0, 0, 0, 0, false, true, 10, black, false, nil, 0, 1.0, newMargins(5, 5, 10, 10)),
}
// Draw table content.
table := c.NewTable(5)
if err := table.SetColumnWidths(0.3, 0.175, 0.175, 0.175, 0.175); err != nil {
return err
}
for _, content := range contents {
cell := table.NewCell()
cell.SetIndent(0)
cell.SetBorder(creator.CellBorderSideAll, creator.CellBorderStyleSingle, 1.0)
cell.SetVerticalAlignment(creator.CellVerticalAlignmentMiddle)
if err := cell.SetContent(content); err != nil {
return err
}
}
// Draw table.
return c.Draw(table)
}
func newParagraph(c *creator.Creator, title string, fontSize float64, textAlign creator.TextAlignment, margins creator.Margins) *creator.StyledParagraph {
sp := c.NewStyledParagraph()
sp.SetMargins(margins.Left, margins.Right, margins.Top, margins.Bottom)
sp.Append(title).Style.FontSize = fontSize
sp.SetTextAlignment(textAlign)
sp.SetTextVerticalAlignment(creator.TextVerticalAlignmentCenter)
return sp
}
func newLine(c *creator.Creator, x1, y1, x2, y2 float64, isAbsolute, fillWidth bool, lineWidth float64,
color creator.Color, isDashed bool, dashArray []int64, dashPhase int64, opacity float64, margins creator.Margins) *creator.Line {
positioning := creator.PositionRelative
if isAbsolute {
positioning = creator.PositionAbsolute
}
fitMode := creator.FitModeNone
if fillWidth {
fitMode = creator.FitModeFillWidth
}
style := draw.LineStyleSolid
if isDashed {
style = draw.LineStyleDashed
}
line := c.NewLine(x1, y1, x2, y2)
line.SetLineWidth(lineWidth)
line.SetMargins(margins.Left, margins.Right, margins.Top, margins.Bottom)
line.SetPositioning(positioning)
line.SetFitMode(fitMode)
line.SetColor(color)
line.SetStyle(style)
line.SetDashPattern(dashArray, dashPhase)
line.SetOpacity(opacity)
return line
}
func newMargins(left, right, top, bottom float64) creator.Margins {
return creator.Margins{
Left: left,
Right: right,
Top: top,
Bottom: bottom,
}
}