forked from syncfusion/xamarin-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tables.cs
347 lines (326 loc) · 15.7 KB
/
Tables.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#region Copyright Syncfusion Inc. 2001-2019.
// Copyright Syncfusion Inc. 2001-2019. All rights reserved.
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// licensing@syncfusion.com. Any infringement will be prosecuted under
// applicable laws.
#endregion
using Syncfusion.Presentation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using COLOR = Syncfusion.Drawing;
using System.IO;
using System.Reflection;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System.Xml;
namespace SampleBrowser
{
public partial class TablesPresentation : SamplePage
{
private Context m_context;
public override View GetSampleContent(Context con)
{
LinearLayout linear = new LinearLayout(con);
linear.SetBackgroundColor(Android.Graphics.Color.White);
linear.Orientation = Orientation.Vertical;
linear.SetPadding(10, 10, 10, 10);
TextView text1 = new TextView(con);
text1.TextSize = 17;
text1.TextAlignment = TextAlignment.Center;
text1.SetTextColor(Android.Graphics.Color.ParseColor("#262626"));
text1.Text = "This sample demonstrates how to create a table with specified number of rows and columns in PowerPoint presentation for displaying tabular data.";
text1.SetPadding(5, 10, 10, 5);
linear.AddView(text1);
TextView space = new TextView(con);
space.TextSize = 10;
linear.AddView(space);
m_context = con;
TextView space2 = new TextView(con);
space2.TextSize = 10;
linear.AddView(space2);
Button button1 = new Button(con);
button1.Text = "Generate Presentation";
button1.Click += OnButtonClicked;
linear.AddView(button1);
return linear;
}
void OnButtonClicked(object sender, EventArgs e)
{
//Stream to save the created PowerPoint presnetation
MemoryStream stream = new MemoryStream();
//Creates a new instance of the presentation.
using (IPresentation presentation = Syncfusion.Presentation.Presentation.Create())
{
#region Slide1
//To add a slide to PowerPoint presentation
ISlide slide = presentation.Slides.Add(SlideLayoutType.TitleOnly);
//To set the table title in a slide
SetTableTitle(slide);
//To get the table data from an XML file
Dictionary<string, Dictionary<string, string>> products = LoadXMLData();
int columnCount = products.Keys.Count + 1;
int rowCount = products[products.Keys.ToArray()[0]].Count + 1;
//To add a new table in slide.
ITable table = slide.Shapes.AddTable(rowCount, columnCount, 61.92, 95.76, 856.8, 378.72);
//To set the style for the table.
table.BuiltInStyle = BuiltInTableStyle.MediumStyle2Accent6;
//To set category title
SetCategoryTitle(table);
//Iterates and sets the values to the table cells.
for (int rowIndex = 0; rowIndex < table.Rows.Count; rowIndex++)
{
IRow row = table.Rows[rowIndex];
Dictionary<string, string> months = products[products.Keys.ToArray()[0]];
string[] monthName = months.Keys.ToArray();
for (int cellIndex = 0; cellIndex < row.Cells.Count - 1; cellIndex++)
{
months = products[products.Keys.ToArray()[cellIndex]];
AddHeaderRowAndColumn(row, cellIndex, products.Keys.ToArray(), rowIndex, monthName);
AddCellContent(row, rowIndex, monthName, months, cellIndex);
}
}
#endregion
//Save the presentation instance to the memory stream.
presentation.Save(stream);
}
stream.Position = 0;
if (stream != null)
{
SaveAndroid androidSave = new SaveAndroid ();
androidSave.Save ("Tables.pptx", "application/powerpoint", stream, m_context);
}
}
#region HelperMethods
/// <summary>
/// Loads the xml content to fill the table cells in the presentation.
/// </summary>
/// <returns></returns>
private Dictionary<string, Dictionary<string, string>> LoadXMLData()
{
Dictionary<string, Dictionary<string, string>> Products = new Dictionary<string, Dictionary<string, string>>();
Assembly assembly = Assembly.GetExecutingAssembly();
Stream productXMLStream = assembly.GetManifestResourceStream("SampleBrowser.Samples.Presentation.Templates.TableData.xml");
XmlReader reader = XmlReader.Create(productXMLStream);
string serailNo = string.Empty;
string productName = string.Empty;
string sum = string.Empty;
string month;
while (reader.Read())
{
if (reader.IsStartElement())
{
switch (reader.Name)
{
case "Products":
Dictionary<string, string> Month_Value = new Dictionary<string, string>();
while (reader.Read())
{
if (reader.IsStartElement())
{
switch (reader.Name)
{
case "Product":
while (reader.Read())
{
if (reader.IsStartElement())
{
switch (reader.Name)
{
case "Month":
while (reader.Read())
{
month = reader.Name;
if (reader.IsStartElement())
{
month = reader.Name;
while (reader.Read())
{
if (reader.IsStartElement())
{
if (reader.Name == "Value")
{
reader.Read();
reader.MoveToContent();
Month_Value.Add(month, reader.Value);
}
}
else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == month)
{
break;
}
}
}
else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "Month")
{
break;
}
}
break;
case "ProductName":
reader.Read();
reader.MoveToContent();
productName = reader.Value;
break;
}
}
else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "Product")
{
break;
}
}
break;
}
Products.Add(productName, Month_Value);
Month_Value = new Dictionary<string, string>();
}
else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "Products")
break;
}
break;
}
}
}
return Products;
}
/// <summary>
/// Sets the table title.
/// </summary>
/// <param name="slide">Represents the slide instance of the presentation.</param>
private void SetTableTitle(ISlide slide)
{
IShape shape = slide.Shapes[0] as IShape;
shape.Left = 84.24;
shape.Top = 0;
shape.Width = 792;
shape.Height = 126.72;
ITextBody textFrame = shape.TextBody;
IParagraphs paragraphs = textFrame.Paragraphs;
paragraphs.Add();
IParagraph paragraph = paragraphs[0];
paragraph.HorizontalAlignment = HorizontalAlignmentType.Center;
//Instance to hold textparts in paragraph.
ITextParts textParts = paragraph.TextParts;
textParts.Add();
ITextPart textPart = textParts[0];
textPart.Text = "Target ";
IFont font = textPart.Font;
font.FontName = "Arial";
font.FontSize = 28;
font.Bold = true;
font.CapsType = TextCapsType.All;
textParts.Add();
//Creates a textpart and assigns value to it.
textPart = textParts[1];
textPart.Text = "Vs ";
font = textPart.Font;
font.FontName = "Arial";
font.FontSize = 18;
textParts.Add();
//Creates a textpart and assigns value to it.
textPart = textParts[2];
textPart.Text = "PERFORMANCE";
font = textPart.Font;
font.FontName = "Arial";
font.FontSize = 28;
font.Bold = true;
}
/// <summary>
/// Adds the cell content to the table.
/// </summary>
/// <param name="row">Represents the instance of row.</param>
/// <param name="rowIndex">Represents the row index.</param>
/// <param name="monthName">Represnets the array of month name.</param>
/// <param name="months">Represnets the dictionary of months and its values.</param>
/// <param name="cellIndex">Represents the cell index.</param>
private void AddCellContent(IRow row, int rowIndex, string[] monthName, Dictionary<string, string> months, int cellIndex)
{
if (rowIndex == 0) return;
ICell cell = row.Cells[cellIndex + 1];
//Instance to hold paragraphs in cell.
IParagraphs paragraphs = cell.TextBody.Paragraphs;
paragraphs.Add();
IParagraph paragraph = paragraphs[0];
paragraph.HorizontalAlignment = HorizontalAlignmentType.Left;
ITextParts textParts = paragraph.TextParts;
textParts.Add();
//Creates a textpart and assigns value to it.
ITextPart textPart = textParts[0];
textPart.Text = months[monthName[rowIndex - 1]];
IFont font = textPart.Font;
font.FontName = "Arial";
font.FontSize = 14;
}
/// <summary>
/// Adds the content for the row and column for the table.
/// </summary>
/// <param name="row">Represents the particular row.</param>
/// <param name="cellIndex">Represents the index of the cell.</param>
/// <param name="cellContent">Represents the cell content.</param>
/// <param name="rowIndex">Represents the index of the row.</param>
/// <param name="monthName">Represents the content of monthname for the table.</param>
private void AddHeaderRowAndColumn(IRow row, int cellIndex, string[] cellContent, int rowIndex, string[] monthName)
{
//To set text alignment type inside cell
ICell cell = null;
if (rowIndex == 0)
cell = row.Cells[cellIndex + 1];
else
cell = row.Cells[0];
cell.TextBody.VerticalAlignment = VerticalAlignmentType.Middle;
//To add a paragraph inside cell
IParagraphs paragraphs = cell.TextBody.Paragraphs;
if (paragraphs.Count == 0)
paragraphs.Add();
IParagraph paragraph = paragraphs[0];
paragraph.HorizontalAlignment = HorizontalAlignmentType.Center;
ITextParts textParts = paragraph.TextParts;
if (textParts.Count == 0)
textParts.Add();
//Creates a textpart and assigns value to it.
ITextPart textPart = textParts[0];
if (rowIndex == 0)
textPart.Text = cellContent[cellIndex];
else
textPart.Text = monthName[rowIndex - 1];
IFont font = textPart.Font;
font.FontName = "Arial";
font.FontSize = 14;
font.Bold = true;
}
/// <summary>
/// Sets the title for the category in the table.
/// </summary>
/// <param name="table">Instance to access the table from the presentation.</param>
void SetCategoryTitle(ITable table)
{
//Instance to hold rows in the table
table.Rows[0].Height = 81.44;
//To set text alignment type inside cell
//ICell cell11 = ;
table.Rows[0].Cells[0].TextBody.VerticalAlignment = VerticalAlignmentType.Middle;
//To add a paragraph inside cell
IParagraphs paragraphs = table.Rows[0].Cells[0].TextBody.Paragraphs;
paragraphs.Add();
IParagraph paragraph = paragraphs[0];
paragraph.HorizontalAlignment = HorizontalAlignmentType.Center;
ITextParts textParts = paragraph.TextParts;
textParts.Add();
//Creates a textpart and assigns value to it.
ITextPart textPart = textParts[0];
textPart.Text = "Month";
IFont font = textPart.Font;
font.FontName = "Arial";
font.FontSize = 14;
font.Bold = true;
}
#endregion HelperMethods
}
}