-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.ts
More file actions
409 lines (358 loc) · 12.2 KB
/
Copy pathtable.ts
File metadata and controls
409 lines (358 loc) · 12.2 KB
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import { RichTextDocumentBuilder, pt, mm, inch } from "../../lib"
const builder = new RichTextDocumentBuilder()
// Setup colors for the demo
builder.withColor("white", { red: 255, green: 255, blue: 255 })
builder.withColor("lightGray", { red: 220, green: 220, blue: 220 })
builder.withColor("mediumGray", { red: 128, green: 128, blue: 128 })
builder.withColor("darkGray", { red: 64, green: 64, blue: 64 })
builder.withColor("darkBlue", { red: 0, green: 0, blue: 139 })
builder.withColor("green", { red: 0, green: 128, blue: 0 })
builder.withColor("red", { red: 255, green: 0, blue: 0 })
builder.withColor("yellow", { red: 255, green: 255, blue: 0 })
// Add fonts
builder.withFont("arial", { name: "Arial", family: "swiss" })
builder.withFont("courier", { name: "Courier New", family: "modern" })
// Create a new section
builder.withSection((section) => {
// Title
section.body.withTitle("RTF Table Features Demo")
// Section 1: Basic Table
section.body.withHeading("1. Basic Table", 1)
section.body.withText("A simple table with headers and data:").closeParagraph()
section.body.withTable((table) => {
table.withHeaderRow((row) => {
row.withCell((cell) => cell.withText({ bold: true }, "Name"))
row.withCell((cell) => cell.withText({ bold: true }, "Age"))
row.withCell((cell) => cell.withText({ bold: true }, "City"))
})
table.withRow((row) => {
row.withCell((cell) => cell.withText("Alice"))
row.withCell((cell) => cell.withText("30"))
row.withCell((cell) => cell.withText("New York"))
})
table.withRow((row) => {
row.withCell((cell) => cell.withText("Bob"))
row.withCell((cell) => cell.withText("25"))
row.withCell((cell) => cell.withText("Los Angeles"))
})
})
// Section 2: Table with Borders
section.body.withHeading("2. Table with Borders", 1)
section.body.withText("Tables can have various border styles:").closeParagraph()
section.body.withTable((table) => {
table.border("all", { width: pt(1) })
table.cellPadding({ top: pt(5), right: pt(5), bottom: pt(5), left: pt(5) })
table.column(2, undefined, 2)
table.withHeaderRow((row) => {
row.border("bottom", { width: pt(2), colorAlias: "darkBlue" })
row.withCell((cell) => cell.withText({ bold: true }, "Product"))
row.withCell((cell) => cell.withText({ bold: true }, "Price"))
row.withCell((cell) => cell.withText({ bold: true }, "Stock"))
})
table.withRow((row) => {
row.withCell((cell) => cell.withText("Laptop"))
row.withCell((cell) => cell.withText("$999"))
row.withCell((cell) => cell.withText("15"))
})
table.withRow((row) => {
row.withCell((cell) => cell.withText("Mouse"))
row.withCell((cell) => cell.withText("$29"))
row.withCell((cell) => cell.withText("50"))
})
})
// Section 3: Cell Borders and Shading
section.body.withHeading("3. Cell Borders and Shading", 1)
section.body.withText("Individual cells can have custom borders and shading:").closeParagraph()
section.body.withTable((table) => {
table.cellPadding({ top: pt(8), right: pt(8), bottom: pt(8), left: pt(8) })
table.withRow((row) => {
row.withCell((cell) => {
cell.border("all", { width: pt(2), colorAlias: "red", style: "dotted" })
cell.withText("Dotted Red Border")
})
row.withCell((cell) => {
cell.border("all", { width: pt(1), colorAlias: "green", style: "dashed" })
cell.backgroundColor("lightGray")
cell.withText("Dashed Green + Shading")
})
row.withCell((cell) => {
cell.border("all", { width: pt(3), colorAlias: "darkBlue" })
cell.withText("Double Blue Border")
})
})
table.withRow((row) => {
row.withCell((cell) => {
cell.backgroundColor("mediumGray")
cell.withText("50% Gray Shading")
})
row.withCell((cell) => {
cell.backgroundColor("darkGray")
cell.withText("75% Gray Shading")
})
row.withCell((cell) => {
cell.backgroundColor("yellow")
cell.withText("Yellow Background")
})
})
})
// Section 4: Cell Alignment
section.body.withHeading("4. Cell Alignment", 1)
section.body.withText("Cells can have different horizontal alignments:").closeParagraph()
section.body.withTable((table) => {
table.border("all", { width: pt(1) })
table.cellPadding({ top: pt(10), right: pt(10), bottom: pt(10), left: pt(10) })
table.withRow((row) => {
row.withCell((cell) => {
cell.newParagraph().left().withText("Left Aligned")
})
row.withCell((cell) => {
cell.newParagraph().center().withText("Center Aligned")
})
row.withCell((cell) => {
cell.newParagraph().right().withText("Right Aligned")
})
})
})
section.body.withText("Cells can have different vertical alignments:").closeParagraph()
section.body.withTable((table) => {
table.border("all", { width: pt(1) })
table.cellPadding({ top: pt(10), right: pt(10), bottom: pt(10), left: pt(10) })
table.withRow((row) => {
row.height(mm(20)) // Set row height to 20mm
row.withCell((cell) => {
cell.valign("top")
cell.withText("Top Aligned")
})
row.withCell((cell) => {
cell.valign("center")
cell.withText("Center Aligned")
})
row.withCell((cell) => {
cell.valign("bottom")
cell.withText("Bottom Aligned")
})
})
})
// Section 5: Cell Merging
section.body.withHeading("5. Cell Merging", 1)
section.body.withText("Cells can be merged horizontally and vertically:").closeParagraph()
section.body.withTable((table) => {
table.border("all", { width: pt(1) })
table.cellPadding({ top: pt(5), right: pt(5), bottom: pt(5), left: pt(5) })
// Row 1: Horizontal merge
table.withRow((row) => {
row.withCell((cell) => {
cell.hspan("first")
cell.withText({ bold: true }, "Merged Horizontally")
})
row.withCell((cell) => {
cell.hspan("next")
})
row.withCell((cell) => {
cell.withText("Regular Cell")
})
})
// Row 2: Start vertical merge
table.withRow((row) => {
row.withCell((cell) => {
cell.vspan("first")
cell.withText("Merged Vertically")
})
row.withCell((cell) => {
cell.withText("Cell 2-2")
})
row.withCell((cell) => {
cell.withText("Cell 2-3")
})
})
// Row 3: Continue vertical merge
table.withRow((row) => {
row.withCell((cell) => {
cell.vspan("next")
})
row.withCell((cell) => {
cell.withText("Cell 3-2")
})
row.withCell((cell) => {
cell.withText("Cell 3-3")
})
})
})
// Section 6: Complex Table Layout
section.body.withHeading("6. Complex Table Layout", 1)
section.body.withText("A complex table combining multiple features:").closeParagraph()
section.body.withTable((table) => {
table.border("all", { width: pt(1), colorAlias: "darkGray" })
table.cellPadding({ top: pt(8), right: pt(8), bottom: pt(8), left: pt(8) })
// Header row with merged cells
table.withHeaderRow((row) => {
row.border("bottom", { width: pt(2), colorAlias: "darkBlue" })
row.height(pt(30))
row.withCell((cell) => {
cell.hspan("first")
cell.valign("center")
cell.backgroundColor("mediumGray")
cell.withText({ bold: true, fontSize: pt(14) }, "Sales Report Q4 2024")
})
row.withCell((cell) => {
cell.hspan("next")
})
row.withCell((cell) => {
cell.hspan("next")
})
row.withCell((cell) => {
cell.hspan("next")
})
})
// Sub-header row
table.withRow((row) => {
row.border("bottom", { width: pt(1) })
row.withCell((cell) => {
cell.withText({ bold: true }, "Region")
})
row.withCell((cell) => {
cell.withText({ bold: true }, "Product")
})
row.withCell((cell) => {
cell.withText({ bold: true }, "Units Sold")
})
row.withCell((cell) => {
cell.withText({ bold: true }, "Revenue")
})
})
// Data rows with alternating shading
table.withRow((row) => {
row.backgroundColor("lightGray")
row.withCell((cell) => {
cell.vspan("first")
cell.backgroundColor("white")
cell.withText({ bold: true }, "North America")
})
row.withCell((cell) => {
cell.withText("Laptops")
})
row.withCell((cell) => {
cell.withText("1,250")
})
row.withCell((cell) => {
cell.withText({ colorAlias: "green" }, "$1,248,750")
})
})
table.withRow((row) => {
row.withCell((cell) => {
cell.vspan("next")
})
row.withCell((cell) => {
cell.withText("Tablets")
})
row.withCell((cell) => {
cell.withText("850")
})
row.withCell((cell) => {
cell.withText({ colorAlias: "green" }, "$425,000")
})
})
table.withRow((row) => {
row.backgroundColor("lightGray")
row.withCell((cell) => {
cell.vspan("first")
cell.backgroundColor("white")
cell.withText({ bold: true }, "Europe")
})
row.withCell((cell) => {
cell.withText("Laptops")
})
row.withCell((cell) => {
cell.withText("980")
})
row.withCell((cell) => {
cell.withText({ colorAlias: "green" }, "$979,020")
})
})
table.withRow((row) => {
row.withCell((cell) => {
cell.vspan("next")
})
row.withCell((cell) => {
cell.withText("Tablets")
})
row.withCell((cell) => {
cell.withText("650")
})
row.withCell((cell) => {
cell.withText({ colorAlias: "green" }, "$325,000")
})
})
// Total row
table.withRow((row) => {
row.border("top", { width: pt(2) })
row.backgroundColor("lightGray")
row.withCell((cell) => {
cell.hspan("first")
cell.withText({ bold: true }, "Total")
})
row.withCell((cell) => {
cell.hspan("next")
})
row.withCell((cell) => {
cell.withText({ bold: true }, "3,730")
})
row.withCell((cell) => {
cell.withText({ bold: true, colorAlias: "darkBlue" }, "$2,977,770")
})
})
})
// Section 7: Table Width and Alignment
section.body.withHeading("7. Table Width and Alignment", 1)
section.body.withText("Left-aligned table:").closeParagraph()
section.body.withTable((table) => {
table.align("left")
table.border("all", { width: pt(1) })
table.width(inch(3))
table.withRow((row) => {
row.withCell((cell) => cell.withText("Left 1"))
row.withCell((cell) => cell.withText("Left 2"))
})
})
section.body.withText("Center-aligned table:").closeParagraph()
section.body.withTable((table) => {
table.align("center")
table.border("all", { width: pt(1) })
table.width(inch(3))
table.withRow((row) => {
row.withCell((cell) => cell.withText("Center 1"))
row.withCell((cell) => cell.withText("Center 2"))
})
})
section.body.withText("Right-aligned table:").closeParagraph()
section.body.withTable((table) => {
table.align("right")
table.border("all", { width: pt(1) })
table.width(inch(3))
table.withRow((row) => {
row.withCell((cell) => cell.withText("Right 1"))
row.withCell((cell) => cell.withText("Right 2"))
})
})
// Section 8: Row Properties
section.body.withHeading("8. Row Properties", 1)
section.body.withText("Rows can have special properties:").closeParagraph()
section.body.withTable((table) => {
table.border("all", { width: pt(1) })
// Header row that repeats on new pages
table.withHeaderRow((row) => {
row.flags("keepTogether") // Keep row together on one page
row.withCell((cell) => cell.withText({ bold: true }, "This header repeats"))
row.withCell((cell) => cell.withText({ bold: true }, "on each page"))
})
// Regular rows
for (let i = 1; i <= 5; i++) {
table.withRow((row) => {
// Note: pageBreakBefore might not be available in row context
row.withCell((cell) => cell.withText(`Row ${i}`))
row.withCell((cell) => cell.withText(`Data ${i}`))
})
}
})
})
export default builder