forked from VahidN/PdfReport.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventsPdfReport.cs
More file actions
253 lines (236 loc) · 10.2 KB
/
EventsPdfReport.cs
File metadata and controls
253 lines (236 loc) · 10.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
using System;
using System.Collections.Generic;
using System.Globalization;
using iTextSharp.text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PdfRpt.Core.Contracts;
using PdfRpt.Core.FunctionalTests.Models;
using PdfRpt.Core.Helper;
using PdfRpt.FluentInterface;
using System.Linq;
namespace PdfRpt.Core.FunctionalTests
{
[TestClass]
public class EventsPdfReport
{
[TestMethod]
public void Verify_EventsPdfReport_Can_Be_Created()
{
var report = CreateEventsPdfReport();
TestUtils.VerifyPdfFileIsReadable(report.FileName);
}
public IPdfReportData CreateEventsPdfReport()
{
return new PdfReport().DocumentPreferences(doc =>
{
doc.RunDirection(PdfRunDirection.LeftToRight);
doc.Orientation(PageOrientation.Portrait);
doc.PageSize(PdfPageSize.A4);
doc.DocumentMetadata(new DocumentMetadata { Author = "Vahid", Application = "PdfRpt", Keywords = "IList Rpt.", Subject = "Test Rpt", Title = "Test" });
doc.Compression(new CompressionSettings
{
EnableCompression = true,
EnableFullCompression = true
});
})
.DefaultFonts(fonts =>
{
fonts.Path(TestUtils.GetVerdanaFontPath(),
TestUtils.GetTahomaFontPath());
fonts.Size(9);
fonts.Color(System.Drawing.Color.Black);
})
.PagesFooter(footer =>
{
footer.DefaultFooter(DateTime.Now.ToString("MM/dd/yyyy"));
})
.PagesHeader(header =>
{
header.CacheHeader(cache: true); // It's a default setting to improve the performance.
header.DefaultHeader(defaultHeader =>
{
defaultHeader.RunDirection(PdfRunDirection.LeftToRight);
defaultHeader.ImagePath(TestUtils.GetImagePath("01.png"));
defaultHeader.Message("Our new rpt.");
});
})
.MainTableTemplate(template =>
{
template.BasicTemplate(BasicTemplate.SilverTemplate);
})
.MainTablePreferences(table =>
{
table.ColumnsWidthsType(TableColumnWidthType.Relative);
})
.MainTableDataSource(dataSource =>
{
var listOfRows = new List<Order>();
for (int i = 0; i < 60; i++)
{
listOfRows.Add(new Order
{
Id = i,
Description = "Description Description ... " + i,
Price = 1000 + i
});
}
dataSource.StronglyTypedList(listOfRows);
})
.MainTableSummarySettings(summarySettings =>
{
summarySettings.OverallSummarySettings("Summary");
summarySettings.PreviousPageSummarySettings("Previous Page Summary");
summarySettings.PageSummarySettings("Page Summary");
})
.MainTableColumns(columns =>
{
columns.AddColumn(column =>
{
column.PropertyName("rowNo");
column.IsRowNumber(true);
column.CellsHorizontalAlignment(HorizontalAlignment.Center);
column.IsVisible(true);
column.Order(0);
column.Width(1);
column.HeaderCell("#");
});
columns.AddColumn(column =>
{
column.PropertyName<Order>(x => x.Description);
column.CellsHorizontalAlignment(HorizontalAlignment.Center);
column.IsVisible(true);
column.Order(1);
column.Width(3);
column.HeaderCell("Description");
});
columns.AddColumn(column =>
{
column.PropertyName<Order>(x => x.Id);
column.CellsHorizontalAlignment(HorizontalAlignment.Center);
column.IsVisible(true);
column.Order(2);
column.Width(1);
column.HeaderCell("Id");
});
columns.AddColumn(column =>
{
column.PropertyName<Order>(x => x.Price);
column.CellsHorizontalAlignment(HorizontalAlignment.Center);
column.IsVisible(true);
column.Order(4);
column.Width(2);
column.HeaderCell("Price");
column.ColumnItemsTemplate(template =>
{
template.TextBlock();
template.DisplayFormatFormula(obj => obj == null || string.IsNullOrEmpty(obj.ToString())
? string.Empty : string.Format("{0:n0}", obj));
});
column.AggregateFunction(aggregateFunction =>
{
aggregateFunction.NumericAggregateFunction(AggregateFunction.Sum);
aggregateFunction.DisplayFormatFormula(obj => obj == null || string.IsNullOrEmpty(obj.ToString())
? string.Empty : string.Format("{0:n0}", obj));
});
});
})
.MainTableEvents(events =>
{
events.DataSourceIsEmpty(message: "There is no data available to display.");
events.CellCreated(args =>
{
if (args.CellType == CellType.PreviousPageSummaryCell ||
args.CellType == CellType.PageSummaryCell ||
args.CellType == CellType.SummaryRowCell)
{
if (!string.IsNullOrEmpty(args.Cell.RowData.FormattedValue) &&
args.Cell.RowData.PropertyName == "Price")
{
args.Cell.RowData.FormattedValue += " $";
}
}
});
events.MainTableCreated(args =>
{
var infoTable = new PdfGrid(numColumns: 1)
{
WidthPercentage = 100
};
infoTable.AddSimpleRow(
(cellData, properties) =>
{
cellData.Value = "Show data before the main table ...";
properties.PdfFont = events.PdfFont;
properties.RunDirection = PdfRunDirection.LeftToRight;
});
var table = infoTable.AddBorderToTable(borderColor: BaseColor.LightGray, spacingBefore: 10f);
table.SpacingAfter = 10f;
args.PdfDoc.Add(table);
});
events.ShouldSkipRow(args =>
{
var rowData = args.TableRowData;
//var previousTableRowData = args.PreviousTableRowData;
var description = rowData.FirstOrDefault(x => x.PropertyName == "Description");
if (description != null &&
description.PropertyValue.ToSafeString() == "Description Description ... 1")
{
return true; // don't render this row.
}
return false;
});
var pageNumber = 0;
events.ShouldSkipHeader(args =>
{
pageNumber++;
if (pageNumber == 2)
{
return true; // don't render this header row.
}
return false;
});
events.ShouldSkipFooter(args =>
{
if (pageNumber == 2)
{
return true; // don't render this footer row.
}
return false;
});
events.MainTableAdded(args =>
{
/*var objData = args.ColumnCellsSummaryData.Where(x => x.CellData.PropertyName.Equals("Price"))
.OrderByDescending(x => x.OverallRowNumber)
.First()
.OverallAggregateValue;*/
var data = args.LastOverallAggregateValueOf<Order>(y => y.Price);
var msg = "Total: " + data + ", " + long.Parse(data, NumberStyles.AllowThousands, CultureInfo.InvariantCulture).NumberToText(Language.English);
var infoTable = new PdfGrid(numColumns: 1)
{
WidthPercentage = 100
};
infoTable.AddSimpleRow(
(cellData, properties) =>
{
cellData.Value = "Show data after the main table ...";
properties.PdfFont = events.PdfFont;
properties.RunDirection = PdfRunDirection.LeftToRight;
});
infoTable.AddSimpleRow(
(cellData, properties) =>
{
cellData.Value = msg;
properties.PdfFont = events.PdfFont;
properties.RunDirection = PdfRunDirection.LeftToRight;
});
args.PdfDoc.Add(infoTable.AddBorderToTable(borderColor: BaseColor.LightGray, spacingBefore: 10f));
});
})
.Export(export =>
{
export.ToExcel();
})
.Generate(data => data.AsPdfFile(TestUtils.GetOutputFileName()));
}
}
}