-
Notifications
You must be signed in to change notification settings - Fork 203
/
ExcelToPDF.cs
410 lines (368 loc) · 17.1 KB
/
ExcelToPDF.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#region Copyright Syncfusion Inc. 2001-2024.
// Copyright Syncfusion Inc. 2001-2024. 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 Syncfusion.XlsIO;
using Syncfusion.XlsIORenderer;
using System.IO;
using System.Collections.Generic;
using System.Reflection;
using Syncfusion.Pdf;
using Syncfusion.iOS.Buttons;
#if __UNIFIED__
using Foundation;
using UIKit;
using CoreGraphics;
#else
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using MonoTouch.CoreGraphics;
using CGRect = System.Drawing.RectangleF;
using CGPoint = System.Drawing.PointF;
using CGSize = System.Drawing.SizeF;
#endif
namespace SampleBrowser
{
public class ExcelToPDF : SampleView
{
CGRect frameRect = new CGRect();
float frameMargin = 8.0f;
private readonly IList<string> layoutList = new List<string>();
string selectedLayout;
UILabel label ;
UILabel pageSetuplabel;
UIButton layoutDoneButton;
UIButton layoutButton;
UIPickerView layoutPicker;
UIButton inputButton ;
UIButton convertButton;
UILabel SubstituteLabel;
UILabel FontStreamLabel;
UILabel FontNameLabel;
SfCheckBox checkfontName;
SfCheckBox checkfontStream;
public ExcelToPDF()
{
this.layoutList.Add((NSString)"NoScaling");
this.layoutList.Add((NSString)"FitAllRowsOnOnePage");
this.layoutList.Add((NSString)"FitAllColumnsOnOnePage");
this.layoutList.Add((NSString)"FitSheetOnOnePage");
label = new UILabel ();
inputButton = new UIButton(UIButtonType.System);
inputButton.TouchUpInside += OnButtonClicked;
convertButton = new UIButton(UIButtonType.System);
convertButton.TouchUpInside += OnConvertClicked;
layoutDoneButton = new UIButton();
layoutButton = new UIButton();
layoutPicker = new UIPickerView();
pageSetuplabel = new UILabel();
layoutPicker = new UIPickerView();
layoutDoneButton = new UIButton();
SubstituteLabel = new UILabel();
FontStreamLabel = new UILabel();
FontNameLabel = new UILabel();
checkfontName = new SfCheckBox();
checkfontStream = new SfCheckBox();
this.AddSubview(label);
this.AddSubview(pageSetuplabel);
this.AddSubview(layoutButton);
this.AddSubview(layoutPicker);
this.AddSubview(layoutDoneButton);
this.AddSubview(SubstituteLabel);
this.AddSubview(checkfontName);
this.AddSubview(FontNameLabel);
this.AddSubview(checkfontStream);
this.AddSubview(FontStreamLabel);
this.AddSubview(convertButton);
this.AddSubview(inputButton);
}
void LoadAllowedTextsLabel()
{
#region Description Label
label.Frame = frameRect;
label.TextColor = UIColor.FromRGB (38/255.0f, 38/255.0f, 38/255.0f);
label.Text = "This sample illustrates the conversion of a Excel document to PDF.";
label.Font = UIFont.SystemFontOfSize(15);
label.Lines = 0;
label.LineBreakMode = UILineBreakMode.WordWrap;
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
label.Font = UIFont.SystemFontOfSize(18);
label.Frame = new CGRect(5, 5, frameRect.Location.X + frameRect.Size.Width , 35);
}
else
{
label.Frame = new CGRect(5, 5, frameRect.Location.X + frameRect.Size.Width, 50);
}
#endregion
#region Page Setup Options Label
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
pageSetuplabel.Font = UIFont.SystemFontOfSize(18);
pageSetuplabel.Frame = new CGRect(10, 45, frameRect.Location.X + frameRect.Size.Width - 20, 50);
layoutButton.Frame = new CGRect(10, 95, frameRect.Location.X + frameRect.Size.Width - 20, 50);
}
else
{
pageSetuplabel.Frame = new CGRect(10, 50, frameRect.Location.X + frameRect.Size.Width - 20, 50);
layoutButton.Frame = new CGRect(10, 100, frameRect.Location.X + frameRect.Size.Width - 20, 50);
}
//filter Label
pageSetuplabel.TextColor = UIColor.Black;
pageSetuplabel.BackgroundColor = UIColor.Clear;
pageSetuplabel.Text = "Page Setup Options :";
pageSetuplabel.TextAlignment = UITextAlignment.Left;
pageSetuplabel.Font = UIFont.FromName("Helvetica", 16f);
//filter button
layoutButton.SetTitle("NoScaling", UIControlState.Normal);
layoutButton.HorizontalAlignment = UIControlContentHorizontalAlignment.Center;
layoutButton.BackgroundColor = UIColor.Clear;
layoutButton.SetTitleColor(UIColor.Black, UIControlState.Normal);
layoutButton.Hidden = false;
layoutButton.Layer.BorderColor = UIColor.FromRGB(246, 246, 246).CGColor;
layoutButton.Layer.BorderWidth = 4;
layoutButton.Layer.CornerRadius = 8;
layoutButton.Font = UIFont.FromName("Helvetica", 16f);
layoutButton.TouchUpInside += ShowFilterPicker;
#endregion
#region Layout Picker
//filterpicker
PickerModel filterPickermodel = new PickerModel(this.layoutList);
filterPickermodel.PickerChanged += (sender, e) =>
{
this.selectedLayout = e.SelectedValue;
layoutButton.SetTitle(selectedLayout, UIControlState.Normal);
};
layoutPicker.ShowSelectionIndicator = true;
layoutPicker.Hidden = true;
layoutPicker.Model = filterPickermodel;
layoutPicker.BackgroundColor = UIColor.White;
layoutDoneButton.SetTitle("Done\t", UIControlState.Normal);
layoutDoneButton.HorizontalAlignment = UIControlContentHorizontalAlignment.Right;
layoutDoneButton.BackgroundColor = UIColor.FromRGB(240, 240, 240);
layoutDoneButton.SetTitleColor(UIColor.Black, UIControlState.Normal);
layoutDoneButton.Hidden = true;
layoutDoneButton.TouchUpInside += HideFilterPicker;
layoutPicker.Frame = new CGRect(0, this.Frame.Size.Height / 4 + 20, this.Frame.Size.Width, this.Frame.Size.Height / 3);
layoutDoneButton.Frame = new CGRect(0, this.Frame.Size.Height / 4 - 20, this.Frame.Size.Width, 40);
#endregion
////UI Substitute button
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
SubstituteLabel.Font = UIFont.SystemFontOfSize(16);
SubstituteLabel.Frame = new CGRect(10, 170, frameRect.Location.X + frameRect.Size.Width - 20, 50);
}
else
{
SubstituteLabel.Frame = new CGRect(10, 175, frameRect.Location.X + frameRect.Size.Width - 20, 50);
}
//filter Label
SubstituteLabel.TextColor = UIColor.Black;
SubstituteLabel.BackgroundColor = UIColor.Clear;
SubstituteLabel.Text = "Substitute Fonts:";
SubstituteLabel.TextAlignment = UITextAlignment.Left;
SubstituteLabel.Font = UIFont.FromName("Helvetica", 16f);
///UI CheckBox
checkfontName.SetTitle("Font Name", UIControlState.Normal);
checkfontName.SetTitleColor(UIColor.Black, UIControlState.Normal);
checkfontName.Frame = new CGRect(10, 205, frameRect.Location.X + frameRect.Size.Width - 20, 50);
////UI Font Name
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
FontNameLabel.Font = UIFont.SystemFontOfSize(18);
FontNameLabel.Frame = new CGRect(10, 225, frameRect.Location.X + frameRect.Size.Width - 20, 50);
}
else
{
FontNameLabel.Frame = new CGRect(10, 230, frameRect.Location.X + frameRect.Size.Width - 20, 50);
}
//filter Label
FontNameLabel.TextColor = UIColor.Black;
FontNameLabel.BackgroundColor = UIColor.Clear;
FontNameLabel.Text = "Missing fonts in the device will be substituted to Calibri.";
FontNameLabel.TextAlignment = UITextAlignment.Left;
FontNameLabel.Font = UIFont.FromName("Helvetica", 9f);
///UI CheckBox
checkfontStream.SetTitle("Font stream", UIControlState.Normal);
checkfontStream.SetTitleColor(UIColor.Black, UIControlState.Normal);
checkfontStream.Frame = new CGRect(10, 255, frameRect.Location.X + frameRect.Size.Width - 20, 50);
////UI Font Stream
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
FontStreamLabel.Font = UIFont.SystemFontOfSize(18);
FontStreamLabel.Frame = new CGRect(10, 270, frameRect.Location.X + frameRect.Size.Width - 20, 50);
}
else
{
FontStreamLabel.Frame = new CGRect(10, 275, frameRect.Location.X + frameRect.Size.Width - 20, 50);
}
//filter Label
FontStreamLabel.TextColor = UIColor.Black;
FontStreamLabel.BackgroundColor = UIColor.Clear;
FontStreamLabel.Text = "Missing fonts in the device will be substituted from embedded resource.";
FontStreamLabel.TextAlignment = UITextAlignment.Left;
FontStreamLabel.Font = UIFont.FromName("Helvetica", 9f);
#region Input Template Button
//button
inputButton.SetTitle("Input Template", UIControlState.Normal);
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
inputButton.Frame = new CGRect(0, 320, frameRect.Location.X + frameRect.Size.Width, 10);
}
else
{
inputButton.Frame = new CGRect(0, 325, frameRect.Location.X + frameRect.Size.Width, 10);
}
#endregion
#region Convert Button
convertButton.SetTitle("Convert",UIControlState.Normal);
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
{
convertButton.Frame = new CGRect(0, 345, frameRect.Location.X + frameRect.Size.Width, 10);
}
else
{
convertButton.Frame = new CGRect(0, 350, frameRect.Location.X + frameRect.Size.Width, 10);
}
#endregion
}
void OnButtonClicked(object sender, EventArgs e)
{
string resourcePath;
//Load the input template from assembly.
if (this.checkfontName.IsChecked.Value || this.checkfontStream.IsChecked.Value)
resourcePath = "SampleBrowser.Samples.XlsIO.Template.InvoiceTemplate.xlsx";
else
resourcePath = "SampleBrowser.Samples.XlsIO.Template.ExcelToPDF.xlsx";
Assembly assembly = Assembly.GetExecutingAssembly();
Stream fileStream = assembly.GetManifestResourceStream(resourcePath);
//Create a new memory stream.
MemoryStream stream = new MemoryStream();
//Copy input template to newly created memory stream.
fileStream.CopyTo(stream);
stream.Position = 0;
SaveAndView(stream, "application/msexcel");
}
void OnConvertClicked(object sender, EventArgs e)
{
//Instantiate excel engine
ExcelEngine excelEngine = new ExcelEngine();
//Excel application
IApplication application = excelEngine.Excel;
string resourcePath;
//Load the input template from assembly.
if (this.checkfontName.IsChecked.Value || this.checkfontStream.IsChecked.Value)
{
application.SubstituteFont += new Syncfusion.XlsIO.Implementation.SubstituteFontEventHandler(SubstituteFont);
resourcePath = "SampleBrowser.Samples.XlsIO.Template.InvoiceTemplate.xlsx";
}
else
resourcePath = "SampleBrowser.Samples.XlsIO.Template.ExcelToPDF.xlsx";
Assembly assembly = Assembly.GetExecutingAssembly();
Stream fileStream = assembly.GetManifestResourceStream(resourcePath);
//Open Workbook
IWorkbook workbook = application.Workbooks.Open(fileStream);
XlsIORenderer renderer = new XlsIORenderer();
XlsIORendererSettings settings = new XlsIORendererSettings();
settings.IsConvertBlankPage = false;
int index = layoutList.IndexOf(selectedLayout);
if (index == 0)
settings.LayoutOptions = LayoutOptions.NoScaling;
else if (index == 1)
settings.LayoutOptions = LayoutOptions.FitAllRowsOnOnePage;
else if (index == 2)
settings.LayoutOptions = LayoutOptions.FitAllColumnsOnOnePage;
else
settings.LayoutOptions = LayoutOptions.FitSheetOnOnePage;
PdfDocument pdfDocument = renderer.ConvertToPDF(workbook, settings);
MemoryStream memoryStream = new MemoryStream();
pdfDocument.Save(memoryStream);
pdfDocument.Close(true);
workbook.Close();
excelEngine.Dispose();
//Save and view the generated file.
SaveAndView(memoryStream, "application/pdf");
}
void SaveAndView(MemoryStream stream, string contentType)
{
if (stream != null)
{
stream.Position = 0;
SaveiOS iOSSave = new SaveiOS();
if (contentType == "application/pdf")
iOSSave.Save("ExcelToPDF.pdf", contentType, stream);
else
iOSSave.Save("Input Template.xlsx", contentType, stream);
}
}
void ShowFilterPicker(object sender, EventArgs e)
{
layoutDoneButton.Hidden = false;
layoutPicker.Hidden = false;
inputButton.Hidden = true;
convertButton.Hidden = true;
this.BecomeFirstResponder();
SubstituteLabel.Hidden = true;
FontNameLabel.Hidden = true;
FontStreamLabel.Hidden = true;
checkfontName.Hidden = true;
checkfontStream.Hidden = true;
}
void HideFilterPicker(object sender, EventArgs e)
{
layoutDoneButton.Hidden = true;
layoutPicker.Hidden = true;
inputButton.Hidden = false;
convertButton.Hidden = false;
this.BecomeFirstResponder();
layoutButton.Hidden = false;
SubstituteLabel.Hidden = false;
FontNameLabel.Hidden = false;
FontStreamLabel.Hidden = false;
checkfontName.Hidden = false;
checkfontStream.Hidden = false;
}
public override void LayoutSubviews ()
{
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad) {
frameMargin = 0.0f;
}
frameRect = Bounds;
frameRect.Location = new CGPoint (frameRect.Location.X + frameMargin, frameRect.Location.Y + 1.5f * frameMargin);
frameRect.Size = new CGSize(frameRect.Size.Width - (frameRect.Location.X + frameMargin), frameRect.Size.Height - (frameRect.Location.Y + 1.5f * frameMargin));
LoadAllowedTextsLabel ();
base.LayoutSubviews ();
}
private void SubstituteFont(object sender, Syncfusion.XlsIO.Implementation.SubstituteFontEventArgs args)
{
Assembly assembly = Assembly.GetExecutingAssembly();
if (checkfontName.IsChecked.Value && (args.OriginalFontName == "Bahnschrift Pro SemiBold" || args.OriginalFontName == "Georgia Pro Semibold"))
{
args.AlternateFontName = "Calibri";
}
if (checkfontStream.IsChecked.Value)
{
if (args.OriginalFontName == "Georgia Pro Semibold")
{
Stream file = assembly.GetManifestResourceStream("SampleBrowser.Samples.XlsIO.Template.georgiab.ttf");
MemoryStream memoryStream = new MemoryStream();
file.CopyTo(memoryStream);
file.Close();
args.AlternateFontStream = memoryStream;
}
else if (args.OriginalFontName == "Bahnschrift Pro SemiBold")
{
Stream file = assembly.GetManifestResourceStream("SampleBrowser.Samples.XlsIO.Template.bahnschrift.ttf");
MemoryStream memoryStream = new MemoryStream();
file.CopyTo(memoryStream);
file.Close();
args.AlternateFontStream = memoryStream;
}
}
}
}
}