I have two buttons. One button sends document to mail and the other button print the document (download as PDF file). They both do these functions but there is something missing.
After preparing a receipt on my web page it automatically generates a QR code for that receipt, which then navigates to next page for printing. When printing, it should print the QR code image alongside the document. That is, the QR code image should be on the document when it prints or when it sends to mail, BUT IT DOES NOT HAVE THE QR CODE WHEN IT PRINTS OR SENDS TO EMAIL.
Here is the “print” and “send to mail” buttons code
Print (download PDF document)
protected void Button3_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security = True");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT * FROM InvoiceTable WHERE Invoice_no = '" + Session["InvoiceID"] + "'";
cmd.Connection = con;
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
sda.SelectCommand = cmd;
sda.Fill(ds, "detail");
if (ds.Tables[0].Rows.Count > 0)
{
byte[] image = (byte[])ds.Tables[0].Rows[0]["logo"];
imgFileUpload.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(image);
string fileName = "Invoice" + DateTime.Now.ToString() + ".pdf";
byte[] QRBytes = GetQRCodeBytes(ToAbsoluteUrl("/invoiceverificationtemp.aspx") + "?Id=" + Request.QueryString["InvoiceID"]);
Image1.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String(QRBytes);
var ImgUrl = Image1.ImageUrl;
var ImagUrl = imgFileUpload.ImageUrl;
// Write image as file to folder.
File.WriteAllBytes(Server.MapPath("qrimg.jpg"), QRBytes);
File.WriteAllBytes(Server.MapPath("logo.jpg"), image);
// convert and set absolute url in image.
Image1.ImageUrl = GetUrl("qrimg.jpg");
imgFileUpload.ImageUrl = GetUrl("logo.jpg");
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Panel1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
PdfWriter PdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
cssResolver.AddCssFile(Server.MapPath("~/css/style2.css"), true);
IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDoc, PdfWriter)));
var worker = new XMLWorker(pipeline, true);
var xmlParse = new XMLParser(true, worker);
pdfDoc.Open();
xmlParse.Parse(sr);
xmlParse.Flush();
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + fileName + ";");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
// Delete the temp image.
File.Delete(Server.MapPath("qrimg.jpg"));
File.Delete(Server.MapPath("logo.jpg"));
Image1.ImageUrl = ImgUrl;
imgFileUpload.ImageUrl = ImagUrl;
Response.End();
}
}
Send Document (send to email)
protected void Button1_Click(object sender, EventArgs e)
{
byte[] QRBytes = GetQRCodeBytes(ToAbsoluteUrl("/invoiceverificationtemp.aspx") + "?Id=" + Request.QueryString["InvoiceID"]);
Image1.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String(QRBytes);
var ImgUrl = Image1.ImageUrl;
// Write image as file to folder.
File.WriteAllBytes(Server.MapPath("qrimg.jpg"), QRBytes);
// convert and set absolute url in image.
Image1.ImageUrl = GetUrl("qrimg.jpg");
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
this.Panel1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
// HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
using (MemoryStream memoryStream = new MemoryStream())
{
PdfWriter PdfWriter = PdfWriter.GetInstance(pdfDoc, memoryStream);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
cssResolver.AddCssFile(Server.MapPath("~/css/style2.css"), true);
IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDoc, PdfWriter)));
var worker = new XMLWorker(pipeline, true);
var xmlParse = new XMLParser(true, worker);
pdfDoc.Open();
xmlParse.Parse(sr);
xmlParse.Flush();
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
MailMessage mm = new MailMessage("mannyrchrd@gmail.com", this.TextBox1.Text);
mm.Subject = "INVOICE DOCUMENT";
mm.Body = "Please find Attached Invoice Document";
mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "Invoice.pdf"));
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "relay-hosting.secureserver.net";
smtp.EnableSsl = false;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "mannyrchrd@gmail.com";
NetworkCred.Password = "******************";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 25;
smtp.Send(mm);
}
}
}
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Document Sent!');", true);
}