-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathDocumentGenerator.cs
301 lines (252 loc) · 13.1 KB
/
DocumentGenerator.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
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
using System;
using System.Diagnostics;
using System.IO;
using Telerik.Windows.Documents.Fixed.FormatProviders.Pdf;
using Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Export;
using Telerik.Windows.Documents.Fixed.Model;
using Telerik.Windows.Documents.Fixed.Model.ColorSpaces;
using Telerik.Windows.Documents.Fixed.Model.Editing;
using Telerik.Windows.Documents.Fixed.Model.Editing.Flow;
using Telerik.Windows.Documents.Fixed.Model.Graphics;
#if NETCOREAPP
using Telerik.Documents.Core.Fonts;
using Telerik.Documents.Primitives;
#else
using FontFamily = System.Windows.Media.FontFamily;
using FontStyles = System.Windows.FontStyles;
using FontWeights = System.Windows.FontWeights;
using Point = System.Windows.Point;
using Size = System.Windows.Size;
#endif
namespace GenerateDocument
{
public class DocumentGenerator
{
private const double defaultLeftIndent = 50;
private const double defaultLineHeight = 16;
public static readonly string RootDirectory = AppDomain.CurrentDomain.BaseDirectory;
private static readonly string sampleDataPath = RootDirectory + "/SampleData/";
private readonly string defaultDocumentName = "Sample Document.pdf";
private static void CenterText(FixedContentEditor editor, string text)
{
Block block = new Block();
block.TextProperties.TrySetFont(new FontFamily("Calibri"));
block.HorizontalAlignment = HorizontalAlignment.Center;
block.VerticalAlignment = VerticalAlignment.Center;
block.GraphicProperties.FillColor = RgbColors.White;
block.InsertText(text);
editor.DrawBlock(block, new Size(96, 96));
}
public void Export(string filePath)
{
PdfFormatProvider formatProvider = new PdfFormatProvider();
formatProvider.ExportSettings.ImageQuality = ImageQuality.High;
string resultFile = System.IO.Path.Combine(filePath, this.defaultDocumentName);
PrepareDirectory(filePath, resultFile);
using (FileStream stream = File.OpenWrite(resultFile))
{
RadFixedDocument document = CreateDocument();
formatProvider.Export(document, stream);
}
Console.WriteLine("The document is generated.");
ProcessStartInfo psi = new ProcessStartInfo()
{
FileName = resultFile,
UseShellExecute = true
};
Process.Start(psi);
}
private static RadFixedDocument CreateDocument()
{
RadFixedDocument document = new RadFixedDocument();
RadFixedPage page = document.Pages.AddPage();
page.Size = new Size(600, 800);
FixedContentEditor editor = new FixedContentEditor(page);
editor.Position.Translate(defaultLeftIndent, 50);
using (Stream stream = File.OpenRead(sampleDataPath + "pdfProcessingWpf.jpg"))
{
editor.DrawImage(stream);
}
double currentTopOffset = 110;
editor.Position.Translate(defaultLeftIndent, currentTopOffset);
double maxWidth = page.Size.Width - defaultLeftIndent * 2;
DrawDescription(editor, maxWidth);
currentTopOffset += defaultLineHeight * 4;
editor.Position.Translate(defaultLeftIndent, currentTopOffset);
using (editor.SaveProperties())
{
DrawFunnelFigure(editor);
}
editor.Position.Translate(defaultLeftIndent * 4, page.Size.Height - 100);
using (Stream stream = File.OpenRead(sampleDataPath + "telerik.jpg"))
{
editor.DrawImage(stream);
}
DrawText(editor, maxWidth);
return document;
}
private static void DrawDescription(FixedContentEditor editor, double maxWidth)
{
Block block = new Block();
block.GraphicProperties.FillColor = RgbColors.Black;
block.HorizontalAlignment = HorizontalAlignment.Left;
block.TextProperties.FontSize = 14;
block.TextProperties.TrySetFont(new FontFamily("Calibri"), FontStyles.Italic, FontWeights.Bold);
block.InsertText("RadPdfProcessing");
block.TextProperties.TrySetFont(new FontFamily("Calibri"));
block.InsertText(" is a document processing library that enables your application to import and export files to and from PDF format. The document model is entirely independent from UI and allows you to generate sleek documents with differently formatted text, images, shapes and more.");
editor.DrawBlock(block, new Size(maxWidth, double.PositiveInfinity));
}
private static void DrawText(FixedContentEditor editor, double maxWidth)
{
double currentTopOffset = 470;
currentTopOffset += defaultLineHeight * 2;
editor.Position.Translate(defaultLeftIndent, currentTopOffset);
editor.TextProperties.FontSize = 11;
Block block = new Block();
block.TextProperties.FontSize = 11;
block.TextProperties.TrySetFont(new FontFamily("Arial"));
block.InsertText("A wizard's job is to vex ");
using (block.GraphicProperties.Save())
{
block.GraphicProperties.FillColor = new RgbColor(255, 146, 208, 80);
block.InsertText("chumps");
}
block.InsertText(" quickly in fog.");
editor.DrawBlock(block, new Size(maxWidth, double.PositiveInfinity));
currentTopOffset += defaultLineHeight;
editor.Position.Translate(defaultLeftIndent, currentTopOffset);
block = new Block();
block.TextProperties.FontSize = 11;
block.TextProperties.TrySetFont(new FontFamily("Trebuchet MS"));
block.InsertText("A wizard's job is to vex chumps ");
using (block.TextProperties.Save())
{
block.TextProperties.UnderlinePattern = UnderlinePattern.Single;
block.TextProperties.UnderlineColor = editor.GraphicProperties.FillColor;
block.InsertText("quickly");
}
block.InsertText(" in fog.");
editor.DrawBlock(block, new Size(maxWidth, double.PositiveInfinity));
currentTopOffset += defaultLineHeight;
editor.Position.Translate(defaultLeftIndent, currentTopOffset);
block = new Block();
block.TextProperties.FontSize = 11;
block.TextProperties.TrySetFont(new FontFamily("Algerian"));
block.InsertText("A ");
using (block.TextProperties.Save())
{
block.TextProperties.UnderlinePattern = UnderlinePattern.Single;
block.TextProperties.UnderlineColor = editor.GraphicProperties.FillColor;
block.InsertText("wizard's");
}
block.InsertText(" job is to vex chumps quickly in fog.");
editor.DrawBlock(block, new Size(maxWidth, double.PositiveInfinity));
currentTopOffset += defaultLineHeight;
editor.Position.Translate(defaultLeftIndent, currentTopOffset);
editor.TextProperties.TrySetFont(new FontFamily("Lucida Calligraphy"));
editor.DrawText("A wizard's job is to vex chumps quickly in fog.", new Size(maxWidth, double.PositiveInfinity));
currentTopOffset += defaultLineHeight + 2;
editor.Position.Translate(defaultLeftIndent, currentTopOffset);
block = new Block();
block.TextProperties.FontSize = 11;
block.TextProperties.TrySetFont(new FontFamily("Consolas"));
block.InsertText("A wizard's job is to vex chumps ");
using (block.TextProperties.Save())
{
block.TextProperties.TrySetFont(new FontFamily("Consolas"), FontStyles.Normal, FontWeights.Bold);
block.InsertText("quickly");
}
block.InsertText(" in fog.");
editor.DrawBlock(block, new Size(maxWidth, double.PositiveInfinity));
currentTopOffset += defaultLineHeight;
editor.Position.Translate(defaultLeftIndent, currentTopOffset);
editor.TextProperties.TrySetFont(new FontFamily("Arial"));
editor.DrawText("Ταχίστη αλώπηξ βαφής ψημένη γη, δρασκελίζει υπέρ νωθρού κυνός.", new Size(maxWidth, double.PositiveInfinity));
currentTopOffset += defaultLineHeight;
editor.Position.Translate(defaultLeftIndent, currentTopOffset);
editor.DrawText("В чащах юга жил бы цитрус? Да, но фальшивый экземпляр!", new Size(maxWidth, double.PositiveInfinity));
currentTopOffset += defaultLineHeight;
editor.Position.Translate(defaultLeftIndent, currentTopOffset);
editor.DrawText("El pingüino Wenceslao hizo kilómetros bajo exhaustiva lluvia y frío; añoraba a su querido cachorro.", new Size(maxWidth, double.PositiveInfinity));
currentTopOffset += defaultLineHeight;
editor.Position.Translate(defaultLeftIndent, currentTopOffset);
editor.TextProperties.TrySetFont(new FontFamily("Malgun Gothic"));
editor.DrawText("키스의 고유조건은 입술끼리 만나야 하고 특별한 기술은 필요치 않다.", new Size(maxWidth, double.PositiveInfinity));
}
private static void DrawFunnelFigure(FixedContentEditor editor)
{
editor.GraphicProperties.IsStroked = false;
editor.GraphicProperties.FillColor = new RgbColor(231, 238, 247);
editor.DrawEllipse(new Point(250, 70), 136, 48);
editor.GraphicProperties.IsStroked = true;
editor.GraphicProperties.StrokeColor = RgbColors.White;
editor.GraphicProperties.StrokeThickness = 1;
editor.GraphicProperties.FillColor = new RgbColor(91, 155, 223);
editor.DrawEllipse(new Point(289, 77), 48, 48);
editor.Position.Translate(291, 204);
CenterText(editor, "Fonts");
editor.Position.Translate(0, 0);
editor.DrawEllipse(new Point(238, 274), 48, 48);
editor.Position.Translate(190, 226);
CenterText(editor, "Images");
editor.Position.Translate(0, 0);
editor.DrawEllipse(new Point(307, 347), 48, 48);
editor.Position.Translate(259, 299);
CenterText(editor, "Shapes");
editor.Position.Translate(0, 0);
PathGeometry arrow = new PathGeometry();
PathFigure figure = arrow.Figures.AddPathFigure();
figure.StartPoint = new Point(287, 422);
figure.IsClosed = true;
figure.Segments.AddLineSegment(new Point(287, 438));
figure.Segments.AddLineSegment(new Point(278, 438));
figure.Segments.AddLineSegment(new Point(300, 454));
figure.Segments.AddLineSegment(new Point(322, 438));
figure.Segments.AddLineSegment(new Point(313, 438));
figure.Segments.AddLineSegment(new Point(313, 422));
editor.DrawPath(arrow);
editor.GraphicProperties.FillColor = new RgbColor(80, 255, 255, 255);
editor.GraphicProperties.IsStroked = true;
editor.GraphicProperties.StrokeThickness = 1;
editor.GraphicProperties.StrokeColor = new RgbColor(91, 155, 223);
PathGeometry funnel = new PathGeometry();
funnel.FillRule = FillRule.EvenOdd;
figure = funnel.Figures.AddPathFigure();
figure.IsClosed = true;
figure.StartPoint = new Point(164, 245);
figure.Segments.AddArcSegment(new Point(436, 245), 136, 48);
figure.Segments.AddArcSegment(new Point(164, 245), 136, 48);
figure = funnel.Figures.AddPathFigure();
figure.IsClosed = true;
figure.StartPoint = new Point(151, 245);
figure.Segments.AddArcSegment(new Point(449, 245), 149, 61);
figure.Segments.AddLineSegment(new Point(332, 415));
figure.Segments.AddArcSegment(new Point(268, 415), 16, 4);
editor.DrawPath(funnel);
editor.Position.Translate(164, 455);
Block block = new Block();
block.TextProperties.Font = editor.TextProperties.Font;
block.GraphicProperties.FillColor = RgbColors.Black;
block.HorizontalAlignment = HorizontalAlignment.Center;
block.VerticalAlignment = VerticalAlignment.Top;
block.TextProperties.FontSize = 18;
block.InsertText("PDF");
editor.DrawBlock(block, new Size(272, double.PositiveInfinity));
}
private static void PrepareDirectory(string filePath, string resultFile)
{
if (Directory.Exists(filePath))
{
if (File.Exists(resultFile))
{
File.Delete(resultFile);
}
}
else
{
Directory.CreateDirectory(filePath);
}
}
}
}