-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainPage.xaml.cs
More file actions
187 lines (173 loc) · 7.56 KB
/
MainPage.xaml.cs
File metadata and controls
187 lines (173 loc) · 7.56 KB
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
using Syncfusion.Maui.PdfViewer;
using System.Globalization;
using System.Reflection;
using System.Text.RegularExpressions;
namespace PdfFormFillingExample
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
// Load the PDF document
Stream? stream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("PdfFormFillingExample.Assets.workshop_registration.pdf");
pdfViewer.LoadDocument(stream);
}
/// <summary>
/// Handle focus change on the DOB field to display DatePicker
/// </summary>
private void pdfViewer_FormFieldFocusChanged(object sender, FormFieldFocusChangedEvenArgs e)
{
// Check whether the focused field is the date of birth field and let the user choose a date.
if (e.FormField.Name == "dob")
{
if (e.HasFocus)
{
datePickerGrid.IsVisible = true;
// Hide the soft keypad shown when clicking the text form field on Android and iOS devices.
#if ANDROID
if(this.Handler.PlatformView is Android.Views.View inputView)
{
using (var inputMethodManager = (Android.Views.InputMethods.InputMethodManager?)inputView.Context?
.GetSystemService(Android.Content.Context.InputMethodService))
{
if (inputMethodManager != null)
{
var token = Platform.CurrentActivity?.CurrentFocus?.WindowToken;
inputMethodManager.HideSoftInputFromWindow(token, Android.Views
.InputMethods.HideSoftInputFlags.None);
Platform.CurrentActivity?.Window?.DecorView.ClearFocus();
}
}
}
#elif IOS && !MACCATALYST
UIKit.UIApplication.SharedApplication.SendAction(new ObjCRuntime.Selector("resignFirstResponder"), null, null, null);
#endif
}
}
}
/// <summary>
/// Handle DatePicker's Ok button action
/// </summary>
private void datePicker_OkButtonClicked(object sender, EventArgs e)
{
FormField field = pdfViewer.FormFields.Where(f => f.Name == "dob").First();
if (field is TextFormField dateOfBirthField)
{
// Remove time and retain only the date from the selected DateTime object.
string date = datePicker.SelectedDate.ToString().Split(' ')[0];
// Convert the date to dd/mm/yyyy format
string[] dateComponents = date.Split("/");
date = $"{dateComponents[1]}/{dateComponents[0]}/{dateComponents[2]}";
dateOfBirthField.Text = date;
}
datePickerGrid.IsVisible = false;
}
/// <summary>
/// Handle DatePicker's Cancel button action
/// </summary>
private void datePicker_CancelButtonClicked(object sender, EventArgs e)
{
datePickerGrid.IsVisible = false;
}
/// <summary>
/// Share the PDF form externally via platform's share dialog
/// </summary>
private async void shareButton_Clicked(object sender, EventArgs e)
{
bool isFormDataValid = await ValidateFormData();
if (isFormDataValid)
{
MemoryStream outputStream = new MemoryStream();
await pdfViewer.SaveDocumentAsync(outputStream);
string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "workshop_registration.pdf");
File.WriteAllBytes(filePath, outputStream.ToArray());
await Share.Default.RequestAsync(new ShareFileRequest
{
Title = "Share filled form",
File = new ShareFile(filePath)
});
}
}
/// <summary>
/// Perform validations on the form data filled
/// </summary>
/// <returns>"True", if all the validations are passed. Otherwise, "False".</returns>
async Task<bool> ValidateFormData()
{
List<string> errors = new List<string>();
foreach (FormField formField in pdfViewer.FormFields)
{
if (formField is TextFormField textFormField)
{
if (textFormField.Name == "Name")
{
if (string.IsNullOrEmpty(textFormField.Text))
{
errors.Add("Name is required.");
}
else if (textFormField.Text.Length < 3)
{
errors.Add("Name should be at least 3 characters.");
}
else if (textFormField.Text.Length > 30)
{
errors.Add("Name should not exceed 30 characters.");
}
else if (Regex.IsMatch(textFormField.Text, @"[0-9]"))
{
errors.Add("Name should not contain numbers.");
}
else if (Regex.IsMatch(textFormField.Text, @"[!@#$%^&*(),.?""{}|<>]"))
{
errors.Add("Name should not contain special characters.");
}
}
else if (textFormField.Name == "dob")
{
if (string.IsNullOrEmpty(textFormField.Text))
{
errors.Add("Date of birth is required.");
}
else if (!DateTime.TryParseExact(textFormField.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out _))
{
errors.Add("Date of birth should be in dd/mm/yyyy format.");
}
}
else if (textFormField.Name == "email")
{
if (string.IsNullOrEmpty(textFormField.Text))
{
errors.Add("Email is required.");
}
else if (!Regex.IsMatch(textFormField.Text, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$"))
{
errors.Add("Email should be in correct format.");
}
}
}
else if (formField is ListBoxFormField listBoxFormField)
{
if (listBoxFormField.SelectedItems.Count == 0)
{
errors.Add("Please select at least one course.");
}
}
else if (formField is SignatureFormField signatureFormField)
{
if (signatureFormField.Signature == null)
{
errors.Add("Please sign the document.");
}
}
}
if (errors.Count > 0)
{
await DisplayAlert("Errors", string.Join("\n", errors), "Try Again");
return false;
}
else
return true;
}
}
}