protected void cmdSubmit_Click(object sender, EventArgs e) { _currentEmployee = (TimeEmployees)Session["CurrentEmployee"]; if (txtFname.Text == "") { lblError.Text = "Please enter a first name."; return; } if (txtLname.Text == "") { lblError.Text = "Please enter a last name."; return; } if (ddlCompany.SelectedIndex == -1) { lblError.Text = "Please choose a company."; return; } if (txtEmail.Text == "") { lblError.Text = "Please enter an email address."; return; } if (UserName.Text == "" && _currentEmployee == null) { lblError.Text = "Please enter a username."; return; } if (Password.Text == "" && _currentEmployee == null) { lblError.Text = "Please enter a password."; return; } if (ddlRoles.SelectedIndex == -1 && _currentEmployee == null) { lblError.Text = "Please choose a role for this user."; return; } lblError.Text = ""; if (_currentEmployee == null) { //create a user login MembershipCreateStatus status; Membership.CreateUser(UserName.Text, Password.Text, txtEmail.Text, null, null, true, out status); if (status == MembershipCreateStatus.Success) { //get the GUID of the newly created user var user = Membership.GetUser(UserName.Text); if (user != null) { var guid = (Guid) user.ProviderUserKey; //add user to the coach role Roles.AddUserToRole(UserName.Text, ddlRoles.SelectedItem.ToString()); //new employee object TimeEmployees employee = TimeEmployees.New(); //fill object with data employee.FirstName = txtFname.Text; employee.LastName = txtLname.Text; employee.CompanyID = Convert.ToInt32(ddlCompany.SelectedValue); employee.Description = txtDescription.Text; employee.Status = 1; employee.Type = 1; employee.UserID = guid; employee.Phone = txtPhone.Text; employee.Email = txtEmail.Text; //save the new employee employee.Save(); txtFname.Text = ""; txtLname.Text = ""; txtEmail.Text = ""; txtPhone.Text = ""; Password.Text = ""; UserName.Text = ""; ddlCompany.SelectedIndex = -1; txtDescription.Text = ""; _currentEmployee = null; Session["CurrentEmployee"] = _currentEmployee; RefreshEntries(); updEntries.Update(); lblSuccessMessage.Text = "Successfully submitted data!"; mpSuccess.Show(); } } else { lblSuccessMessage.Text = GetErrorMessage(status); mpSuccess.Show(); } } else { //we are editing an existing employee _currentEmployee.FirstName = txtFname.Text; _currentEmployee.LastName = txtLname.Text; _currentEmployee.CompanyID = Convert.ToInt32(ddlCompany.SelectedValue); _currentEmployee.Description = txtDescription.Text; _currentEmployee.Email = txtEmail.Text; _currentEmployee.Phone = txtPhone.Text; _currentEmployee.Status = 1; _currentEmployee.Type = 1; //save the new employee _currentEmployee.Save(); //re-enable the hidden controls UserName.Visible = true; Password.Visible = true; ddlRoles.Visible = true; txtFname.Text = ""; txtLname.Text = ""; txtEmail.Text = ""; txtPhone.Text = ""; Password.Text = ""; UserName.Text = ""; ddlCompany.SelectedIndex = -1; txtDescription.Text = ""; _currentEmployee = null; Session["CurrentEmployee"] = _currentEmployee; RefreshEntries(); updEntries.Update(); lblSuccessMessage.Text = "Successfully submitted data!"; mpSuccess.Show(); } }
protected void rptCustomers_ItemCommand(object source, RepeaterCommandEventArgs e) { if (e.CommandName == "Edit") { string allKeys = Convert.ToString(e.CommandArgument); int pkID = Convert.ToInt32(allKeys); _currentEmployee = TimeEmployees.Read(pkID); Session["CurrentEmployee"] = _currentEmployee; txtFname.Text = _currentEmployee.FirstName; txtLname.Text = _currentEmployee.LastName; txtEmail.Text = _currentEmployee.Email; txtPhone.Text = _currentEmployee.Phone; ddlCompany.SelectedValue = ddlCompany.Items.FindByValue(_currentEmployee.CompanyID.ToString()).Value; txtDescription.Text = _currentEmployee.Description; UserName.Visible = false; Password.Visible = false; ddlRoles.Visible = false; } if (e.CommandName == "Delete") { string allKeys = Convert.ToString(e.CommandArgument); int pkID = Convert.ToInt32(allKeys); _currentEmployee = TimeEmployees.Read(pkID); var user = Membership.GetUser(_currentEmployee.UserID); if (user == null) { lblError.Text = "User could not be deleted"; } else { //delete the user from the membership tables Membership.DeleteUser(user.UserName); _currentEmployee.Delete(); _currentEmployee = null; Session["CurrentEmployee"] = _currentEmployee; RefreshEntries(); updEntries.Update(); lblSuccessMessage.Text = "Successfully deleted data!"; mpSuccess.Show(); } } }
public List<byte[]> WriteToPdfForEmployeeMonthly(string sourceFile, CSList<TimeProjectHours> projects, TimeEmployees employee, DateTime dt, int repType) { var pages = new List<byte[]>(); int totalItemCount = 0; int currentLoopCount = 0; int totalHours = 0; while (totalItemCount < projects.Count) { var reader = new PdfReader(sourceFile); using (var memoryStream = new MemoryStream()) { // PDFStamper is the class we use from iTextSharp to alter an existing PDF. var pdfStamper = new PdfStamper(reader, memoryStream); Rectangle pageSize = reader.GetPageSizeWithRotation(1); PdfContentByte pdfPageContents = pdfStamper.GetOverContent(1); pdfPageContents.BeginText(); // Start working with text. BaseFont baseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, Encoding.ASCII.EncodingName, false); if (repType == 2) { pdfPageContents.SetFontAndSize(baseFont, 20); // 20 point font //show the project name for SRED pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Project: " + ddlMonthlyProjectsSRED.SelectedItem, 75, (pageSize.Height - 125), 0); } pdfPageContents.SetFontAndSize(baseFont, 10); // 10 point font pdfPageContents.SetRGBColorFill(0, 0, 0); //Name pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, employee.FirstName + " " + employee.LastName, 210, (pageSize.Height - 150), 0); pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "AIS-0" + employee.TimeEmployeeID, 525, (pageSize.Height - 150), 0); //find out how many days are in the month int days = DateTime.DaysInMonth(dt.Year, dt.Month); //Date of report //pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, dt.ToShortDateString(), 155, (pageSize.Height - 188), 0); pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, String.Format("{0:MMMM dd, yyyy}", dt), 210, (pageSize.Height - 172), 0); pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, String.Format("{0:MMMM dd, yyyy}", dt.AddDays(days - 1)), 525, (pageSize.Height - 172), 0); int yPos = 221; int localLoopCount = 0; foreach (var timeProjectHourse in projects) { if (localLoopCount > currentLoopCount || currentLoopCount == 0) { TimeResources resource = TimeResources.ReadFirst("TimeResourceID = @TimeResourceID", "@TimeResourceID", timeProjectHourse.TimeResourceID); TimeAISCodes classCode = TimeAISCodes.ReadFirst("TimeAISCodeID = @TimeAISCodeID", "@TimeAISCodeID", resource.TimeAISCodeID); TimeDepartments deptCode = TimeDepartments.ReadFirst("TimeDepartmentID = @TimeDepartmentID", "@TimeDepartmentID", timeProjectHourse.TimeDepartmentID); TimeProjects project = TimeProjects.ReadFirst("TimeProjectID = @TimeProjectID", "@TimeProjectID", timeProjectHourse.TimeProjectID); pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, String.Format("{0:MMM d}", timeProjectHourse.DateOfWork), 70, (pageSize.Height - yPos), 0); pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, project.ProjectNumber, 115, (pageSize.Height - yPos), 0); pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, classCode.AISCode, 175, (pageSize.Height - yPos), 0); //show the function pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, deptCode.DepartmentName, 220, (pageSize.Height - yPos), 0); //show the hours worked pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, timeProjectHourse.HoursOfWork.ToString(), 730, (pageSize.Height - yPos), 0); const int NUM_CHARS_ALLOWED = 85; int numLines = 0; if (timeProjectHourse.Description.Length <= NUM_CHARS_ALLOWED) numLines = 1; else if (timeProjectHourse.Description.Length > NUM_CHARS_ALLOWED && timeProjectHourse.Description.Length <= (NUM_CHARS_ALLOWED * 2)) numLines = 2; else if (timeProjectHourse.Description.Length > (NUM_CHARS_ALLOWED * 2) && timeProjectHourse.Description.Length <= (NUM_CHARS_ALLOWED * 3)) numLines = 3; int partCount = numLines; string input = timeProjectHourse.Description; var results = new string[partCount]; int rem = timeProjectHourse.Description.Length % NUM_CHARS_ALLOWED; for (var i = 0; i < partCount; i++) { if (i == partCount - 1) results[i] = input.Substring(NUM_CHARS_ALLOWED * i, rem); else results[i] = input.Substring(NUM_CHARS_ALLOWED * i, NUM_CHARS_ALLOWED); } for (var l = 0; l < numLines; l++) { pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, results[l], 345, (pageSize.Height - yPos), 0); yPos += 10; } //increment the total hours totalHours += timeProjectHourse.HoursOfWork; var botPos = (int)(pageSize.Height - yPos); pdfPageContents.SetLineWidth((float).5); pdfPageContents.MoveTo(65, botPos); pdfPageContents.LineTo(pageSize.Width - 35, botPos); pdfPageContents.Stroke(); yPos += 10; totalItemCount++; //check to see if we are at the bottom of the page if (yPos > 480) //540 { break; } } localLoopCount++; } currentLoopCount = localLoopCount; if (totalItemCount == projects.Count) { //Total pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, "Total Hours " + totalHours.ToString(), 730, (pageSize.Height - yPos), 0); } #region LEGEND SECTION //horizontal pdfPageContents.MoveTo(68, 40); pdfPageContents.LineTo(pageSize.Width - 325, 40); pdfPageContents.Stroke(); pdfPageContents.SetLineWidth((float).5); pdfPageContents.MoveTo(68, 55); pdfPageContents.LineTo(pageSize.Width - 325, 55); pdfPageContents.Stroke(); pdfPageContents.MoveTo(68, 70); pdfPageContents.LineTo(pageSize.Width - 325, 70); pdfPageContents.Stroke(); pdfPageContents.MoveTo(68, 85); pdfPageContents.LineTo(pageSize.Width - 325, 85); pdfPageContents.Stroke(); pdfPageContents.MoveTo(68, 100); pdfPageContents.LineTo(pageSize.Width - 325, 100); pdfPageContents.Stroke(); //horizontal //vertical pdfPageContents.MoveTo(68, 40); pdfPageContents.LineTo(68, 100); pdfPageContents.Stroke(); pdfPageContents.MoveTo(191, 40); pdfPageContents.LineTo(191, 100); pdfPageContents.Stroke(); pdfPageContents.MoveTo(204, 40); pdfPageContents.LineTo(204, 100); pdfPageContents.Stroke(); pdfPageContents.MoveTo(304, 40); pdfPageContents.LineTo(304, 100); pdfPageContents.Stroke(); pdfPageContents.MoveTo(318, 40); pdfPageContents.LineTo(318, 100); pdfPageContents.Stroke(); pdfPageContents.MoveTo(445, 40); pdfPageContents.LineTo(445, 100); pdfPageContents.Stroke(); pdfPageContents.MoveTo(pageSize.Width - 325, 40); pdfPageContents.LineTo(pageSize.Width - 325, 100); pdfPageContents.Stroke(); //end vertical pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, " CODE LEGEND", 70, 105, 0); pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, " Assistant Project Engineer B Software Developer D Advanced Specialist Engineer F+ ", 78, 90, 0); pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, " General Management C Specialist Engineer D Report Writing R ", 78, 75, 0); pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, " Mathematician D User Experience (GUI) D Technician T4 ", 78, 60, 0); pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, " Technical Meeting TM", 78, 45, 0); #endregion pdfPageContents.EndText(); // Done working with text pdfStamper.FormFlattening = true; // enable this if you want the PDF flattened. pdfStamper.Close(); // Always close the stamper or you'll have a 0 byte stream. pages.Add(memoryStream.ToArray()); } } return pages; }
public byte[] WriteToPdfForEmployeeDaily(string sourceFile, CSList<TimeProjectHours> projects, TimeEmployees employee, DateTime dt) { var reader = new PdfReader(sourceFile); using (var memoryStream = new MemoryStream()) { // PDFStamper is the class we use from iTextSharp to alter an existing PDF. var pdfStamper = new PdfStamper(reader, memoryStream); Rectangle pageSize = reader.GetPageSizeWithRotation(1); PdfContentByte pdfPageContents = pdfStamper.GetOverContent(1); pdfPageContents.BeginText(); // Start working with text. BaseFont baseFont = BaseFont.CreateFont(BaseFont.COURIER, Encoding.ASCII.EncodingName, false); pdfPageContents.SetFontAndSize(baseFont, 11); // 11 point font pdfPageContents.SetRGBColorFill(0, 0, 0); // Note: The x,y of the Pdf Matrix is from bottom left corner. // This command tells iTextSharp to write the text at a certain location with a certain angle. // Again, this will angle the text from bottom left corner to top right corner and it will // place the text in the middle of the page. // //pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_CENTER, playerName, pageSize.Width / 2, (pageSize.Height / 2) + 115, 0); //pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_CENTER, teamName, pageSize.Width / 2, (pageSize.Height / 2) + 80, 0); //user Name pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, employee.FirstName + " " + employee.LastName + " (AIS-0" + employee.TimeEmployeeID + ")", 155, (pageSize.Height - 168), 0); //Date of report pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, dt.ToShortDateString(), 155, (pageSize.Height - 188), 0); int yPos = 241; int totalHours = 0; foreach (var timeProjectHourse in projects) { TimeResources resource = TimeResources.ReadFirst("TimeResourceID = @TimeResourceID", "@TimeResourceID", timeProjectHourse.TimeResourceID); TimeAISCodes classCode = TimeAISCodes.ReadFirst("TimeAISCodeID = @TimeAISCodeID", "@TimeAISCodeID", resource.TimeAISCodeID); TimeProjects project = TimeProjects.ReadFirst("TimeProjectID = @TimeProjectID", "@TimeProjectID", timeProjectHourse.TimeProjectID); pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, project.ProjectNumber, 55, (pageSize.Height - yPos), 0); pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, classCode.AISCode, 125, (pageSize.Height - yPos), 0); pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, project.ProjectName, 185, (pageSize.Height - yPos), 0); pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, timeProjectHourse.HoursOfWork.ToString(), 500, (pageSize.Height - yPos), 0); //increment the total hours totalHours += timeProjectHourse.HoursOfWork; yPos += 20; } //Total pdfPageContents.ShowTextAligned(PdfContentByte.ALIGN_LEFT, totalHours.ToString(), 500, (pageSize.Height - 685), 0); pdfPageContents.EndText(); // Done working with text pdfStamper.FormFlattening = true; // enable this if you want the PDF flattened. pdfStamper.Close(); // Always close the stamper or you'll have a 0 byte stream. return memoryStream.ToArray(); } }
private void CreateMonthlyEmployeeSredPDFReport(CSList<TimeProjectHours> projects, TimeEmployees employee, DateTime dt) { try { string templatePdfPath = Server.MapPath("PDFimages"); string oldFile = templatePdfPath + "\\MonthlyEmployeeTemplate.pdf"; List<byte[]> pages = WriteToPdfForEmployeeMonthly(oldFile, projects, employee, dt, 2); if (pages == null) return; using (var output = new MemoryStream()) { var document = new Document(); var writer = new PdfCopy(document, output); document.Open(); for (int index = 0; index < pages.Count; index++) { var file = pages[index]; var reader = new PdfReader(file); int n = reader.NumberOfPages; PdfImportedPage page; for (int p = 1; p <= n; p++) { page = writer.GetImportedPage(reader, p); writer.AddPage(page); } } document.Close(); Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}-{1}.pdf", "MonthlyTimeCard-", employee.FirstName + "_" + employee.LastName)); Response.BinaryWrite(output.ToArray()); Response.Flush(); Response.End(); } } catch (Exception ex) { lblErrorMonthly.Text = "Error: " + ex.Message; } }
private void CreateDailyEmployeePDFReport(CSList<TimeProjectHours> projects, TimeEmployees employee, DateTime dt) { try { string templatePdfPath = Server.MapPath("PDFimages"); string oldFile = templatePdfPath + "\\DailyTemplate.pdf"; byte[] b = WriteToPdfForEmployeeDaily(oldFile, projects, employee, dt); if (b == null) return; HttpResponse response = HttpContext.Current.Response; response.Clear(); response.ContentType = "application/pdf"; response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}-{1}.pdf", "DailyTimeCard-", employee.FirstName + "_" + employee.LastName)); response.BinaryWrite(b); response.Flush(); response.End(); } catch (Exception ex) { lblError.Text = "Error: " + ex.Message; } }