-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathAcroFormTemplatePdfReport.cs
126 lines (122 loc) · 5.65 KB
/
AcroFormTemplatePdfReport.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
using System;
using System.Collections.Generic;
using PdfReportSamples.Models;
using PdfRpt.Core.Contracts;
using PdfRpt.FluentInterface;
namespace PdfReportSamples.AcroFormTemplate
{
public class AcroFormTemplatePdfReport
{
public IPdfReportData CreatePdfReport()
{
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 = "Test", Subject = "Test Rpt", Title = "Test" });
doc.Compression(new CompressionSettings
{
EnableCompression = true,
EnableFullCompression = true
});
})
.DefaultFonts(fonts =>
{
fonts.Path(System.IO.Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"), "fonts\\tahoma.ttf"),
System.IO.Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"), "fonts\\verdana.ttf"));
fonts.Size(9);
fonts.Color(System.Drawing.Color.Black);
})
.PagesFooter(footer =>
{
footer.DefaultFooter(printDate: 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(System.IO.Path.Combine(AppPath.ApplicationPath, "Images\\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<AcroFormModel>();
for (int i = 1; i <= 20; i++)
{
listOfRows.Add(new AcroFormModel
{
Id = i,
Age = 20 + i,
Name = "N" + i,
LastName = "L" + i,
Gender = (i % 2 == 0) ? "Male" : "Female"
});
}
dataSource.StronglyTypedList(listOfRows);
})
.MainTableEvents(events =>
{
events.DataSourceIsEmpty(message: "There is no data available to display.");
})
.MainTableColumns(columns =>
{
columns.AddColumn(column =>
{
column.PropertyName("rowNo");
column.IsRowNumber(true);
column.Width(1);
column.CellsHorizontalAlignment(HorizontalAlignment.Center);
column.IsVisible(true);
column.Order(0);
column.HeaderCell(caption: "#");
});
columns.AddColumn(column =>
{
column.PropertyName<AcroFormModel>(x => x.Id);
column.HeaderCell(caption: "AcroForm");
column.Width(6);
column.CellsHorizontalAlignment(HorizontalAlignment.Center);
column.IsVisible(true);
column.Order(1);
column.ColumnItemsTemplate(template =>
{
template.PdfTemplate(
pdfTemplatePath: System.IO.Path.Combine(AppPath.ApplicationPath, "data\\AcroFormSample.pdf"),
onFillAcroForm: (data, form, pdfStamper) =>
{
form.GenerateAppearances = true;
foreach (var item in data)
{
var value = item.PropertyValue == null ? string.Empty : item.PropertyValue.ToString();
if (item.PropertyNameEquals<AcroFormModel>(x => x.Gender))
{
if (value == "Male")
form.SetField("Male", string.Empty); // "" and "Off" are valid values here
if (value == "Female")
form.SetField("Female", string.Empty);
}
else
{
form.SetField(item.PropertyName, value);
}
}
});
});
});
})
.Generate(data => data.AsPdfFile(string.Format("{0}\\Pdf\\AcroFormTemplate-{1}.pdf", AppPath.ApplicationPath, Guid.NewGuid().ToString("N"))));
}
}
}