-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
208 lines (172 loc) · 6.24 KB
/
Program.cs
File metadata and controls
208 lines (172 loc) · 6.24 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
using ArrayToPdf;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.IO;
using System.Linq;
namespace ConsoleApp
{
class Program
{
static void Main()
{
Example1();
Example2();
Example3();
Example4();
Example5();
Example6();
TestDictionary();
TestExpandoObject();
TestHashtable();
TestDataSet();
TestObject();
TestTypes();
}
static readonly IEnumerable<SomeItem> SomeItems = Enumerable.Range(1, 100).Select(x => new SomeItem
{
Prop1 = $"Text #{x}",
Prop2 = x * 1000,
Prop3 = DateTime.Now.AddDays(-x),
});
// default settings
static void Example1()
{
var pdf = SomeItems.ToPdf();
File.WriteAllBytes($@"..\..\..\..\{nameof(Example1)}.pdf".ToLower(), pdf);
}
// rename title and columns
static void Example2()
{
var pdf = SomeItems.ToPdf(schema => schema
.Title("Example name")
.ColumnName(m => m.Name.Replace("Prop", "Column #")));
File.WriteAllBytes($@"..\..\..\..\{nameof(Example2)}.pdf".ToLower(), pdf);
}
// sort columns
static void Example3()
{
var pdf = SomeItems.ToPdf(schema => schema
.ColumnSort(m => m.Name, desc: true));
File.WriteAllBytes($@"..\..\..\..\{nameof(Example3)}.pdf".ToLower(), pdf);
}
// custom column's mapping
static void Example4()
{
var pdf = SomeItems.ToPdf(schema => schema
.PageOrientation(PdfOrientations.Portrait)
.PageMarginLeft(15)
.AddColumn("MyColumnName #1", x => x.Prop1, 30)
.AddColumn("MyColumnName #2", x => $"test:{x.Prop2}")
.AddColumn("MyColumnName #3", x => x.Prop3));
File.WriteAllBytes($@"..\..\..\..\{nameof(Example4)}.pdf".ToLower(), pdf);
}
// filter columns
static void Example5()
{
var pdf = SomeItems.ToPdf(schema => schema
.ColumnFilter(m => m.Name != "Prop2"));
File.WriteAllBytes($@"..\..\..\..\{nameof(Example5)}.pdf".ToLower(), pdf);
}
// DataTable
static void Example6()
{
var table = new DataTable("Example Table");
table.Columns.Add("Column #1", typeof(string));
table.Columns.Add("Column #2", typeof(int));
table.Columns.Add("Column #3", typeof(DateTime));
for (var x = 1; x <= 100; x++)
table.Rows.Add($"Text #{x}", x * 1000, DateTime.Now.AddDays(-x));
var pdf = table.ToPdf();
File.WriteAllBytes($@"..\..\..\..\{nameof(Example6)}.pdf".ToLower(), pdf);
}
// list of dictionaries
static void TestDictionary()
{
var items = Enumerable.Range(1, 100).Select(x => new Dictionary<object, object>
{
{ "Column #1", $"Text #{x}" },
{ "Column #2", x * 1000 },
{ "Column #3", DateTime.Now.AddDays(-x) },
});
var pdf = items.ToPdf();
File.WriteAllBytes($@"..\{nameof(TestDictionary)}.pdf", pdf);
}
// list of expandos
static void TestExpandoObject()
{
var items = Enumerable.Range(1, 100).Select(x =>
{
var item = new ExpandoObject();
var itemDict = item as IDictionary<string, object>;
itemDict.Add("Column #1", $"Text #{x}");
itemDict.Add("Column #2", x * 1000);
itemDict.Add("Column #3", DateTime.Now.AddDays(-x));
return item;
});
var pdf = items.ToPdf();
File.WriteAllBytes($@"..\{nameof(TestExpandoObject)}.pdf", pdf);
}
// list of hashtables
static void TestHashtable()
{
var items = Enumerable.Range(1, 100).Select(x =>
{
var item = new Hashtable
{
{ "Column #1", $"Text #{x}" },
{ "Column #2", x * 1000 },
{ "Column #3", DateTime.Now.AddDays(-x) }
};
return item;
});
var pdf = items.ToPdf();
File.WriteAllBytes($@"..\{nameof(TestHashtable)}.pdf", pdf);
}
// DataSet
static void TestDataSet()
{
var dataSet = new DataSet();
for (var i = 1; i <= 3; i++)
{
var table = new DataTable($"Table{i}");
dataSet.Tables.Add(table);
table.Columns.Add($"Column {i}-1", typeof(string));
table.Columns.Add($"Column {i}-2", typeof(int));
table.Columns.Add($"Column {i}-3", typeof(DateTime));
for (var x = 1; x <= 10 * i; x++)
table.Rows.Add($"Text #{x}", x * 1000, DateTime.Now.AddDays(-x));
}
var pdf = dataSet.ToPdf();
File.WriteAllBytes($@"..\{nameof(TestDataSet)}.pdf", pdf);
}
// list of objects
static void TestObject()
{
var pdf = SomeItems.AsEnumerable<object>().ToPdf();
File.WriteAllBytes($@"..\{nameof(TestObject)}.pdf", pdf);
}
// different types + stream
static void TestTypes()
{
var items = Enumerable.Range(1, 1000).Select(x => new
{
Bool = x % 2 == 0,
NullableBool = x % 2 == 0 ? true : (bool?)null,
Int = -x * 100,
Uint = (uint)x * 100,
Long = (long)x * 100,
Double = 1.1d + x,
Float = 1.1f + x,
Decimal = 1.1m + x,
DateTime = DateTime.Now.AddDays(-x),
DateTimeOffset = DateTimeOffset.Now.AddDays(-x),
String = $"text text text #{x}",
});
using var file = File.Create($@"..\{nameof(TestTypes)}.pdf");
items.ToPdf(file);
}
}
}