public async Task <bool> ProformaInvoiceEmailPush(ProformaInvoice proformaInvoice) { bool sendsuccess = false; try { if (!string.IsNullOrEmpty(proformaInvoice.EmailSentTo)) { //------------------------ string[] BccList = null; string[] CcList = null; string[] EmailList = proformaInvoice.EmailSentTo.Split(','); if (proformaInvoice.Cc != null) { CcList = proformaInvoice.Cc.Split(','); } if (proformaInvoice.Bcc != null) { BccList = proformaInvoice.Bcc.Split(','); } string mailBody = File.ReadAllText(HttpContext.Current.Server.MapPath("~/Content/MailTemplate/DocumentEmailBody.html")); MailMessage _mail = new MailMessage(); PDFTools pDFTools = new PDFTools(); string link = WebConfigurationManager.AppSettings["AppURL"] + "/Content/images/Pilot1.png"; _mail.Body = mailBody.Replace("$Customer$", proformaInvoice.Customer.ContactPerson).Replace("$Document$", "Proforma Invoice").Replace("$DocumentNo$", proformaInvoice.ProfInvNo).Replace("$DocumentDate$", proformaInvoice.ProfInvDateFormatted).Replace("$Logo$", link); pDFTools.Content = proformaInvoice.MailContant; pDFTools.ContentFileName = "ProformaInvoice"; pDFTools.IsWithWaterMark = proformaInvoice.LatestApprovalStatus == 0 ? true : false; _mail.Attachments.Add(new Attachment(new MemoryStream(_pdfGeneratorBusiness.GetPdfAttachment(pDFTools)), proformaInvoice.ProfInvNo + ".pdf")); _mail.Subject = proformaInvoice.Subject; _mail.IsBodyHtml = true; foreach (string email in EmailList) { _mail.To.Add(email); } if (proformaInvoice.Cc != null) { foreach (string email in CcList) { _mail.CC.Add(email); } } if (proformaInvoice.Bcc != null) { foreach (string email in BccList) { _mail.Bcc.Add(email); } } sendsuccess = await _mailBusiness.MailMessageSendAsync(_mail); } } catch (Exception ex) { throw ex; } return(sendsuccess); }
public void AddFile(string path) { var file = new FileObject { FileName = utility.TrimFileName(path), FilePath = path, FileNum = Files.Count + 1 }; PDFTools.CheckPDFPassword(file); file.NumPages = PDFTools.GetTotalPages(file); TotalPages += file.NumPages; Files.Add(file); }
public byte[] GetPdfAttachment(PDFTools pDFTools) { try { string htmlBody = pDFTools.Content == null ? "" : pDFTools.Content.Replace("<br>", "<br/>").ToString().Replace("workAround:image\">", "workAround:image\"/>"); StringReader reader = new StringReader(htmlBody.ToString()); Document pdfDoc = new Document(PageSize.A4, 42.5197f, 28.3465f, 155.732f, 141.732f); byte[] bytes = null; using (MemoryStream memoryStream = new MemoryStream()) { PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream); Footer footobj = new Footer(); footobj.imageURL = System.Web.HttpContext.Current.Server.MapPath("~/Content/images/LetterHead.jpg"); footobj.watermark = pDFTools.IsWithWaterMark ? "DRAFT COPY" : ""; writer.PageEvent = footobj; pdfDoc.Open(); XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, reader); pdfDoc.Close(); bytes = memoryStream.ToArray(); memoryStream.Close(); //return bytes; } string contentFileName = pDFTools.ContentFileName.ToString() == null ? "Report.pdf" : (pDFTools.ContentFileName.ToString() + ".pdf"); //string filename = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/Content/Uploads/"), contentFileName); if (pDFTools.ContentFileName.ToString() == "Quotation" || pDFTools.ContentFileName.ToString() == "ProformaInvoice" || pDFTools.ContentFileName.ToString() == "SaleInvoice") { string demo = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/Content/Uploads/"), "Demo" + contentFileName); System.IO.File.WriteAllBytes(demo, bytes); return(MergePDFs(demo, System.Web.HttpContext.Current.Server.MapPath("~/Content/images/Terms&Cond.pdf"))); } else { return(bytes); } } catch (Exception ex) { throw ex; } }
private void GenerarInforme() { PDFTools.GenerarInformePDF(informe, saveDialog); }
public string PrintPDF(PDFTools pDFToolsObj) { //string imageURL = Server.MapPath("~/Content/images/logo.png"); //iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageURL); ////Resize image depend upon your need //jpg.ScaleToFit(70f, 60f); //jpg.SpacingBefore = 10f; //jpg.SpacingAfter = 1f; //jpg.Alignment = Element.ALIGN_LEFT; string htmlBody = pDFToolsObj.Content.Replace("<br>", "<br/>").ToString(); StringReader reader = new StringReader(htmlBody.ToString()); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 85f, 30f); byte[] bytes = null; using (MemoryStream memoryStream = new MemoryStream()) { PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream); Footer footobj = new Footer(); footobj.imageURL = Server.MapPath("~/Content/images/header.png"); footobj.imgURL = Server.MapPath("~/Content/images/footer.jpg"); footobj.Header = XMLWorkerHelper.ParseToElementList(pDFToolsObj.Headcontent, null); footobj.Tableheader = pDFToolsObj.HeaderText; writer.PageEvent = footobj; // Our custom Header and Footer is done using Event Handler //TwoColumnHeaderFooter PageEventHandler = new TwoColumnHeaderFooter(); //writer.PageEvent = PageEventHandler; //// Define the page header //PageEventHandler.Title = "Column Header"; //PageEventHandler.HeaderFont = FontFactory.GetFont(BaseFont.COURIER_BOLD, 10, Font.BOLD); //PageEventHandler.HeaderLeft = "Group"; //PageEventHandler.HeaderRight = "1"; pdfDoc.Open(); //jpg.SetAbsolutePosition(pdfDoc.Left, pdfDoc.Top - 60); //pdfDoc.Add(jpg); //PdfContentByte cb = writer.DirectContent; //cb.MoveTo(pdfDoc.Left, pdfDoc.Top-60 ); //cb.LineTo(pdfDoc.Right, pdfDoc.Top-60); //cb.SetLineWidth(1); //cb.SetColorStroke(new CMYKColor(0f, 12f, 0f, 7f)); //cb.Stroke(); //cb.MoveTo(pdfDoc.Left, pdfDoc.Top+5); //cb.LineTo(pdfDoc.Right, pdfDoc.Top+5); //cb.SetLineWidth(1); //cb.SetColorStroke(new CMYKColor(0f, 12f, 0f, 7f)); //cb.Stroke(); //Paragraph welcomeParagraph = new Paragraph("Hello, World!"); // Our custom Header and Footer is done using Event Handler //pdfDoc.Add(welcomeParagraph); XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, reader); //for (int i = 0; i <= 2; i++) //{ // // Define the page header // PageEventHandler.HeaderRight = i.ToString(); // if (i != 1) // { // pdfDoc.NewPage(); // } //} pdfDoc.Close(); bytes = memoryStream.ToArray(); memoryStream.Close(); } string name = pDFToolsObj.CustomerName;//.Split(":").ToString(); string[] words = name.Split(':'); string fname = Path.Combine(Server.MapPath("~/Content/Uploads/"), pDFToolsObj.HeaderText.ToString() + " FOR " + words[1] + ".pdf"); System.IO.File.WriteAllBytes(fname, bytes); //File(bytes, "application/pdf", "Report.pdf").sa //bytes.SaveAs(fname); return(JsonConvert.SerializeObject(new { Result = "OK", URL = "../Content/Uploads/" + pDFToolsObj.HeaderText.ToString() + " FOR " + words[1] + ".pdf" })); }
public FileResult Download(PDFTools pDFToolsObj) { //jpg.Alignment = Element.ALIGN_LEFT; string htmlBody = pDFToolsObj.Content.Replace("<br>", "<br/>").ToString(); StringReader reader = new StringReader(htmlBody.ToString()); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 85f, 30f); byte[] bytes = null; using (MemoryStream memoryStream = new MemoryStream()) { PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream); Footer footobj = new Footer(); footobj.imageURL = Server.MapPath("~/Content/images/header.png"); footobj.imgURL = Server.MapPath("~/Content/images/footer.jpg"); footobj.Header = XMLWorkerHelper.ParseToElementList(pDFToolsObj.Headcontent, null); footobj.CustomerName = pDFToolsObj.CustomerName; footobj.Tableheader = pDFToolsObj.HeaderText; writer.PageEvent = footobj; // Our custom Header and Footer is done using Event Handler //TwoColumnHeaderFooter PageEventHandler = new TwoColumnHeaderFooter(); //writer.PageEvent = PageEventHandler; //// Define the page header //PageEventHandler.Title = "Column Header"; //PageEventHandler.HeaderFont = FontFactory.GetFont(BaseFont.COURIER_BOLD, 10, Font.BOLD); //PageEventHandler.HeaderLeft = "Group"; //PageEventHandler.HeaderRight = "1"; pdfDoc.Open(); //jpg.SetAbsolutePosition(pdfDoc.Left, pdfDoc.Top - 60); //pdfDoc.Add(jpg); //PdfContentByte cb = writer.DirectContent; //cb.MoveTo(pdfDoc.Left, pdfDoc.Top-60 ); //cb.LineTo(pdfDoc.Right, pdfDoc.Top-60); //cb.SetLineWidth(1); //cb.SetColorStroke(new CMYKColor(0f, 12f, 0f, 7f)); //cb.Stroke(); //cb.MoveTo(pdfDoc.Left, pdfDoc.Top+5); //cb.LineTo(pdfDoc.Right, pdfDoc.Top+5); //cb.SetLineWidth(1); //cb.SetColorStroke(new CMYKColor(0f, 12f, 0f, 7f)); //cb.Stroke(); //Paragraph welcomeParagraph = new Paragraph("Hello, World!"); // Our custom Header and Footer is done using Event Handler //pdfDoc.Add(welcomeParagraph); XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, reader); //for (int i = 0; i <= 2; i++) //{ // // Define the page header // PageEventHandler.HeaderRight = i.ToString(); // if (i != 1) // { // pdfDoc.NewPage(); // } //} pdfDoc.Close(); bytes = memoryStream.ToArray(); memoryStream.Close(); } string name = pDFToolsObj.CustomerName; string[] words = name.Split(':'); string fname = Path.Combine(Server.MapPath("~/Content/Uploads/"), pDFToolsObj.HeaderText.ToString() + " FOR " + words[1] + ".pdf"); System.IO.File.WriteAllBytes(fname, bytes); string contentType = "application/pdf"; //Parameters to file are //1. The File Path on the File Server //2. The content type MIME type //3. The parameter for the file save by the browser return(File(fname, contentType, pDFToolsObj.HeaderText.ToString() + " FOR " + words[1] + ".pdf")); }
private void button2_Click(object sender, EventArgs e) { PDFTools.GenerarInformePDF(actual, saveDialog); }
private int CountPDFPages() { WPFDoEvents.AssertThisCodeIs_NOT_RunningInTheUIThread(); string cached_count_filename = MakeFilename_PageCount(); int num; // Try the cached version try { if (File.Exists(cached_count_filename)) { num = Convert.ToInt32(File.ReadAllText(cached_count_filename)); if (num > 0) { return(num); } // NOTE: all fringe cases, that is CORRUPTED and EMPTY documents, will be re-analyzed this way on every run! // This prevents odd page numbers from creeping into the persisted cache. } } catch (Exception ex) { Logging.Warn(ex, "There was a problem loading the cached page count."); } // Nuke the cache file, iff it exists. When we get here, it contained undesirable data anyway. FileTools.Delete(cached_count_filename); // If we get here, either the pagecount-file doesn't exist, or there was an exception Logging.Debug特("Calculating PDF page count for file {0}", DocumentPath); if (!DocumentExists) { num = -2; } else { num = PDFTools.CountPDFPages(DocumentPath, PDFPassword); Logging.Info("The result is {1} for using calculated PDF page count for file {0}", DocumentPath, num); if (num > 0) { try { Directory.CreateDirectory(Path.GetDirectoryName(cached_count_filename)); string outputText = Convert.ToString(num, 10); File.WriteAllText(cached_count_filename, outputText); // Open the file and read back what was written: we don't do a file lock for this one, but reckon all threads who // do this, will produce the same number = file content. // // Besides, we don't know if WriteAllText() is atomic; it probably is not. // Hence we read back to verify written content and if there's anything off about it, we // blow it out of the water and retry later, possibly. string readText = File.ReadAllText(cached_count_filename); if (readText != outputText) { throw new IOException("CountPDFPages: cache content as read back from cache file does not match written data. Cache file is untrustworthy."); } return(num); } catch (Exception ex) { Logging.Error(ex, "There was a problem using calculated PDF page count for file {0} / {1}", DocumentPath, cached_count_filename); FileTools.Delete(cached_count_filename); // we know the *calculated* pagecount is A-okay, so we can pass that on. // It's just the file I/O for caching the value that may have gone awry, // so we should retry that the next time we (re)calculate the pagecount number // as it is a heavy operation! // //num = 0; return(num); } } // Special case: when the CountPDFPages() API does not deliver a sane, that is positive, page count, // then we've got a PDF file issue on our hands, very probably a damaged PDF. // // Damaged PDF files SHOULD NOT burden us forever, hence we introduce the heuristic of Retry At Restart: // all "suspect" page counts are used as is for now, but when the application is restarted at any time // later, those documents will be inspected once again. // // we DO NOT count I/O errors around the cache file. ASSERT.Test(num <= 0); Logging.Warn("Marking this PDF Document as uncompromisingly stubborn in its failure to be inspected for file {0}. Page count code: {1}", DocumentPath, num); } return(num); }