public string CreateTable1(string FilePath, string ImagePath, string fontpath, PdfContractContent pdfModel)
        {

            string oldFile =FilePath + "ContractSample.pdf";
            string newFile = FilePath + "newContractFile.pdf";

            // open the reader
            PdfReader reader = new PdfReader(oldFile);
            iTextSharp.text.Rectangle size = reader.GetPageSizeWithRotation(1);
            Document document = new Document(size);

            // open the writer
            FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
            PdfWriter writer = PdfWriter.GetInstance(document, fs);
            document.Open();

            // the pdf content
            PdfContentByte cb = writer.DirectContent;

            // select the font properties
            BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            cb.SetColorFill(BaseColor.DARK_GRAY);
            cb.SetFontAndSize(bf, 14);

            string RequestNumber = pdfModel.RequestNumber;
            string Address = pdfModel.Address;
            string Category = pdfModel.Category;
            string Priority = pdfModel.Priority;
            string Status = pdfModel.Status;
            string IssuedOn = pdfModel.Issued;

            string MainContact = pdfModel.primaryContact;
            string MainEmailAddress = pdfModel.PrimaryEmail;
            string MainPhone = pdfModel.PrimaryPhone;

            string Problem = pdfModel.problem;
            string Instructions = pdfModel.TenantInstruction;
            string OfficeNotes = pdfModel.OfficeNotes;

  

           // // write the text in the pdf content
           // cb.BeginText();          
           // // put the alignment and coordinates here
           // cb.ShowTextAligned(0, RequestNumber, 700, 740, 0);

            cb.BeginText();
            
            cb.ShowTextAligned(2, RequestNumber, 550, 648, 0);
            cb.EndText();

            cb.BeginText();
            cb.ShowTextAligned(0, Address, 48, 648, 0);
            cb.EndText(); 

            cb.BeginText();
            cb.ShowTextAligned(0, Category, 118, 578, 0);
            cb.EndText();


            cb.BeginText();
            cb.ShowTextAligned(0, Priority, 108, 561, 0);
            cb.EndText();

            cb.BeginText();
            cb.ShowTextAligned(0, Status, 100, 544, 0);
            cb.EndText();
            

            cb.BeginText();
            cb.ShowTextAligned(0, IssuedOn, 120, 525, 0);                    
            cb.EndText();

            cb.BeginText();
            cb.ShowTextAligned(0, MainContact, 320, 560, 0);
            cb.EndText();

            cb.BeginText();
            cb.ShowTextAligned(0, MainEmailAddress, 363, 542, 0);
            cb.EndText();

            cb.BeginText();
            cb.ShowTextAligned(0, MainPhone, 348, 526, 0);
            cb.EndText();
            
            ColumnText ct = new ColumnText(cb);
             ct.SetSimpleColumn(new Phrase(new Chunk(Problem, FontFactory.GetFont(FontFactory.HELVETICA, 14,iTextSharp.text.Font.NORMAL))),
                     46, 465, 550, 30, 20, Element.ALIGN_LEFT | Element.ALIGN_TOP);
             ct.Go();

             ColumnText oinstruction = new ColumnText(cb);
             oinstruction.SetSimpleColumn(new Phrase(new Chunk(OfficeNotes, FontFactory.GetFont(FontFactory.HELVETICA, 14, iTextSharp.text.Font.NORMAL))),
                     46, 264, 550, 30, 20, Element.ALIGN_LEFT | Element.ALIGN_TOP);
             oinstruction.Go();


           
            
            

            //cb.BeginText();
          
            //cb.ShowTextAligned(0, Problem, 100, 450, 0);
            //cb.ShowTextAligned(0, Instructions, 100, 350, 0);
            //cb.ShowTextAligned(0, OfficeNotes, 100, 250, 0);
           
            //cb.EndText();

            //cb.BeginText();
            //// put the alignment and coordinates here
            //cb.ShowTextAligned(0, MainContact, 100, 540, 0);
            //cb.ShowTextAligned(0, MainEmailAddress, 100, 500, 0);
            //cb.ShowTextAligned(0, MainPhone, 100, 460, 0);
            //cb.EndText();

           


            // create the new page and add it to the pdf
            PdfImportedPage page = writer.GetImportedPage(reader, 1);
            cb.AddTemplate(page, 0, 0);

            // close the streams and voilá the file should be changed :)
            document.Close();
            fs.Close();
            fs.Dispose();
            writer.Close();
            reader.Close();

           

            return "success";
        }
       public JsonResult ApplyUpdatesRepairRequest(string UserID, int RepairRequestID, string Notes, bool whichone)
       {

           if (whichone == true) //Building Staff========================================================================
           {
               pdfWorker pdfworker = new pdfWorker();
               RepairRequest RR = db.RepairRequest.Find(RepairRequestID);

               RepairRequestNote RN = new RepairRequestNote
               {
                   DateCreated = DateTime.Now,
                   Notes = Notes
               };

               db.RepairRequestNote.Add(RN);
               db.SaveChanges();

               RR.RepairRequestNoteID = RN.Id;
               RR.AssignID = UserID;
               RR.AssignContractorID = null;
               RR.Status = "Assigned";

               db.RepairRequest.Attach(RR);
               var Entry = db.Entry(RR);

               Entry.Property(c => c.RepairRequestNoteID).IsModified = true;
               Entry.Property(c => c.AssignID).IsModified = true;
               Entry.Property(c => c.AssignContractorID).IsModified = true;
               Entry.Property(c => c.Status).IsModified = true;

               db.SaveChanges();

               var Worker = db.BuildingUser.Where(c => c.UserID == UserID).FirstOrDefault();

               //string string1 = "<div style='font-size:20px; colo:blue; display:bloc'>Hi " + Worker.FirstName + " " + Worker.LastName + ",</div> ";
               //string string2 = "You have a new assignemt and the description is bellow:";
               //string string3 = "The Category of this Request is " + RR.RepairRequestCategories.Categories;
               //string string4 = "The Decription of the request is: " + RR.ProblemDescription;
               //string string5 = "The Urgency is: " + RR.RepairUrgency.Urgency;
               //string string6 = "For questions about this email Contact management at: " + RR.Buildings.BuildingPhone;
               //string string7 = "Find more information...";

               //string x = string.Format("{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n", string1, string2, string3, string4, string5, string6, string7);

               string contenttobemail = " <div style='font-size:20px; display:block;  width:100%; background:#0071bc;  height:50px;  line-height:50px; padding:0 15px; border:1px solid lightgrey;   color:white;' >"+
                Worker.FirstName + " " + Worker.LastName +"</div>"+
                                   "<div style='   display:block;   width:100%;   margin:10px 0 10px 0;   padding:10px 15px;   background:#F0F0F0;   border:1px solid lightgrey;   '>     You have a new assignemt and the description is bellow:<br/>"+
                                   " <hr/>"+
                                   " <br/>"+
                                   " <b> The Category of this Request is</b> "+
                                   "<br/>"+
                                   RR.RepairRequestCategories.Categories +
                                   " <hr/>"+
                                   " <br/>"+
                                   "<b>The Urgency is:</b>"+
                                   " <br/>" + RR.RepairUrgency.Urgency +
                                   "<hr/>"+
                                   " <br/>"+
                                   " <b>The Decription of the request is:</b>"+
                                   "<br/>"+
                                  RR.ProblemDescription+
                                   " <hr/>"+
                                   "<br/>"+
                                   "</div>"+
                                   "<div style='font-size:20px; display:block; width:100%; background:#0071bc; height:50px;line-height:50px; padding:0 15px; border:1px solid lightgrey; color:white;' >For questions about this email Contact management at: " + RR.Buildings.BuildingPhone + "Find more information...  </div>";

               Gmail gmail = new Gmail("pointerwebapp", "dmc10040");
               MailMessage msg = new MailMessage("*****@*****.**", Worker.Email);
               msg.Subject = "New Assignment Notification";
               msg.Body = contenttobemail;
               msg.IsBodyHtml = true;
               
               //new
               PdfContractContent pdfContent = new PdfContractContent {
                   Address = RR.Buildings.Address + " " + RR.Tenant.Apartment.ApartmentNumber + " " + RR.Buildings.City +" " + RR.Buildings.State+" " + RR.Buildings.Zipcode,
                 Category = RR.RepairRequestCategories.Categories,
                 Priority = RR.RepairUrgency.Urgency,
                 Status = RR.Status,
                 Issued = RR.RequestedDate.Month.ToString() +"/"+ RR.RequestedDate.Day.ToString() +"/"+ RR.RequestedDate.Year.ToString(),
                 primaryContact = RR.Tenant.FirstName + " " + RR.Tenant.LastName,
                 PrimaryPhone = RR.Tenant.Phone,
                 PrimaryEmail = RR.Tenant.Username,
                 OfficeNotes = RR.RepairRequestNote.Notes,
                 RequestNumber =RR.RequestNumber,
                 problem = RR.ProblemDescription,
                 TenantInstruction = RR.Instructions_   
               };
               //new
               var result = pdfworker.CreateTable1(Server.MapPath("~/ContractPDF/"), Server.MapPath("~/img/"), Server.MapPath("~/fonts/"), pdfContent);
              
               Attachment attachment;
               attachment = new Attachment(Server.MapPath("~/ContractPDF/newContractFile.pdf"));
               msg.Attachments.Add(attachment);     

               gmail.Send(msg);
               attachment.Dispose();//needs to be dispose because the process is use and cannot be open twice at the same time.
               msg.Dispose();
           }
           else if (whichone == false) //Company========================================================================================================
           {
               pdfWorker pdfworker = new pdfWorker();
               RepairRequest RR = db.RepairRequest.Find(RepairRequestID);

               RepairRequestNote RN = new RepairRequestNote
               {
                   DateCreated = DateTime.Now,
                   Notes = Notes

               };

               db.RepairRequestNote.Add(RN);
               db.SaveChanges();

               RR.RepairRequestNoteID = RN.Id;
               RR.AssignContractorID = UserID;
               RR.AssignID = null;
               RR.Status = "Assigned";

               db.RepairRequest.Attach(RR);
               var Entry = db.Entry(RR);

               Entry.Property(c => c.RepairRequestNoteID).IsModified = true;
               Entry.Property(c => c.AssignContractorID).IsModified = true;
               Entry.Property(c => c.AssignID).IsModified = true;
               Entry.Property(c => c.Status).IsModified = true;
               db.SaveChanges();

               var Worker = db.Contractor.Where(c => c.Id == UserID).FirstOrDefault();

               string contenttobemail = " <div style='font-size:20px; display:block;  width:100%; background:#0071bc;  height:50px;  line-height:50px; padding:0 15px; border:1px solid lightgrey;   color:white;' >" +
                 Worker.CompanyName + "</div>" +
                                        "<div style='   display:block;   width:100%;   margin:10px 0 10px 0;   padding:10px 15px;   background:#F0F0F0;   border:1px solid lightgrey;   '>     You have a new assignemt and the description is bellow:<br/>" +
                                        " <hr/>" +
                                        " <br/>" +
                                        " <b> The Category of this Request is</b> " +
                                        "<br/>" +
                                        RR.RepairRequestCategories.Categories +
                                        " <hr/>" +
                                        " <br/>" +
                                        "<b>The Urgency is:</b>" +
                                        " <br/>" + RR.RepairUrgency.Urgency +
                                        "<hr/>" +
                                        " <br/>" +
                                        " <b>The Decription of the request is:</b>" +
                                        "<br/>" +
                                       RR.ProblemDescription +
                                        " <hr/>" +
                                        "<br/>" +
                                        "</div>" +
                                        "<div style='font-size:20px; display:block; width:100%; background:#0071bc; height:50px;line-height:50px; padding:0 15px; border:1px solid lightgrey; color:white;' >For questions about this email Contact management at: " + RR.Buildings.BuildingPhone + "Find more information...  </div>";


               Gmail gmail = new Gmail("pointerwebapp", "dmc10040");
               MailMessage msg = new MailMessage("*****@*****.**", Worker.Email);
               msg.Subject = "New Assignment Notification";
               msg.Body = contenttobemail;

               msg.IsBodyHtml = true;
               //new
               PdfContractContent pdfContent = new PdfContractContent
               {
                   Address = RR.Buildings.Address + " " + RR.Tenant.Apartment.ApartmentNumber + " " + RR.Buildings.City + " " + RR.Buildings.State + " " + RR.Buildings.Zipcode,
                   Category = RR.RepairRequestCategories.Categories,
                   Priority = RR.RepairUrgency.Urgency,
                   Status = RR.Status,
                   Issued = RR.RequestedDate.Month.ToString() + "/" + RR.RequestedDate.Day.ToString() + "/" + RR.RequestedDate.Year.ToString(),
                   primaryContact = RR.Tenant.FirstName + " " + RR.Tenant.LastName,
                   PrimaryPhone = RR.Tenant.Phone,
                   PrimaryEmail = RR.Tenant.Username,
                   OfficeNotes = RR.RepairRequestNote.Notes,
                   RequestNumber =RR.RequestNumber,
                   problem = RR.ProblemDescription,
                   TenantInstruction = RR.Instructions_
               };
               //new
               var result = pdfworker.CreateTable1(Server.MapPath("~/ContractPDF/"), Server.MapPath("~/img/"), Server.MapPath("~/fonts/"), pdfContent);

               Attachment attachment;
               attachment = new Attachment(Server.MapPath("~/ContractPDF/newContractFile.pdf"));
               msg.Attachments.Add(attachment); 
               gmail.Send(msg);
               attachment.Dispose();
           
           }
            
           var JSONdATA = Json("");
           return new JsonResult {Data = JSONdATA, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
       }