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
|
/////////////////////////////////////////////////////////////////////////
// Copyright (C) 2002-2011 Aspose Pty Ltd. All rights reserved.
//
// This file is part of Aspose.Pdf. The source code in this file
// is only intended as a supplement to the documentation, and is provided
// "as is", without warranty of any kind, either expressed or implied.
/////////////////////////////////////////////////////////////////////////
using System;
using Aspose.Pdf.Generator;
using System.Globalization;
namespace Aspose.Pdf.Demos
{
/// <summary>
/// Summary description for Invoice.
/// </summary>
public class Invoice
{
private System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
private System.Data.OleDb.OleDbDataReader resultReader;
private System.Data.OleDb.OleDbConnection oleDbConnection;
string path;
System.Globalization.CultureInfo locale = new System.Globalization.CultureInfo("en-US");
public Invoice(string curPath)
{
path = curPath;
this.oleDbConnection = new System.Data.OleDb.OleDbConnection();
this.oleDbConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path +
"\\Database\\Northwind.mdb";
this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand();
this.oleDbSelectCommand1.Connection = this.oleDbConnection;
this.oleDbConnection.Open();
}
public Aspose.Pdf.Generator.Pdf GetInvoice()
{
string invoiceStr ="SELECT DISTINCTROW Orders.ShipName, Orders.ShipAddress, Orders.ShipCity, " +
"Orders.ShipRegion, Orders.ShipPostalCode, Orders.ShipCountry, Orders.CustomerID, " +
"Customers.CompanyName, Customers.Address, Customers.City, Customers.Region, " +
"Customers.PostalCode, Customers.Country, [FirstName] & \" \" & [LastName] AS Salesperson, " +
"Orders.OrderID, Orders.OrderDate, Orders.RequiredDate, Orders.ShippedDate, " +
"Shippers.CompanyName, [Order Details].ProductID, Products.ProductName, " +
"[Order Details].UnitPrice, [Order Details].Quantity, [Order Details].Discount, " +
"CCur([Order Details].UnitPrice*[Quantity]*(1-[Discount])/100)*100 AS ExtendedPrice, " +
"Orders.Freight,Shippers.CompanyName FROM Shippers INNER JOIN (Products INNER JOIN ((Employees " +
"INNER JOIN (Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID) " +
"ON Employees.EmployeeID = Orders.EmployeeID) INNER JOIN [Order Details] ON " +
"Orders.OrderID = [Order Details].OrderID) ON Products.ProductID = " +
"[Order Details].ProductID) ON Shippers.ShipperID = Orders.ShipVia";
Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
pdf.IsTruetypeFontMapCached = false;
// If you have purchased a license,
// Set license like this:
// string licenseFile = MapPath("License") + "\\Aspose.Pdf.lic";
// Aspose.Pdf.License lic = new Aspose.Pdf.License();
// lic.SetLicense(licenseFile);
string xmlFile = path + "\\Xml\\Invoice.xml";
pdf.BindXML(xmlFile, null);
HeaderFooter headerFooter = pdf.Sections[0].OddHeader;
Image logoImage = (Image)headerFooter.Paragraphs[0];
logoImage.ImageInfo.File = path + "\\Images\\logo1.jpg";
logoImage.ImageScale = 0.74F;
this.oleDbSelectCommand1.CommandText =
"SELECT DISTINCTROW Orders.OrderID FROM Orders ORDER BY Orders.OrderID DESC;";
resultReader = this.oleDbSelectCommand1.ExecuteReader();
int ordersNumber=0,number;
int[] orderIDs = new int[2048];
while(resultReader.Read())
{
orderIDs[ordersNumber] = Convert.ToInt32(resultReader.GetValue(0));
ordersNumber++;
}
resultReader.Close();
//for(number = 0; number < ordersNumber; number++)
//We generate invoices for the first 100 orders for demo only. If you want to
//generate all invoices,uncomment the line above and comment the line below.
for(number = 0; number < 100; number++)
{
this.oleDbSelectCommand1.CommandText = "SELECT * FROM (" + invoiceStr +
") WHERE ((Orders.OrderID)=" + orderIDs[number].ToString() + ")";
resultReader = this.oleDbSelectCommand1.ExecuteReader();
PutOrder(pdf,resultReader);
resultReader.Close();
}
this.oleDbConnection.Close();
return pdf;
}
private void PutOrder(Aspose.Pdf.Generator.Pdf pdf, System.Data.OleDb.OleDbDataReader resultReader)
{
bool first = true;
float subTotal = 0,freight = 0;
Table table = null;
int i = 0;
while(resultReader.Read())
{
if(first)
{
PutAdress(pdf,resultReader,i);
PutSummary(pdf,resultReader,i);
table = AddTable(pdf);
}
AddRow(pdf,table,resultReader,i);
first = false;
subTotal += (float)Convert.ToDouble(resultReader.GetValue(24));
if(freight == 0)
freight = (float)Convert.ToDouble(resultReader.GetValue(25));
i++;
}
PutAmount(pdf,subTotal,freight);
}
private void PutAdress(Aspose.Pdf.Generator.Pdf pdf, System.Data.OleDb.OleDbDataReader resultReader, int rowNumber)
{
Table adressTable = new Table();
adressTable.IsFirstParagraph = true;
adressTable.ColumnWidths = "60 180 60 180";
adressTable.DefaultCellTextInfo.FontSize = 10;
adressTable.DefaultCellTextInfo.LineSpacing = 3;
adressTable.DefaultCellPadding.Bottom = 3;
Section section = pdf.Sections[0];
section.Paragraphs.Add(adressTable);
Row row1AdressTable = new Row(adressTable);
adressTable.Rows.Add(row1AdressTable);
Aspose.Pdf.Generator.TextInfo tf1 = new Aspose.Pdf.Generator.TextInfo();
tf1.Color = new Aspose.Pdf.Generator.Color(0, 0, 160);
tf1.FontName = "Helvetica-Bold";
row1AdressTable.Cells.Add("Ship To:",tf1);
string city,regin,postalCode;
city = resultReader.GetValue(2) is DBNull?"":Convert.ToString(resultReader.GetValue(2));
regin = Convert.ToString(resultReader.GetValue(3));
postalCode = Convert.ToString(resultReader.GetValue(4));
row1AdressTable.Cells.Add(Convert.ToString(resultReader.GetValue(0))
+ "#$NL" + Convert.ToString(resultReader.GetValue(1)) + "#$NL" +
city + regin + postalCode + "#$NL" + Convert.ToString(resultReader.GetValue(5)));
row1AdressTable.Cells.Add("Bill To:",tf1);
city = resultReader.GetValue(9) is DBNull?"":Convert.ToString(resultReader.GetValue(9));
regin = resultReader.GetValue(10) is DBNull?"":Convert.ToString(resultReader.GetValue(10));
postalCode = resultReader.GetValue(11) is DBNull?"":Convert.ToString(resultReader.GetValue(11));
row1AdressTable.Cells.Add(Convert.ToString(resultReader.GetValue(7)) + "#$NL" +
Convert.ToString(resultReader.GetValue(8)) + "#$NL" + city + regin + postalCode + "#$NL" +
Convert.ToString(resultReader.GetValue(12)));
}
private void PutSummary(Aspose.Pdf.Generator.Pdf pdf, System.Data.OleDb.OleDbDataReader resultReader, int rowNumber)
{
Table summaryTable = new Table();
summaryTable.Margin.Top = 20;
summaryTable.ColumnWidths = "50 70 70 70 75 75 75";
summaryTable.DefaultCellTextInfo.FontSize = 10;
summaryTable.DefaultCellBorder = new BorderInfo((int)BorderSide.All, new Aspose.Pdf.Generator.Color(0, 0, 160));
summaryTable.DefaultCellPadding.Bottom = summaryTable.DefaultCellPadding.Top = 3;
Section section = pdf.Sections[0];
section.Paragraphs.Add(summaryTable);
Row row1SummaryTable = summaryTable.Rows.Add();
Aspose.Pdf.Generator.TextInfo tf1 = new Aspose.Pdf.Generator.TextInfo();
tf1.FontSize = 10;
tf1.Color = new Aspose.Pdf.Generator.Color("White");
tf1.FontName = "Helvetica-Bold";
tf1.Alignment = AlignmentType.Center;
Cell cell1Row1SummaryTable = row1SummaryTable.Cells.Add("Order ID:",tf1);
cell1Row1SummaryTable.BackgroundColor = new Aspose.Pdf.Generator.Color(0, 0, 160);
Cell cell2Row1SummaryTable = row1SummaryTable.Cells.Add("Customer ID:",tf1);
cell2Row1SummaryTable.BackgroundColor = new Aspose.Pdf.Generator.Color(0, 0, 160);
Cell cell3Row1SummaryTable = row1SummaryTable.Cells.Add("Sales Person:",tf1);
cell3Row1SummaryTable.BackgroundColor = new Aspose.Pdf.Generator.Color(0, 0, 160);
Cell cell4Row1SummaryTable = row1SummaryTable.Cells.Add("Order Date:",tf1);
cell4Row1SummaryTable.BackgroundColor = new Aspose.Pdf.Generator.Color(0, 0, 160);
Cell cell5Row1SummaryTable = row1SummaryTable.Cells.Add("Required Date:",tf1);
cell5Row1SummaryTable.BackgroundColor = new Aspose.Pdf.Generator.Color(0, 0, 160);
Cell cell6Row1SummaryTable = row1SummaryTable.Cells.Add("Shipped Date:",tf1);
cell6Row1SummaryTable.BackgroundColor = new Aspose.Pdf.Generator.Color(0, 0, 160);
Cell cell7Row1SummaryTable = row1SummaryTable.Cells.Add("Shipped Date:",tf1);
cell7Row1SummaryTable.BackgroundColor = new Aspose.Pdf.Generator.Color(0, 0, 160);
Row row2SummaryTable = summaryTable.Rows.Add();
tf1 = new Aspose.Pdf.Generator.TextInfo();
tf1.FontSize = 9;
tf1.Color = new Aspose.Pdf.Generator.Color("Black");
tf1.FontName = "Times-Roman";
tf1.Alignment = AlignmentType.Center;
row2SummaryTable.Cells.Add(Convert.ToInt32(resultReader.GetValue(14)).ToString(),tf1);
row2SummaryTable.Cells.Add(Convert.ToString(resultReader.GetValue(6)),tf1);
row2SummaryTable.Cells.Add(Convert.ToString(resultReader.GetValue(13)),tf1);
row2SummaryTable.Cells.Add(Convert.ToDateTime(resultReader.GetValue(15)).ToString(
"d",DateTimeFormatInfo.InvariantInfo),tf1);
row2SummaryTable.Cells.Add(Convert.ToDateTime(resultReader.GetValue(16)).ToString(
"d",DateTimeFormatInfo.InvariantInfo),tf1);
row2SummaryTable.Cells.Add(resultReader.GetValue(17) is DBNull?"":
Convert.ToDateTime(resultReader.GetValue(17)).ToString(
"d",DateTimeFormatInfo.InvariantInfo),tf1);
row2SummaryTable.Cells.Add(Convert.ToString(resultReader.GetValue(26)),tf1);
}
private Table AddTable(Aspose.Pdf.Generator.Pdf pdf)
{
Table detailTable = new Table();
detailTable.Margin.Top = 20;
detailTable.ColumnWidths = "60 150 60 80 60 80";
detailTable.DefaultCellBorder = new BorderInfo((int)(BorderSide.Top|BorderSide.Bottom),
new Aspose.Pdf.Generator.Color(0, 0, 160));
detailTable.DefaultCellTextInfo.FontSize = 10;
detailTable.DefaultCellPadding.Bottom = detailTable.DefaultCellPadding.Top = 4;
Section section = pdf.Sections[0];
section.Paragraphs.Add(detailTable);
Row row1DetailTable = detailTable.Rows.Add();
Aspose.Pdf.Generator.TextInfo tf1 = new Aspose.Pdf.Generator.TextInfo();
tf1.FontSize = 10;
tf1.Color = new Aspose.Pdf.Generator.Color("White");
tf1.FontName = "Helvetica-Bold";
tf1.Alignment = AlignmentType.Center;
Cell cell1Row1DetailTable = row1DetailTable.Cells.Add("Product ID:",tf1);
cell1Row1DetailTable.BackgroundColor = new Aspose.Pdf.Generator.Color(0, 0, 160);
Cell cell2Row1DetailTable = row1DetailTable.Cells.Add("Product Name:",tf1);
cell2Row1DetailTable.BackgroundColor = new Aspose.Pdf.Generator.Color(0, 0, 160);
Cell cell3Row1DetailTable = row1DetailTable.Cells.Add("Quantity:",tf1);
cell3Row1DetailTable.BackgroundColor = new Aspose.Pdf.Generator.Color(0, 0, 160);
Cell cell4Row1DetailTable = row1DetailTable.Cells.Add("Unit Price:",tf1);
cell4Row1DetailTable.BackgroundColor = new Aspose.Pdf.Generator.Color(0, 0, 160);
Cell cell5Row1DetailTable = row1DetailTable.Cells.Add("Discount:",tf1);
cell5Row1DetailTable.BackgroundColor = new Aspose.Pdf.Generator.Color(0, 0, 160);
Cell cell6Row1DetailTable = row1DetailTable.Cells.Add("Extended Price:",tf1);
cell6Row1DetailTable.BackgroundColor = new Aspose.Pdf.Generator.Color(0, 0, 160);
return detailTable;
}
private void AddRow(Aspose.Pdf.Generator.Pdf pdf, Aspose.Pdf.Generator.Table tabel, System.Data.OleDb.OleDbDataReader resultReader, int rowNumber)
{
Section section = pdf.Sections[0];
Aspose.Pdf.Generator.TextInfo tf1 = new Aspose.Pdf.Generator.TextInfo();
tf1.FontSize = 10;
tf1.Alignment = AlignmentType.Center;
Row row1DetailTable = tabel.Rows.Add();
row1DetailTable.Cells.Add(Convert.ToInt32(resultReader.GetValue(19)).ToString(),tf1);
tf1.Alignment = AlignmentType.Left;
row1DetailTable.Cells.Add(Convert.ToString(resultReader.GetValue(20)),tf1);
tf1.Alignment = AlignmentType.Center;
row1DetailTable.Cells.Add(Convert.ToInt32(resultReader.GetValue(22)).ToString(),tf1);
row1DetailTable.Cells.Add(Convert.ToDouble(resultReader.GetValue(21)).ToString("c",locale),tf1);
float v = (float)Convert.ToDouble(resultReader.GetValue(23)) * 100;
row1DetailTable.Cells.Add(v.ToString() + "%",tf1);
row1DetailTable.Cells.Add(Convert.ToDouble(resultReader.GetValue(24)).ToString("c",locale),tf1);
}
private void PutAmount(Aspose.Pdf.Generator.Pdf pdf, float subTotal, float freight)
{
CultureInfo culture = new CultureInfo("en-US");
Table amountTable = new Table();
amountTable.Margin.Top = 20;
amountTable.Margin.Left = 370;
amountTable.ColumnWidths = "60 60";
amountTable.DefaultCellBorder = new BorderInfo((int)BorderSide.Bottom,new Aspose.Pdf.Generator.Color(0,0,160));
amountTable.DefaultCellPadding.Bottom = amountTable.DefaultCellPadding.Top = 5;
amountTable.DefaultCellTextInfo.FontSize = 10;
Section section = pdf.Sections[0];
section.Paragraphs.Add(amountTable);
Row row1AmountTable = amountTable.Rows.Add();
Aspose.Pdf.Generator.TextInfo tf1 = new Aspose.Pdf.Generator.TextInfo();
tf1.FontSize = 10;
tf1.Color = new Aspose.Pdf.Generator.Color(0, 0, 160);
tf1.FontName = "Helvetica-Bold";
row1AmountTable.Cells.Add("Subtotal",tf1);
row1AmountTable.Cells.Add(subTotal.ToString("c",culture));
Row row2AmountTable = amountTable.Rows.Add();
row2AmountTable.Cells.Add("Freight",tf1);
row2AmountTable.Cells.Add(freight.ToString("c",culture));
Row row3AmountTable = amountTable.Rows.Add();
row3AmountTable.Cells.Add("Total",tf1);
row3AmountTable.Cells.Add((subTotal+freight).ToString("c",culture));
}
}
}
|