-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sample12.cs
227 lines (201 loc) · 9.39 KB
/
Sample12.cs
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
using iTin.Core.ComponentModel;
using iTin.Core.Models;
using iTin.Core.Models.Design.Enums;
using iTin.Core.Models.Design.Styling;
using iTin.Logging.ComponentModel;
using iTin.Utilities.Pdf.Design.Styles;
namespace iPdfWriter.ConsoleApp.Code
{
/// <summary>
/// Shows the use of how serialize and deserialize text, image and table styles.
/// </summary>
internal static class Sample12
{
public static void Generate(ILogger logger)
{
#region image style
logger.Info(" > Working with image styles");
var imageStyle = new PdfImageStyle
{
Name = "ImageStyle",
Borders =
{
new BaseBorder { Color = "Red", Show = YesNo.Yes, Position = KnownBorderPosition.Right },
new BaseBorder { Color = "Yellow", Show = YesNo.Yes, Position = KnownBorderPosition.Top }
},
Content =
{
Color = "Blue",
Alignment =
{
Horizontal = KnownHorizontalAlignment.Right
},
Properties = new Properties
{
new() { Name = "p001", Value = "v001" },
new() { Name = "p002", Value = "v002" }
}
}
};
// Save image style to disk
var imageStyleAsXmlResult = imageStyle.SaveToFile("~/Output/Sample12/ImageStyle");
if (!imageStyleAsXmlResult.Success)
{
logger.Info(" > Error while saving image style as xml to disk");
logger.Info($" > Error: {imageStyleAsXmlResult.Errors.AsMessages().ToStringBuilder()}");
}
else
{
logger.Info(" > Saved image style as xml to disk correctly");
logger.Info(" > Path: ~/Output/Sample12/ImageStyle.xml");
}
var imageStyleAsJsonResult = imageStyle.SaveToFile("~/Output/Sample12/ImageStyle", KnownFileFormat.Json);
if (!imageStyleAsJsonResult.Success)
{
logger.Info(" > Error while saving image style as json to disk");
logger.Info($" > Error: {imageStyleAsJsonResult.Errors.AsMessages().ToStringBuilder()}");
}
else
{
logger.Info(" > Saved image style as json to disk correctly");
logger.Info(" > Path: ~/Output/Sample12/ImageStyle.json");
}
// New image style instances from disk
var imageStyleFromXml = PdfImageStyle.LoadFromFile("~/Output/Sample12/ImageStyle.xml");
logger.Info(imageStyleFromXml == null
? " > Error while loading image style from xml file"
: " > Image style loaded correctly from xml '~/Output/Sample12/ImageStyle.xml' file");
var imageStyleFromJson = PdfImageStyle.LoadFromFile("~/Output/Sample12/ImageStyle.json", KnownFileFormat.Json);
logger.Info(imageStyleFromJson == null
? " > Error while loading image style from json file"
: " > Image style loaded correctly from json '~/Output/Sample12/ImageStyle.json' file");
#endregion
#region text style
logger.Info("");
logger.Info(" > Working with text styles");
var textStyle = new PdfTextStyle
{
Name = "NormalStyle",
Font =
{
Bold = YesNo.Yes,
Italic = YesNo.Yes,
Color = "Yellow",
Underline = YesNo.No
},
Borders =
{
new BaseBorder { Color = "Red", Show = YesNo.Yes, Position = KnownBorderPosition.Right },
new BaseBorder { Color = "Yellow", Show = YesNo.Yes, Position = KnownBorderPosition.Top }
},
Content =
{
Color = "Blue",
Alignment =
{
Vertical = KnownVerticalAlignment.Top,
Horizontal = KnownHorizontalAlignment.Right
},
Properties = new Properties
{
new() { Name = "p001", Value = "v001" },
new() { Name = "p002", Value = "v002" }
}
}
};
// Save text style to disk
var textStyleAsXmlResult = textStyle.SaveToFile("~/Output/Sample12/TextStyle");
if (!textStyleAsXmlResult.Success)
{
logger.Info(" > Error while saving text style as xml to disk");
logger.Info($" > Error: {textStyleAsXmlResult.Errors.AsMessages().ToStringBuilder()}");
}
else
{
logger.Info(" > Saved text style as xml to disk correctly");
logger.Info(" > Path: ~/Output/Sample12/TextStyle.xml");
}
var textStyleAsJsonResult = textStyle.SaveToFile("~/Output/Sample12/TextStyle", KnownFileFormat.Json);
if (!textStyleAsJsonResult.Success)
{
logger.Info(" > Error while saving text style as json to disk");
logger.Info($" > Error: {textStyleAsJsonResult.Errors.AsMessages().ToStringBuilder()}");
}
else
{
logger.Info(" > Saved text style as json to disk correctly");
logger.Info(" > Path: ~/Output/Sample12/TextStyle.json");
}
// New text style instances from disk
var textStyleFromXml = PdfTextStyle.LoadFromFile("~/Output/Sample12/TextStyle.xml");
logger.Info(textStyleFromXml == null
? " > Error while loading text style from xml file"
: " > Text style loaded correctly from xml '~/Output/Sample12/TextStyle.xml' file");
var textStyleFromJson = PdfTextStyle.LoadFromFile("~/Output/Sample12/TextStyle.json", KnownFileFormat.Json);
logger.Info(textStyleFromJson == null
? " > Error while loading text style from json file"
: " > Text style loaded correctly from json '~/Output/Sample12/TextStyle.json' file");
#endregion
#region table style
logger.Info("");
logger.Info(" > Working with table styles");
var tableStyle = new PdfTableStyle
{
Name = "NormalStyle",
Alignment =
{
Vertical = KnownVerticalAlignment.Top
},
Borders =
{
new BaseBorder { Color = "Red", Show = YesNo.Yes, Position = KnownBorderPosition.Right },
new BaseBorder { Color = "Yellow", Show = YesNo.Yes, Position = KnownBorderPosition.Top }
},
Content =
{
Color = "Blue",
Show = YesNo.Yes,
Properties = new Properties
{
new() { Name = "p001", Value = "v001" },
new() { Name = "p002", Value = "v002" }
}
}
};
// Save table style to disk
var tableStyleAsXmlResult = tableStyle.SaveToFile("~/Output/Sample12/TableStyle");
if (!tableStyleAsXmlResult.Success)
{
logger.Info(" > Error while saving table style as xml to disk");
logger.Info($" > Error: {tableStyleAsXmlResult.Errors.AsMessages().ToStringBuilder()}");
}
else
{
logger.Info(" > Saved table style as xml to disk correctly");
logger.Info(" > Path: ~/Output/Sample12/TableStyle.xml");
}
var tableStyleAsJsonResult = tableStyle.SaveToFile("~/Output/Sample12/TableStyle", KnownFileFormat.Json);
if (!tableStyleAsJsonResult.Success)
{
logger.Info(" > Error while saving table style as json to disk");
logger.Info($" > Error: {tableStyleAsJsonResult.Errors.AsMessages().ToStringBuilder()}");
}
else
{
logger.Info(" > Saved table style as json to disk correctly");
logger.Info(" > Path: ~/Output/Sample12/TableStyle.json");
}
// New table style instances from disk
var tableStyleFromXml = PdfTableStyle.LoadFromFile("~/Output/Sample12/TableStyle.xml");
logger.Info(tableStyleFromXml == null
? " > Error while loading table style from xml file"
: " > Table style loaded correctly from xml '~/Output/Sample12/TableStyle.xml' file");
var tableStyleFromJson = PdfTableStyle.LoadFromFile("~/Output/Sample12/TableStyle.json", KnownFileFormat.Json);
logger.Info(tableStyleFromJson == null
? " > Error while loading table style from json file"
: " > Table style loaded correctly from json '~/Output/Sample12/TableStyle.json' file");
#endregion
}
}
}