/
MigraDocService.cs
473 lines (372 loc) · 19.1 KB
/
MigraDocService.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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
using AspNetCorePdf.PdfProvider.DataModel;
using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Shapes;
using MigraDoc.DocumentObjectModel.Shapes.Charts;
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.Rendering;
using System;
namespace AspNetCorePdf.PdfProvider
{
public class MigraDocService : IMigraDocService
{
private string _createdDocsPath = ".\\PdfProvider\\Created";
private static string _imagesPath = ".\\PdfProvider\\Images";
private static string FillerTextShortText = "fdvsvsvsfv fsfdsfs fdfs fdfdsfsd fdsfsdf fdf";
private static string FillerTextMediumText = "fdvsvsvsfv fdsfsdfsf fdsfs fsdf fdsfsdfsd fdsfdsf f dsfdsfs deded deded dedede dedede deded deded deded defdsfsdf fdfsdfsd fdfdsfsdf fdfsdfs fdfdsfsd fdsfsdf fdf";
private static string FillerTextText = "fdvsvsvsfv fdsfsdfsf fdsfs fsdf fdsfsdfsd fdsfdsf f dsfdsfs fdsfsdf fdfsdfsd fdfdsfsdf fdfsdfs fdfdsfsd fdsfsdf fdf";
public string CreateMigraDocPdf(PdfData pdfData)
{
// Create a MigraDoc document
Document document = CreateDocument();
//string ddl = MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToString(document);
MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToFile(document, "MigraDoc.mdddl");
PdfDocumentRenderer renderer = new PdfDocumentRenderer(true);
renderer.Document = document;
renderer.RenderDocument();
// Save the document...
string docName = $"{_createdDocsPath}/{pdfData.DocumentName}-{DateTime.UtcNow.ToOADate()}.pdf";
renderer.PdfDocument.Save(docName);
return docName;
}
public static Document CreateDocument()
{
// Create a new MigraDoc document
Document document = new Document();
document.Info.Title = "Hello, MigraDoc";
document.Info.Subject = "Demonstrates an excerpt of the capabilities of MigraDoc.";
document.Info.Author = "Stefan Lange";
DefineStyles(document);
DefineCover(document);
DefineTableOfContents(document);
DefineContentSection(document);
DefineParagraphs(document);
DefineTables(document);
DefineCharts(document);
return document;
}
/// <summary>
/// Defines the styles used in the document.
/// </summary>
static void DefineStyles(Document document)
{
// Get the predefined style Normal.
Style style = document.Styles["Normal"];
// Because all styles are derived from Normal, the next line changes the
// font of the whole document. Or, more exactly, it changes the font of
// all styles and paragraphs that do not redefine the font.
style.Font.Name = "Times New Roman";
// Heading1 to Heading9 are predefined styles with an outline level. An outline level
// other than OutlineLevel.BodyText automatically creates the outline (or bookmarks)
// in PDF.
style = document.Styles["Heading1"];
style.Font.Name = "Tahoma";
style.Font.Size = 14;
style.Font.Bold = true;
style.Font.Color = Colors.DarkBlue;
style.ParagraphFormat.PageBreakBefore = true;
style.ParagraphFormat.SpaceAfter = 6;
style = document.Styles["Heading2"];
style.Font.Size = 12;
style.Font.Bold = true;
style.ParagraphFormat.PageBreakBefore = false;
style.ParagraphFormat.SpaceBefore = 6;
style.ParagraphFormat.SpaceAfter = 6;
style = document.Styles["Heading3"];
style.Font.Size = 10;
style.Font.Bold = true;
style.Font.Italic = true;
style.ParagraphFormat.SpaceBefore = 6;
style.ParagraphFormat.SpaceAfter = 3;
style = document.Styles[StyleNames.Header];
style.ParagraphFormat.AddTabStop("16cm", TabAlignment.Right);
style = document.Styles[StyleNames.Footer];
style.ParagraphFormat.AddTabStop("8cm", TabAlignment.Center);
// Create a new style called TextBox based on style Normal
style = document.Styles.AddStyle("TextBox", "Normal");
style.ParagraphFormat.Alignment = ParagraphAlignment.Justify;
style.ParagraphFormat.Borders.Width = 2.5;
style.ParagraphFormat.Borders.Distance = "3pt";
style.ParagraphFormat.Shading.Color = Colors.SkyBlue;
// Create a new style called TOC based on style Normal
style = document.Styles.AddStyle("TOC", "Normal");
style.ParagraphFormat.AddTabStop("16cm", TabAlignment.Right, TabLeader.Dots);
style.ParagraphFormat.Font.Color = Colors.Blue;
}
static void DefineCover(Document document)
{
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph();
paragraph.Format.SpaceAfter = "3cm";
Image image = section.AddImage($"{_imagesPath}\\logo.jpg");
image.Width = "10cm";
paragraph = section.AddParagraph("A sample document that demonstrates the\ncapabilities of MigraDoc");
paragraph.Format.Font.Size = 16;
paragraph.Format.Font.Color = Colors.DarkRed;
paragraph.Format.SpaceBefore = "8cm";
paragraph.Format.SpaceAfter = "3cm";
paragraph = section.AddParagraph("Rendering date: ");
paragraph.AddDateField();
}
static void DefineTableOfContents(Document document)
{
Section section = document.LastSection;
section.AddPageBreak();
Paragraph paragraph = section.AddParagraph("Table of Contents");
paragraph.Format.Font.Size = 14;
paragraph.Format.Font.Bold = true;
paragraph.Format.SpaceAfter = 24;
paragraph.Format.OutlineLevel = OutlineLevel.Level1;
paragraph = section.AddParagraph();
paragraph.Style = "TOC";
Hyperlink hyperlink = paragraph.AddHyperlink("Paragraphs");
hyperlink.AddText("Paragraphs\t");
hyperlink.AddPageRefField("Paragraphs");
paragraph = section.AddParagraph();
paragraph.Style = "TOC";
hyperlink = paragraph.AddHyperlink("Tables");
hyperlink.AddText("Tables\t");
hyperlink.AddPageRefField("Tables");
paragraph = section.AddParagraph();
paragraph.Style = "TOC";
hyperlink = paragraph.AddHyperlink("Charts");
hyperlink.AddText("Charts\t");
hyperlink.AddPageRefField("Charts");
}
static void DefineContentSection(Document document)
{
Section section = document.AddSection();
section.PageSetup.OddAndEvenPagesHeaderFooter = true;
section.PageSetup.StartingNumber = 1;
HeaderFooter header = section.Headers.Primary;
header.AddParagraph("\tOdd Page Header");
header = section.Headers.EvenPage;
header.AddParagraph("Even Page Header");
// Create a paragraph with centered page number. See definition of style "Footer".
Paragraph paragraph = new Paragraph();
paragraph.AddTab();
paragraph.AddPageField();
// Add paragraph to footer for odd pages.
section.Footers.Primary.Add(paragraph);
// Add clone of paragraph to footer for odd pages. Cloning is necessary because an object must
// not belong to more than one other object. If you forget cloning an exception is thrown.
section.Footers.EvenPage.Add(paragraph.Clone());
}
static void DefineParagraphs(Document document)
{
Paragraph paragraph = document.LastSection.AddParagraph("Paragraph Layout Overview", "Heading1");
paragraph.AddBookmark("Paragraphs");
DemonstrateAlignment(document);
DemonstrateIndent(document);
DemonstrateFormattedText(document);
DemonstrateBordersAndShading(document);
}
static void DemonstrateAlignment(Document document)
{
document.LastSection.AddParagraph("Alignment", "Heading2");
document.LastSection.AddParagraph("Left Aligned", "Heading3");
Paragraph paragraph = document.LastSection.AddParagraph();
paragraph.Format.Alignment = ParagraphAlignment.Left;
paragraph.AddText(FillerTextText);
document.LastSection.AddParagraph("Right Aligned", "Heading3");
paragraph = document.LastSection.AddParagraph();
paragraph.Format.Alignment = ParagraphAlignment.Right;
paragraph.AddText(FillerTextText);
document.LastSection.AddParagraph("Centered", "Heading3");
paragraph = document.LastSection.AddParagraph();
paragraph.Format.Alignment = ParagraphAlignment.Center;
paragraph.AddText(FillerTextText);
document.LastSection.AddParagraph("Justified", "Heading3");
paragraph = document.LastSection.AddParagraph();
paragraph.Format.Alignment = ParagraphAlignment.Justify;
paragraph.AddText(FillerTextMediumText);
}
static void DemonstrateIndent(Document document)
{
document.LastSection.AddParagraph("Indent", "Heading2");
document.LastSection.AddParagraph("Left Indent", "Heading3");
Paragraph paragraph = document.LastSection.AddParagraph();
paragraph.Format.LeftIndent = "2cm";
paragraph.AddText(FillerTextText);
document.LastSection.AddParagraph("Right Indent", "Heading3");
paragraph = document.LastSection.AddParagraph();
paragraph.Format.RightIndent = "1in";
paragraph.AddText(FillerTextText);
document.LastSection.AddParagraph("First Line Indent", "Heading3");
paragraph = document.LastSection.AddParagraph();
paragraph.Format.FirstLineIndent = "12mm";
paragraph.AddText(FillerTextText);
document.LastSection.AddParagraph("First Line Negative Indent", "Heading3");
paragraph = document.LastSection.AddParagraph();
paragraph.Format.LeftIndent = "1.5cm";
paragraph.Format.FirstLineIndent = "-1.5cm";
paragraph.AddText(FillerTextText);
}
static void DemonstrateFormattedText(Document document)
{
document.LastSection.AddParagraph("Formatted Text", "Heading2");
//document.LastSection.AddParagraph("Left Aligned", "Heading3");
Paragraph paragraph = document.LastSection.AddParagraph();
paragraph.AddText("Text can be formatted ");
paragraph.AddFormattedText("bold", TextFormat.Bold);
paragraph.AddText(", ");
paragraph.AddFormattedText("italic", TextFormat.Italic);
paragraph.AddText(", or ");
paragraph.AddFormattedText("bold & italic", TextFormat.Bold | TextFormat.Italic);
paragraph.AddText(".");
paragraph.AddLineBreak();
paragraph.AddText("You can set the ");
FormattedText formattedText = paragraph.AddFormattedText("size ");
formattedText.Size = 15;
paragraph.AddText("the ");
formattedText = paragraph.AddFormattedText("color ");
formattedText.Color = Colors.Firebrick;
paragraph.AddText("the ");
formattedText = paragraph.AddFormattedText("font", new Font("Verdana"));
paragraph.AddText(".");
paragraph.AddLineBreak();
paragraph.AddText("You can set the ");
formattedText = paragraph.AddFormattedText("subscript");
formattedText.Subscript = true;
paragraph.AddText(" or ");
formattedText = paragraph.AddFormattedText("superscript");
formattedText.Superscript = true;
paragraph.AddText(".");
}
static void DemonstrateBordersAndShading(Document document)
{
document.LastSection.AddPageBreak();
document.LastSection.AddParagraph("Borders and Shading", "Heading2");
document.LastSection.AddParagraph("Border around Paragraph", "Heading3");
Paragraph paragraph = document.LastSection.AddParagraph();
paragraph.Format.Borders.Width = 2.5;
paragraph.Format.Borders.Color = Colors.Navy;
paragraph.Format.Borders.Distance = 3;
paragraph.AddText(FillerTextMediumText);
document.LastSection.AddParagraph("Shading", "Heading3");
paragraph = document.LastSection.AddParagraph();
paragraph.Format.Shading.Color = Colors.LightCoral;
paragraph.AddText(FillerTextText);
document.LastSection.AddParagraph("Borders & Shading", "Heading3");
paragraph = document.LastSection.AddParagraph();
paragraph.Style = "TextBox";
paragraph.AddText(FillerTextMediumText);
}
static void DefineTables(Document document)
{
Paragraph paragraph = document.LastSection.AddParagraph("Table Overview", "Heading1");
paragraph.AddBookmark("Tables");
DemonstrateSimpleTable(document);
DemonstrateTableAlignment(document);
DemonstrateCellMerge(document);
}
static void DemonstrateSimpleTable(Document document)
{
document.LastSection.AddParagraph("Simple Tables", "Heading2");
Table table = new Table();
table.Borders.Width = 0.75;
Column column = table.AddColumn(Unit.FromCentimeter(2));
column.Format.Alignment = ParagraphAlignment.Center;
table.AddColumn(Unit.FromCentimeter(5));
Row row = table.AddRow();
row.Shading.Color = Colors.PaleGoldenrod;
Cell cell = row.Cells[0];
cell.AddParagraph("Itemus");
cell = row.Cells[1];
cell.AddParagraph("Descriptum");
row = table.AddRow();
cell = row.Cells[0];
cell.AddParagraph("1");
cell = row.Cells[1];
cell.AddParagraph(FillerTextShortText);
row = table.AddRow();
cell = row.Cells[0];
cell.AddParagraph("2");
cell = row.Cells[1];
cell.AddParagraph(FillerTextText);
table.SetEdge(0, 0, 2, 3, Edge.Box, BorderStyle.Single, 1.5, Colors.Black);
document.LastSection.Add(table);
}
public static void DemonstrateTableAlignment(Document document)
{
document.LastSection.AddParagraph("Cell Alignment", "Heading2");
Table table = document.LastSection.AddTable();
table.Borders.Visible = true;
table.Format.Shading.Color = Colors.LavenderBlush;
table.Shading.Color = Colors.Salmon;
table.TopPadding = 5;
table.BottomPadding = 5;
Column column = table.AddColumn();
column.Format.Alignment = ParagraphAlignment.Left;
column = table.AddColumn();
column.Format.Alignment = ParagraphAlignment.Center;
column = table.AddColumn();
column.Format.Alignment = ParagraphAlignment.Right;
table.Rows.Height = 35;
Row row = table.AddRow();
row.VerticalAlignment = VerticalAlignment.Top;
row.Cells[0].AddParagraph("Text");
row.Cells[1].AddParagraph("Text");
row.Cells[2].AddParagraph("Text");
row = table.AddRow();
row.VerticalAlignment = VerticalAlignment.Center;
row.Cells[0].AddParagraph("Text");
row.Cells[1].AddParagraph("Text");
row.Cells[2].AddParagraph("Text");
row = table.AddRow();
row.VerticalAlignment = VerticalAlignment.Bottom;
row.Cells[0].AddParagraph("Text");
row.Cells[1].AddParagraph("Text");
row.Cells[2].AddParagraph("Text");
}
public static void DemonstrateCellMerge(Document document)
{
document.LastSection.AddParagraph("Cell Merge", "Heading2");
Table table = document.LastSection.AddTable();
table.Borders.Visible = true;
table.TopPadding = 5;
table.BottomPadding = 5;
Column column = table.AddColumn();
column.Format.Alignment = ParagraphAlignment.Left;
column = table.AddColumn();
column.Format.Alignment = ParagraphAlignment.Center;
column = table.AddColumn();
column.Format.Alignment = ParagraphAlignment.Right;
table.Rows.Height = 35;
Row row = table.AddRow();
row.Cells[0].AddParagraph("Merge Right");
row.Cells[0].MergeRight = 1;
row = table.AddRow();
row.VerticalAlignment = VerticalAlignment.Bottom;
row.Cells[0].MergeDown = 1;
row.Cells[0].VerticalAlignment = VerticalAlignment.Bottom;
row.Cells[0].AddParagraph("Merge Down");
table.AddRow();
}
public static void DefineCharts(Document document)
{
Paragraph paragraph = document.LastSection.AddParagraph("Chart Overview", "Heading1");
paragraph.AddBookmark("Charts");
document.LastSection.AddParagraph("Sample Chart", "Heading2");
Chart chart = new Chart();
chart.Left = 0;
chart.Width = Unit.FromCentimeter(16);
chart.Height = Unit.FromCentimeter(12);
Series series = chart.SeriesCollection.AddSeries();
series.ChartType = ChartType.Column2D;
series.Add(new double[] { 1, 17, 45, 5, 3, 20, 11, 23, 8, 19 });
series.HasDataLabel = true;
series = chart.SeriesCollection.AddSeries();
series.ChartType = ChartType.Line;
series.Add(new double[] { 41, 7, 5, 45, 13, 10, 21, 13, 18, 9 });
XSeries xseries = chart.XValues.AddXSeries();
xseries.Add("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N");
chart.XAxis.MajorTickMark = TickMarkType.Outside;
chart.XAxis.Title.Caption = "X-Axis";
chart.YAxis.MajorTickMark = TickMarkType.Outside;
chart.YAxis.HasMajorGridlines = true;
chart.PlotArea.LineFormat.Color = Colors.DarkGray;
chart.PlotArea.LineFormat.Width = 1;
document.LastSection.Add(chart);
}
}
}