-
Notifications
You must be signed in to change notification settings - Fork 1
/
Form1.cs
293 lines (216 loc) · 8.38 KB
/
Form1.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
using LumenWorks.Framework.IO.Csv;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
/*
1- Check for unique columns and add only those columns to "Name output files according to Drop down list"
*/
namespace certificate_generator
{
public partial class Form1 : Form
{
private Control activeControl;
private Point previousPosition;
string img_path;
public Form1()
{
InitializeComponent();
}
private void load_img_btn_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog())
{
ofd.Filter = "Templates |*.png;*.jpg;*.jpeg;*.pdf;";
if (ofd.ShowDialog() == DialogResult.OK)
{
string file_name = ofd.FileName;
Console.WriteLine(file_name);
Image img = null;
if (Path.GetExtension(file_name) == ".pdf")
{
List<string> errors = cs_pdf_to_image.Pdf2Image.Convert(file_name, "certificate_generator_temp_inputfile.png");
//extension = ".png";
img = Image.FromFile("certificate_generator_temp_inputfile.png");
img_path = "certificate_generator_temp_inputfile.png";
foreach (string er in errors)
{
Console.WriteLine(er);
}
}
else
{
img = Image.FromFile(file_name);
img_path = file_name;
}
pictureBox1.Image = img;
img_dim_lbl.Text = img.Width + " x " + img.Height;
tmplt_path_tb.Text =ofd.FileName;
}
}
}
private void MyControl_MouseDown(Object sender ,MouseEventArgs e)
{
activeControl = sender as Control;
previousPosition = e.Location;
Cursor = Cursors.Hand;
}
private void MyControl_MouseUp(Object sender, MouseEventArgs e)
{
activeControl = null;
Cursor = Cursors.Default;
}
private void MyControl_MouseMove(Object sender, MouseEventArgs e)
{
if(activeControl == null || activeControl !=sender )
{
return;
}
var location = activeControl.Location;
location.Offset(e.Location.X - previousPosition.X, e.Location.Y - previousPosition.Y);
activeControl.Location = location;
Control s = sender as Control;
location_lbl.Text = s.Location.X + " , " + s.Location.Y;
}
private void MyControl_Change_Font_Click(Object sender, EventArgs e)
{
FontDialog fontDlg = new FontDialog();
fontDlg.ShowColor = true;
fontDlg.ShowEffects = true;
fontDlg.MaxSize = 100;
if (fontDlg.ShowDialog() != DialogResult.Cancel)
{
MenuItem menuItem = sender as MenuItem;
if(menuItem != null)
{
ContextMenu contextMenu = menuItem.GetContextMenu();
Control lbl = contextMenu.SourceControl;
lbl.Font = fontDlg.Font;
lbl.ForeColor = fontDlg.Color;
}
}
}
private void Add_labels_to_Template(string path)
{
Point lbl_location = new Point(30, 50);
ContextMenu menu = new ContextMenu();
menu.MenuItems.Add(new MenuItem("Change Font",new EventHandler(MyControl_Change_Font_Click)));
foreach (Label lbl in Tools.load_file_labels(path))
{
lbl.Location = lbl_location;
lbl.MouseDown += new MouseEventHandler(MyControl_MouseDown);
lbl.MouseMove += new MouseEventHandler(MyControl_MouseMove);
lbl.MouseUp += new MouseEventHandler(MyControl_MouseUp);
lbl.ContextMenu = menu;
pictureBox1.Controls.Add(lbl);
file_names_dd.Items.Add(lbl.Text);
}
}
private void choose_fldr_dlg_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "Choose Folder for saving certificates";
if (fbd.ShowDialog() == DialogResult.OK)
{
output_folder_path.Text = fbd.SelectedPath;
}
}
private void csv_dlg_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
{
openFileDialog1.Filter = "CSV files (*.csv)|*.csv";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
csv_txt.Text = openFileDialog1.FileName;
Add_labels_to_Template(csv_txt.Text);
}
}
file_names_dd.SelectedIndex = 0;
}
private bool check_validations()
{
string a = output_folder_path.Text;
string b = csv_txt.Text;
string c = tmplt_path_tb.Text;
return (!String.IsNullOrWhiteSpace(a) && !String.IsNullOrWhiteSpace(b) && !String.IsNullOrWhiteSpace(c));
}
private void remove_txt_Click(object sender, EventArgs e)
{
pictureBox1.Controls.Clear();
csv_txt.Text = "";
location_lbl.Text = "-,-";
}
private void button1_Click(object sender, EventArgs e)
{
//pictureBox1.Image = null;
tmplt_path_tb.Text = "";
pictureBox1.Controls.Clear();
if (!String.IsNullOrWhiteSpace(csv_txt.Text))
{
Add_labels_to_Template(csv_txt.Text);
}
}
private void reset_btn_Click(object sender, EventArgs e)
{
pictureBox1.Image = null;
location_lbl.Text = "-,-";
img_dim_lbl.Text = "0 x 0";
output_folder_path.Text = "";
csv_txt.Text = "";
tmplt_path_tb.Text = "";
pictureBox1.Controls.Clear();
file_names_dd.Items.Clear();
file_names_dd.Items.Add("Default Numbering");
file_names_dd.SelectedIndex = 0;
png_rb.Checked = true;
}
private string get_output_file_type()
{
string output_ext = "";
if(png_rb.Checked)
{
output_ext = ".png";
}
else if(jpeg_rb.Checked)
{
output_ext = ".jpeg";
}else if(jpg_rb.Checked)
{
output_ext = ".jpg";
}
else if (pdf_rb.Checked)
{
output_ext = ".pdf";
}
return output_ext;
}
private void Form1_Load(object sender, EventArgs e)
{
file_names_dd.SelectedIndex = 0;
}
private void save_Click(object sender, EventArgs e)
{
if (check_validations())
{
Dictionary<string, Point> d = Tools.get_labels_locations(pictureBox1.Controls.OfType<Label>());
Dictionary<string, Tuple<Font, Color>> fonts = Tools.get_labels_fonts(pictureBox1.Controls.OfType<Label>());
List<Tuple<List<Tuple<string, Point, Font, Color>>, string>> dic = Tools.map_lbl_to_file(csv_txt.Text, d, file_names_dd.Text, fonts);
Progress p = new Progress(dic,img_path, get_output_file_type(), output_folder_path.Text);
p.ShowDialog();
}
else
{
MessageBox.Show("Please choose all options");
}
}
}
}