-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathInvoiceDocument.cs
172 lines (143 loc) · 5.98 KB
/
InvoiceDocument.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
using System.Globalization;
using System.Linq;
using QuestPDF.Drawing;
using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;
namespace QuestPDF.ExampleInvoice
{
public class InvoiceDocument : IDocument
{
public static Image LogoImage { get; } = Image.FromFile("logo.png");
public InvoiceModel Model { get; }
public InvoiceDocument(InvoiceModel model)
{
Model = model;
}
public DocumentMetadata GetMetadata() => DocumentMetadata.Default;
public void Compose(IDocumentContainer container)
{
container
.Page(page =>
{
page.Margin(50);
page.Header().Element(ComposeHeader);
page.Content().Element(ComposeContent);
page.Footer().AlignCenter().Text(text =>
{
text.CurrentPageNumber();
text.Span(" / ");
text.TotalPages();
});
});
}
void ComposeHeader(IContainer container)
{
container.Row(row =>
{
row.RelativeItem().Column(column =>
{
column
.Item().Text($"Invoice #{Model.InvoiceNumber}")
.FontSize(20).SemiBold().FontColor(Colors.Blue.Medium);
column.Item().Text(text =>
{
text.Span("Issue date: ").SemiBold();
text.Span($"{Model.IssueDate:d}");
});
column.Item().Text(text =>
{
text.Span("Due date: ").SemiBold();
text.Span($"{Model.DueDate:d}");
});
});
row.ConstantItem(175).Image(LogoImage);
});
}
void ComposeContent(IContainer container)
{
container.PaddingVertical(40).Column(column =>
{
column.Spacing(20);
column.Item().Row(row =>
{
row.RelativeItem().Component(new AddressComponent("From", Model.SellerAddress));
row.ConstantItem(50);
row.RelativeItem().Component(new AddressComponent("For", Model.CustomerAddress));
});
column.Item().Element(ComposeTable);
var totalPrice = Model.Items.Sum(x => x.Price * x.Quantity);
column.Item().PaddingRight(5).AlignRight().Text($"Grand total: {totalPrice:C}").SemiBold();
if (!string.IsNullOrWhiteSpace(Model.Comments))
column.Item().PaddingTop(25).Element(ComposeComments);
});
}
void ComposeTable(IContainer container)
{
var headerStyle = TextStyle.Default.SemiBold();
container.Table(table =>
{
table.ColumnsDefinition(columns =>
{
columns.ConstantColumn(25);
columns.RelativeColumn(3);
columns.RelativeColumn();
columns.RelativeColumn();
columns.RelativeColumn();
});
table.Header(header =>
{
header.Cell().Text("#");
header.Cell().Text("Product").Style(headerStyle);
header.Cell().AlignRight().Text("Unit price").Style(headerStyle);
header.Cell().AlignRight().Text("Quantity").Style(headerStyle);
header.Cell().AlignRight().Text("Total").Style(headerStyle);
header.Cell().ColumnSpan(5).PaddingTop(5).BorderBottom(1).BorderColor(Colors.Black);
});
foreach (var item in Model.Items)
{
var index = Model.Items.IndexOf(item) + 1;
table.Cell().Element(CellStyle).Text($"{index}");
table.Cell().Element(CellStyle).Text(item.Name);
table.Cell().Element(CellStyle).AlignRight().Text($"{item.Price:C}");
table.Cell().Element(CellStyle).AlignRight().Text($"{item.Quantity}");
table.Cell().Element(CellStyle).AlignRight().Text($"{item.Price * item.Quantity:C}");
static IContainer CellStyle(IContainer container) => container.BorderBottom(1).BorderColor(Colors.Grey.Lighten2).PaddingVertical(5);
}
});
}
void ComposeComments(IContainer container)
{
container.ShowEntire().Background(Colors.Grey.Lighten3).Padding(10).Column(column =>
{
column.Spacing(5);
column.Item().Text("Comments").FontSize(14).SemiBold();
column.Item().Text(Model.Comments);
});
}
}
public class AddressComponent : IComponent
{
private string Title { get; }
private Address Address { get; }
public AddressComponent(string title, Address address)
{
Title = title;
Address = address;
}
public void Compose(IContainer container)
{
container.ShowEntire().Column(column =>
{
column.Spacing(2);
column.Item().Text(Title).SemiBold();
column.Item().PaddingBottom(5).LineHorizontal(1);
column.Item().Text(Address.CompanyName);
column.Item().Text(Address.Street);
column.Item().Text($"{Address.City}, {Address.State}");
column.Item().Text(Address.Email);
column.Item().Text(Address.Phone);
});
}
}
}