-
Notifications
You must be signed in to change notification settings - Fork 0
/
Template1Form.cs
383 lines (355 loc) · 17.4 KB
/
Template1Form.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Diagnostics;
using System.Drawing.Imaging;
using Microsoft.Win32.SafeHandles;
namespace Assignment_2
{
public partial class Template1Form : Form
{
public Choose_Template_Form Form;
public int count_of_experience = 2;
public int count_of_education_title = 2;
public int count_of_skill_set = 2;
private int MoveFactor = 1;
public int count_of_image_sections = 0;
public Template1Form()
{
InitializeComponent();
}
public void ExportContent(string username, string AboutUser, string email, string number, string address, List<string> Experienceandcommunityservice, List<string> Educationandcertification, List<string> Skills)
{
label_Name.Text = username;
textbox_AboutText.Text = AboutUser;
label_Email.Text = email;
if (String.IsNullOrEmpty(number))
{
label_Number.Hide();
}
else
{
label_Number.Text = number;
}
label_Location.Text = address;
for (int i = 0; i < Experienceandcommunityservice.Count; i = i + 4)
{
if (i == 0)
{
if (i + 3 == Experienceandcommunityservice.Count)
{
AddExperience("1. " + Experienceandcommunityservice[i], "From " + Experienceandcommunityservice[i + 1] + " To " + Experienceandcommunityservice[i + 2], Experienceandcommunityservice[i + 3]);
break;
}
else
{
AddExperience("1. " + Experienceandcommunityservice[i], "From " + Experienceandcommunityservice[i + 1] + " To " + Experienceandcommunityservice[i + 2], Experienceandcommunityservice[i + 3]);
}
}
else
{
if (i + 3 == Experienceandcommunityservice.Count)
{
AddNewExperience(Experienceandcommunityservice[i], "From " + Experienceandcommunityservice[i + 1] + " To " + Experienceandcommunityservice[i + 2], Experienceandcommunityservice[i + 3]);
break;
}
else
{
AddNewExperience(Experienceandcommunityservice[i], "From " + Experienceandcommunityservice[i + 1] + "To" + Experienceandcommunityservice[i + 2], Experienceandcommunityservice[i + 3]);
}
}
}
for (int i = 0; i < Educationandcertification.Count; i = i + 4)
{
if (i == 0)
{
if (i + 3 == Educationandcertification.Count)
{
AddEducation("1. " + Educationandcertification[i] +" " + Educationandcertification[i + 1], "From " + Educationandcertification[i + 2] + " To " + Educationandcertification[i + 3]);
break;
}
else
{
AddEducation("1. " + Educationandcertification[i] + " " + Educationandcertification[i + 1], "From " + Educationandcertification[i + 2] + " To " + Educationandcertification[i + 3]);
}
}
else
{
if (i + 3 == Educationandcertification.Count)
{
AddNewEducation(Educationandcertification[i] + " " + Educationandcertification[i + 1], "From " + Educationandcertification[i + 2] + " To " + Educationandcertification[i + 3]);
break;
}
else
{
AddNewEducation(Educationandcertification[i] + " " + Educationandcertification[i + 1], "From " + Educationandcertification[i + 2] + " To " + Educationandcertification[i + 3]);
}
}
}
for (int i = 0; i < Skills.Count; i = i + 1)
{
if (i == 0)
{
AddSkills("1." + Skills[i]);
}
else
{
AddNewSkills(Skills[i]);
}
}
this.Show();
this.ExportToPdf();
}
public void ExportToPdf()
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "PDF (*.pdf)|*.pdf";
sfd.FileName = "Output.pdf";
if (sfd.ShowDialog() == DialogResult.OK)
{
string fileOutputPath = sfd.FileName;
try
{
if (File.Exists(fileOutputPath))
{
File.Delete(fileOutputPath);
}
using (FileStream Stream = new FileStream(fileOutputPath, FileMode.Create))
{
float leftMargin = 1;
float rightMargin = 1;
float topMargin = 1 * 18;
float bottomMargin = 1;
Document pdfDoc = new Document(PageSize.A4, leftMargin, rightMargin, topMargin, bottomMargin);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Stream);
pdfDoc.Open();
WriteImagesToPdf(pdfDoc);
pdfDoc.Close();
Stream.Close();
FileOpenParallel(fileOutputPath);
Application.Restart();
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex);
}
}
else
{
this.Close();
}
}
private void WriteImagesToPdf(Document pdf)
{
int panelHeight = Template_panel1.Height;
int pageHeight = 1123; // A4 page height in pixels
int numPages = (int)Math.Ceiling((double)panelHeight / pageHeight);
int minusfactor = 0;
int minusfactorsectioncapturedheight = 52;
// Capture the entire panel as an image
Bitmap panelImage = CapturePanel(Template_panel1);
for (int i = 0; i < numPages; i++)
{
if (i > 0)
{
pdf.NewPage();
minusfactor = 52;
}
int startY = (i * pageHeight) - minusfactor;
int height = Math.Min(pageHeight, panelHeight - startY);
if (i == numPages - 1)
{
minusfactorsectioncapturedheight = 0;
}
// Crop the section of the panel image for the current page
Bitmap sectionBitmap = CropBitmap(panelImage, new System.Drawing.Rectangle(0, startY, panelImage.Width, height - minusfactorsectioncapturedheight));
// Add the cropped section to the PDF
AddImageToPdf(pdf, sectionBitmap);
}
}
private Bitmap CropBitmap(Bitmap bitmap, System.Drawing.Rectangle cropArea)
{
Bitmap croppedBitmap = bitmap.Clone(cropArea, bitmap.PixelFormat);
return croppedBitmap;
}
private void AddImageToPdf(Document pdf, Bitmap bitmap)
{
using (MemoryStream ms = new MemoryStream())
{
bitmap.Save(ms, ImageFormat.Png);
ms.Position = 0;
iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(ms);
// Scale the image to fit the page
pdfImage.ScaleToFit(pdf.PageSize.Width - pdf.LeftMargin - pdf.RightMargin, pdf.PageSize.Height - pdf.TopMargin + 0.5f);
pdf.Add(pdfImage);
count_of_image_sections = 0;
}
}
private Bitmap CapturePanel(Panel panel)
{
float scaleFactor = 1.0f; // Adjust this to increase resolution
Bitmap bmp = new Bitmap((int)(panel.Width * scaleFactor), (int)(panel.Height * scaleFactor));
using (Graphics g = Graphics.FromImage(bmp))
{
g.ScaleTransform(scaleFactor, scaleFactor);
panel.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, panel.Width, panel.Height));
}
return bmp;
}
private void FileOpenParallel(string path)
{
Process.Start(new ProcessStartInfo(path) { UseShellExecute = true });
}
private void AddExperience(string organizationname, string experiencedate, string experiencedescription)
{
label_CompanyName1.Text = organizationname;
label_ExperienceDate1.Text = experiencedate;
rtb_ExperienceDescription.Text = experiencedescription;
}
private void AddNewExperience(string organizationname, string experiencedate, string experiencedescription)
{
Label label_company = new Label();
label_company.Text = (count_of_experience) + ". " + organizationname;
label_company.Name = "label_CompanyName " + count_of_experience;
Label label_date = new Label();
label_date.Text = experiencedate;
label_date.Name = "labelExpDate " + count_of_experience;
RichTextBox description = new RichTextBox();
description.Text = experiencedescription;
description.Name = "rtb_JobDescription " + count_of_experience;
CopyLabelProperties(label_CompanyName1, label_company);
CopyLabelProperties(label_ExperienceDate1, label_date);
CopyRichTextBoxProperties(rtb_ExperienceDescription, description);
flp_Experience.Controls.Add(label_company);
flp_Experience.Controls.Add(label_date);
flp_Experience.Controls.Add(description);
while (flp_Experience.Bounds.IntersectsWith(label_Education.Bounds))
{
System.Drawing.Point newPointofeducationlabel = new System.Drawing.Point(label_Education.Location.X, label_Education.Location.Y + MoveFactor);
label_Education.Location = newPointofeducationlabel;
while (label_Education.Bounds.IntersectsWith(pictureBox_LineUnderEducation.Bounds))
{
System.Drawing.Point PointofEducationLine = new System.Drawing.Point(pictureBox_LineUnderEducation.Location.X, pictureBox_LineUnderEducation.Location.Y + MoveFactor);
pictureBox_LineUnderEducation.Location = PointofEducationLine;
System.Drawing.Point PointofflpEducation = new System.Drawing.Point(flp_EducationContent.Location.X, flp_EducationContent.Location.Y + MoveFactor);
flp_EducationContent.Location = PointofflpEducation;
while (flp_EducationContent.Bounds.IntersectsWith(label_Skills.Bounds))
{
System.Drawing.Point newPointofskilllabel = new System.Drawing.Point(label_Skills.Location.X, label_Skills.Location.Y + MoveFactor);
label_Skills.Location = newPointofskilllabel;
while (label_Skills.Bounds.IntersectsWith(pictureBox_LineUnderSkills.Bounds))
{
System.Drawing.Point PointofSkillLine = new System.Drawing.Point(pictureBox_LineUnderSkills.Location.X, pictureBox_LineUnderSkills.Location.Y + MoveFactor);
pictureBox_LineUnderSkills.Location = PointofSkillLine;
System.Drawing.Point Pointofflpskillset = new System.Drawing.Point(flp_skillset.Location.X, flp_skillset.Location.Y + MoveFactor);
flp_skillset.Location = Pointofflpskillset;
}
}
}
}
count_of_experience++;
}
private void AddEducation(string educationtitle, string educationdate)
{
label_EducationTitleandUni.Text = educationtitle;
label_Education_Date.Text = educationdate;
}
private void AddNewEducation(string educationtitle, string educationdate)
{
Label label_educationdate = new Label();
label_educationdate.Text = educationdate;
Label label_educationtitle = new Label();
label_educationtitle.Text = (count_of_education_title) + ". " + educationtitle;
CopyLabelProperties(label_Education_Date, label_educationdate);
CopyLabelProperties(label_EducationTitleandUni, label_educationtitle);
flp_EducationContent.Controls.Add(label_educationtitle);
flp_EducationContent.Controls.Add(label_educationdate);
while (flp_EducationContent.Bounds.IntersectsWith(label_Skills.Bounds))
{
System.Drawing.Point newPointofskilllabel = new System.Drawing.Point(label_Skills.Location.X, label_Skills.Location.Y + MoveFactor);
label_Skills.Location = newPointofskilllabel;
while (label_Skills.Bounds.IntersectsWith(pictureBox_LineUnderSkills.Bounds))
{
System.Drawing.Point PointofSkillLine = new System.Drawing.Point(pictureBox_LineUnderSkills.Location.X, pictureBox_LineUnderSkills.Location.Y + MoveFactor);
pictureBox_LineUnderSkills.Location = PointofSkillLine;
System.Drawing.Point Pointofflpskillset = new System.Drawing.Point(flp_skillset.Location.X, flp_skillset.Location.Y + MoveFactor);
flp_skillset.Location = Pointofflpskillset;
}
}
while (!flp_Experience.Bounds.IntersectsWith(label_Education.Bounds))
{
System.Drawing.Point newPointofeducationlabel = new System.Drawing.Point(label_Education.Location.X, label_Education.Location.Y - MoveFactor);
label_Education.Location = newPointofeducationlabel;
}
System.Drawing.Point newPointofeducationlabel2 = new System.Drawing.Point(label_Education.Location.X, label_Education.Location.Y + MoveFactor);
label_Education.Location = newPointofeducationlabel2;
count_of_education_title++;
}
public void AddSkills(string skilltext)
{
label_skillset.Text = skilltext;
}
public void AddNewSkills(string skilltext)
{
Label skillset = new Label();
skillset.Text = count_of_skill_set.ToString() + ". " + skilltext;
CopyLabelProperties(label_skillset, skillset);
flp_skillset.Controls.Add(skillset);
while (!flp_Experience.Bounds.IntersectsWith(label_Education.Bounds))
{
System.Drawing.Point newPointofeducationlabel = new System.Drawing.Point(label_Education.Location.X, label_Education.Location.Y - MoveFactor);
label_Education.Location = newPointofeducationlabel;
}
System.Drawing.Point newPointofeducationlabel2 = new System.Drawing.Point(label_Education.Location.X, label_Education.Location.Y + MoveFactor);
label_Education.Location = newPointofeducationlabel2;
while (!flp_EducationContent.Bounds.IntersectsWith(label_Skills.Bounds))
{
System.Drawing.Point newPointofskillslabel1 = new System.Drawing.Point(label_Skills.Location.X, label_Skills.Location.Y - MoveFactor);
label_Skills.Location = newPointofskillslabel1;
}
System.Drawing.Point newPointofskillslabel2 = new System.Drawing.Point(label_Skills.Location.X, label_Skills.Location.Y + MoveFactor);
label_Skills.Location = newPointofskillslabel2;
count_of_skill_set++;
}
private void CopyLabelProperties(Label source, Label target)
{
target.Font = source.Font;
target.ForeColor = source.ForeColor;
target.BackColor = source.BackColor;
target.Size = source.Size;
target.Location = source.Location;
target.Padding = source.Padding;
target.Margin = source.Margin;
target.AutoSize = source.AutoSize;
target.TextAlign = source.TextAlign;
target.BorderStyle = source.BorderStyle;
}
private void CopyRichTextBoxProperties(RichTextBox source, RichTextBox target)
{
target.Font = source.Font;
target.ForeColor = source.ForeColor;
target.BackColor = source.BackColor;
target.Size = source.Size;
target.Location = source.Location;
target.Padding = source.Padding;
target.Margin = source.Margin;
target.ReadOnly = source.ReadOnly;
target.ScrollBars = source.ScrollBars;
target.BorderStyle = source.BorderStyle;
}
private void Template1Form_FormClosed(object sender, FormClosedEventArgs e)
{
this.Owner.Show();
}
}
}