forked from syncfusion/xamarin-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TableFeatures.cs
381 lines (331 loc) · 14.9 KB
/
TableFeatures.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#region Copyright Syncfusion Inc. 2001-2022.
// Copyright Syncfusion Inc. 2001-2022. 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
// [email protected]. Any infringement will be prosecuted under
// applicable laws.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Syncfusion.Pdf.Grid;
using System.IO;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Tables;
using Syncfusion.Pdf;
using Syncfusion.Drawing;
using System.Reflection;
using System.Xml.Linq;
using System.Xml;
using Syncfusion.Pdf.Interactive;
namespace SampleBrowser
{
public partial class TableFeatures : SamplePage
{
private Context m_context;
PdfPen borderPen;
PdfPen transparentPen;
float cellSpacing = 7f;
float margin = 40f;
PdfFont smallFont;
PdfLightTable pdfLightTable = null;
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 space = new TextView(con);
space.TextSize = 10;
linear.AddView(space);
TextView text2 = new TextView(con);
text2.TextSize = 17;
text2.TextAlignment = TextAlignment.Center;
text2.Text = "This sample demonstrates how to create borderless tables in a PDF document.";
text2.SetTextColor(Android.Graphics.Color.ParseColor("#262626"));
text2.SetPadding(5, 5, 5, 5);
linear.AddView(text2);
TextView space1 = new TextView(con);
space1.TextSize = 10;
linear.AddView(space1);
m_context = con;
TextView space2 = new TextView(con);
space2.TextSize = 10;
linear.AddView(space2);
Button button1 = new Button(con);
button1.Text = "Generate PDF";
button1.Click += OnButtonClicked;
linear.AddView(button1);
return linear;
}
void OnButtonClicked(object sender, EventArgs e)
{
#region Field Definitions
IEnumerable<TableProducts> products = DataProvider.GetProducts();
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 8f);
smallFont = new PdfStandardFont(font, 5f);
PdfFont bigFont = new PdfStandardFont(font, 16f);
PdfBrush orangeBrush = new PdfSolidBrush(new PdfColor(247, 148, 29));
PdfBrush grayBrush = new PdfSolidBrush(new PdfColor(170, 171, 171));
borderPen = new PdfPen(PdfBrushes.DarkGray, .3f);
borderPen.LineCap = PdfLineCap.Square;
transparentPen = new PdfPen(PdfBrushes.Transparent, .3f);
transparentPen.LineCap = PdfLineCap.Square;
# endregion
PdfDocument document = new PdfDocument();
document.PageSettings.Margins.All = 0;
# region Footer
PdfPageTemplateElement footer = new PdfPageTemplateElement(new RectangleF(new PointF(0, document.PageSettings.Height - 40), new SizeF(document.PageSettings.Width, 40)));
footer.Graphics.DrawRectangle(new PdfSolidBrush(new PdfColor(77, 77, 77)), footer.Bounds);
footer.Graphics.DrawString("http://www.syncfusion.com", font, grayBrush, new PointF(footer.Width - (footer.Width / 4), 15));
footer.Graphics.DrawString("Copyright © 2001 - 2017 Syncfusion Inc.", font, grayBrush, new PointF(0, 15));
document.Template.Bottom = footer;
# endregion
PdfPage page = document.Pages.Add();
page.Graphics.DrawRectangle(orangeBrush, new RectangleF(PointF.Empty, new SizeF(page.Graphics.ClientSize.Width, margin)));
page.Graphics.DrawString("Essential Studio File Formats Edition", bigFont, PdfBrushes.White, new PointF(10, 10));
# region PdfLightTable
pdfLightTable = new PdfLightTable();
pdfLightTable.DataSource = products;
pdfLightTable.Style.DefaultStyle.BorderPen = transparentPen;
for (int i = 0; i < pdfLightTable.Columns.Count; i++)
{
if (i % 2 == 0)
pdfLightTable.Columns[i].Width = 16.5f;
}
pdfLightTable.Style.CellSpacing = cellSpacing;
pdfLightTable.BeginRowLayout += new BeginRowLayoutEventHandler(pdfLightTable_BeginRowLayout);
pdfLightTable.BeginCellLayout += new BeginCellLayoutEventHandler(pdfLightTable_BeginCellLayout);
pdfLightTable.Style.DefaultStyle.Font = font;
PdfLayoutResult result = pdfLightTable.Draw(page, new RectangleF(new PointF(margin, 70), new SizeF(page.Graphics.ClientSize.Width - (2 * margin), page.Graphics.ClientSize.Height - margin)));
# endregion
page.Graphics.DrawString("What You Get with Syncfusion", bigFont, orangeBrush, new PointF(margin, result.Bounds.Bottom + 50));
# region PdfGrid
PdfGrid pdfGrid = new PdfGrid();
IEnumerable<Report> report = DataProvider.GetReport();
pdfGrid.DataSource = report;
pdfGrid.Headers.Clear();
pdfGrid.Columns[0].Width = 80;
pdfGrid.Style.Font = font;
pdfGrid.Style.CellSpacing = 15f;
for (int i = 0; i < pdfGrid.Rows.Count; i++)
{
if (i % 2 == 0)
{
PdfGridCell cell = pdfGrid.Rows[i].Cells[0];
cell.RowSpan = 2;
cell.Value = "";
cell = pdfGrid.Rows[i].Cells[1];
cell.Style.Font = bigFont;
}
for (int j = 0; j < pdfGrid.Columns.Count; j++)
{
pdfGrid.Rows[i].Cells[j].Style.Borders.All = transparentPen;
}
}
PdfGridLayoutFormat gridLayoutFormat = new PdfGridLayoutFormat();
gridLayoutFormat.Layout = PdfLayoutType.Paginate;
pdfGrid.Draw(page, new RectangleF(new PointF(margin, result.Bounds.Bottom + 75), new SizeF(page.Graphics.ClientSize.Width - (2 * margin), page.Graphics.ClientSize.Height - margin)), gridLayoutFormat);
#endregion
MemoryStream stream = new MemoryStream();
document.Save(stream);
document.Close(true);
if (stream != null)
{
SaveAndroid androidSave = new SaveAndroid();
androidSave.Save("TableFeatures.pdf", "application/pdf", stream, m_context);
}
}
#region PdfLightTable Events
void pdfLightTable_BeginRowLayout(object sender, BeginRowLayoutEventArgs args)
{
if (args.RowIndex % 2 == 0)
args.MinimalHeight = 20;
else
args.MinimalHeight = 30;
}
void pdfLightTable_BeginCellLayout(object sender, BeginCellLayoutEventArgs args)
{
if (args.RowIndex > -1 && args.CellIndex > -1)
{
float x = args.Bounds.X;
float y = args.Bounds.Y;
float width = args.Bounds.Right;
float height = args.Bounds.Bottom;
if (args.Value == "dummy")
{
args.Skip = true;
return;
}
if (args.CellIndex % 2 == 0 && !string.IsNullOrEmpty(args.Value))
{
args.Skip = true;
}
if (args.Value.Contains("http"))
{
args.Skip = true;
// Create the Text Web Link
PdfTextWebLink textLink = new PdfTextWebLink();
textLink.Url = args.Value;
textLink.Text = "Know more...";
textLink.Brush = PdfBrushes.Black;
textLink.Font = smallFont;
textLink.DrawTextWebLink(args.Graphics, new PointF(args.Bounds.X + 2 * args.Bounds.Width / 3, args.Bounds.Y));
}
# region Draw manual borders
if (args.RowIndex % 3 == 0)//top
{
if (args.CellIndex % 2 == 0)
width += cellSpacing;
args.Graphics.DrawLine(borderPen, new PointF(x, y), new PointF(width, y));
}
else if (args.RowIndex % 3 == 2)//bottom
{
if (args.CellIndex % 2 == 0)
width += cellSpacing;
args.Graphics.DrawLine(borderPen, new PointF(x, height), new PointF(width, height));
}
if (args.CellIndex % 2 == 0)//left
{
if (args.RowIndex % 3 != 2)
height += cellSpacing;
args.Graphics.DrawLine(borderPen, new PointF(x, y), new PointF(x, height));
}
else if (args.CellIndex % 2 != 0)//right
{
if (args.RowIndex % 3 != 2)
height += cellSpacing;
args.Graphics.DrawLine(borderPen, new PointF(width, y), new PointF(width, height));
}
# endregion
}
}
# endregion
}
internal sealed class DataProvider
{
public static IEnumerable<TableProducts> GetProducts()
{
Stream xmlStream = typeof(TableFeatures).GetTypeInfo().Assembly.GetManifestResourceStream("SampleBrowser.Samples.PDF.Assets.Products.xml");
XmlReader reader = XmlReader.Create(xmlStream);
List<TableProducts> collection = new List<TableProducts>();
while (reader.Read())
{
if (reader.IsStartElement())
{
switch (reader.Name)
{
case "Products":
TableProducts product = new TableProducts();
while (reader.Read())
{
if (reader.IsStartElement())
{
switch (reader.Name)
{
case "Image1":
reader.Read();
product.Image1 = reader.Value;
break;
case "Description1":
reader.Read();
product.Description1 = reader.Value;
break;
case "Image2":
reader.Read();
product.Image2 = reader.Value;
break;
case "Description2":
reader.Read();
product.Description2 = reader.Value;
break;
case "Description3":
reader.Read();
product.Description3 = reader.Value;
break;
case "Image3":
reader.Read();
product.Image3 = reader.Value;
break;
}
}
else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "Products")
{
collection.Add(product);
break;
}
}
break;
}
}
}
return collection.AsEnumerable();
}
public static IEnumerable<Report> GetReport()
{
Stream xmlStream = typeof(TableFeatures).GetTypeInfo().Assembly.GetManifestResourceStream("SampleBrowser.Samples.PDF.Assets.Report.xml");
XmlReader reader = XmlReader.Create(xmlStream);
List<Report> collection = new List<Report>();
while (reader.Read())
{
if (reader.IsStartElement())
{
switch (reader.Name)
{
case "Report":
Report report = new Report();
while (reader.Read())
{
if (reader.IsStartElement())
{
switch (reader.Name)
{
case "Image":
reader.Read();
report.Image = reader.Value;
break;
case "Description":
reader.Read();
report.Description = reader.Value;
break;
}
}
else if (reader.NodeType == XmlNodeType.EndElement && reader.Name == "Report")
{
collection.Add(report);
break;
}
}
break;
}
}
}
return collection.AsEnumerable();
}
}
#region Products
public class TableProducts
{
public string Image1 { get; set; }
public string Description1 { get; set; }
public string Image2 { get; set; }
public string Description2 { get; set; }
public string Image3 { get; set; }
public string Description3 { get; set; }
}
#endregion
#region Report
public class Report
{
public string Image { get; set; }
public string Description { get; set; }
}
#endregion
}