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
|
/////////////////////////////////////////////////////////////////////////
// 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 SalesByYear.
/// </summary>
public class SalesByYear
{
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");
private string queryString = "SELECT DISTINCTROW Orders.ShippedDate, Orders.OrderID, "+
"[Order Subtotals].Subtotal, Format([ShippedDate],'yyyy') AS [Year] "+
"FROM Orders INNER JOIN [Order Subtotals] ON Orders.OrderID = [Order Subtotals].OrderID " +
"WHERE (((Orders.ShippedDate) Is Not Null))";
public SalesByYear(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 GetSalesByYear()
{
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\\SalesByYear.xml";
pdf.BindXML(xmlFile, null);
Section section = pdf.Sections["section1"];
int year;
for(year=1996;year<=1998;year++)
{
PutYear(pdf,year);
}
this.oleDbConnection.Close();
return pdf;
}
private void PutYear(Aspose.Pdf.Generator.Pdf pdf, int year)
{
PutSummary(pdf,year);
PutDetail(pdf,year);
}
private void PutSummary(Aspose.Pdf.Generator.Pdf pdf, int year)
{
int[] orders = new int[4];
float[] sales = new float[4];
int ordersTotal = 0;
float salesTotal = 0F;
int i;
for(i=1;i<=4;i++)
{
this.oleDbSelectCommand1.CommandText = "SELECT COUNT(*),SUM([Order Subtotals].Subtotal) FROM (" + queryString +
") WHERE Year(Orders.ShippedDate)='"+ year.ToString() +
"' AND DatePart('q',Orders.ShippedDate)=" + i.ToString() + ";";
resultReader = this.oleDbSelectCommand1.ExecuteReader();
if(resultReader.Read())
{
orders[i-1] = resultReader.IsDBNull(0)?0:Convert.ToInt32(resultReader.GetValue(0));
ordersTotal += orders[i-1];
sales[i-1] = resultReader.IsDBNull(1)?0:(float)Convert.ToDouble(resultReader.GetValue(1));
salesTotal += sales[i-1];
}
resultReader.Close();
}
this.oleDbConnection.Close();
Section section = pdf.Sections[0];
Graph g1 = new Graph(section,480,10);
section.Paragraphs.Add(g1);
if(year != 1996)
g1.IsFirstParagraph = true;
else
g1.Margin.Top = 20;
float[] linePosArr = new float[]{0,5,480,5};
Line l1 = new Line(g1,linePosArr);
g1.Shapes.Add(l1);
l1.GraphInfo.LineWidth = 2;
l1.GraphInfo.Color = new Aspose.Pdf.Generator.Color(204);
Aspose.Pdf.Generator.Text title = new Aspose.Pdf.Generator.Text(section, year.ToString() + " Summary");
section.Paragraphs.Add(title);
title.Segments[0].TextInfo.FontName = "Times-BoldItalic";
title.Segments[0].TextInfo.FontSize = 14;
Table table = new Table();
table.Margin.Top = 10;
table.Margin.Left = 80;
table.ColumnWidths = "120 120 120";
table.DefaultCellTextInfo.FontSize = 10;
table.DefaultCellPadding.Top = table.DefaultCellPadding.Bottom = 4;
section.Paragraphs.Add(table);
AddSummaryRow(pdf,table,"Quarter:","Orders Shipped:","Sales:",0);
for(i=1;i<=4;i++)
{
if(orders[i-1] != 0)
AddSummaryRow(pdf,table,i.ToString(),orders[i-1].ToString(),
sales[i-1].ToString("c",locale),1);
}
AddSummaryRow(pdf,table,"Totals",ordersTotal.ToString(),salesTotal.ToString("c",locale),2);
Graph g2 = new Graph(section,480,10);
section.Paragraphs.Add(g2);
g2.Margin.Top = 20;
Line l2 = new Line(g2,linePosArr);
g2.Shapes.Add(l2);
l2.GraphInfo.LineWidth = 2;
l2.GraphInfo.Color = new Aspose.Pdf.Generator.Color(204);
}
private void AddSummaryRow(Aspose.Pdf.Generator.Pdf pdf, Table table, string s1, string s2, string s3, int type)
{
Row row1 = table.Rows.Add();
Cell cell1 = row1.Cells.Add(s1);
if(type == 0 || type == 2)
((Aspose.Pdf.Generator.Text)cell1.Paragraphs[0]).Segments[0].TextInfo.FontName = "Times-Bold";
((Aspose.Pdf.Generator.Text)cell1.Paragraphs[0]).TextInfo.Alignment = AlignmentType.Center;
Cell cell2 = row1.Cells.Add(s2);
if(type == 0)
((Aspose.Pdf.Generator.Text)cell2.Paragraphs[0]).Segments[0].TextInfo.FontName = "Times-Bold";
((Aspose.Pdf.Generator.Text)cell2.Paragraphs[0]).TextInfo.Alignment = AlignmentType.Center;
Cell cell3 = row1.Cells.Add(s3);
if(type == 0)
((Aspose.Pdf.Generator.Text)cell3.Paragraphs[0]).Segments[0].TextInfo.FontName = "Times-Bold";
((Aspose.Pdf.Generator.Text)cell3.Paragraphs[0]).TextInfo.Alignment = AlignmentType.Center;
}
private void PutDetail(Aspose.Pdf.Generator.Pdf pdf, int year)
{
Section section = pdf.Sections[0];
Graph g1 = new Graph(section,480,10);
section.Paragraphs.Add(g1);
g1.Margin.Top = 15;
float[] linePosArr = new float[]{0,5,480,5};
Line l1 = new Line(g1,linePosArr);
g1.Shapes.Add(l1);
l1.GraphInfo.LineWidth = 2;
Aspose.Pdf.Generator.Text title = new Aspose.Pdf.Generator.Text(year.ToString() + " Details");
section.Paragraphs.Add(title);
title.Segments[0].TextInfo.FontName = "Times-BoldItalic";
title.Segments[0].TextInfo.FontSize = 14;
Table table = new Table();
table.Margin.Top = 10;
table.Margin.Left = 80;
table.ColumnWidths = "100 100 100 80";
table.DefaultCellTextInfo.FontSize = 10;
table.DefaultCellPadding.Top = table.DefaultCellPadding.Bottom = 4;
section.Paragraphs.Add(table);
AddDetailRow(pdf,table,"Line Number:","Shipped Date:","OrderID:","Sales:",true);
this.oleDbConnection.Open();
this.oleDbSelectCommand1.CommandText = "SELECT * FROM (" + queryString +
") WHERE Year(Orders.ShippedDate)='"+ year.ToString() +
"';";
resultReader = this.oleDbSelectCommand1.ExecuteReader();
int i = 0;
string shippedDate,orderID;
float sales;
while(resultReader.Read())
{
i++;
shippedDate = Convert.ToDateTime(resultReader.GetValue(0)).ToString("d",DateTimeFormatInfo.InvariantInfo);
orderID = Convert.ToInt32(resultReader.GetValue(1)).ToString();
sales = (float)Convert.ToDouble(resultReader.GetValue(2));
AddDetailRow(pdf,table,i.ToString(),shippedDate,orderID,sales.ToString("c",locale),false);
}
resultReader.Close();
Graph g2 = new Graph(section,480,10);
section.Paragraphs.Add(g2);
g2.Margin.Top = 50;
Line l2 = new Line(g2,linePosArr);
g2.Shapes.Add(l2);
l2.GraphInfo.LineWidth = 2;
}
private void AddDetailRow(Aspose.Pdf.Generator.Pdf pdf, Aspose.Pdf.Generator.Table table, string s1, string s2, string s3, string s4, bool isHeader)
{
Row row1 = table.Rows.Add();
Cell cell1 = row1.Cells.Add(s1);
if(isHeader)
((Aspose.Pdf.Generator.Text)cell1.Paragraphs[0]).Segments[0].TextInfo.FontName = "Times-Bold";
((Aspose.Pdf.Generator.Text)cell1.Paragraphs[0]).TextInfo.Alignment = AlignmentType.Center;
Cell cell2 = row1.Cells.Add(s2);
if(isHeader)
((Aspose.Pdf.Generator.Text)cell2.Paragraphs[0]).Segments[0].TextInfo.FontName = "Times-Bold";
((Aspose.Pdf.Generator.Text)cell2.Paragraphs[0]).TextInfo.Alignment = AlignmentType.Center;
Cell cell3 = row1.Cells.Add(s3);
if(isHeader)
((Aspose.Pdf.Generator.Text)cell3.Paragraphs[0]).Segments[0].TextInfo.FontName = "Times-Bold";
((Aspose.Pdf.Generator.Text)cell3.Paragraphs[0]).TextInfo.Alignment = AlignmentType.Center;
Cell cell4 = row1.Cells.Add(s4);
if(isHeader)
((Aspose.Pdf.Generator.Text)cell4.Paragraphs[0]).Segments[0].TextInfo.FontName = "Times-Bold";
((Aspose.Pdf.Generator.Text)cell4.Paragraphs[0]).TextInfo.Alignment = AlignmentType.Right;
}
}
}
|