I am traying to convert a specific DIV content into PDF via iTextSharp - MemoryStream. Here goes my DIV Content:

<div id="ekstre" style="width: 210mm; min-height: 297mm; border: 0px solid #000000; margin-left: 0px;" runat="server">
.........
</div>

I have 2 Gridview in this DIV; and also a few asp labels which I bind data from sqlServer Database.

and here my codeBehind which I try to convert that DIV into PDF in order to put it in email attachment without saving to disk:

byte[] bytes = null;
        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw))
            {
                sw.Flush();
                ekstre.RenderControl(hw);
                hw.Flush();
                StringReader sr = new StringReader(sw.ToString());
                Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0.0f);

                MemoryStream memoryStream = new MemoryStream();


                    PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, memoryStream);
                pdfWriter.Flush();

                    pdfDoc.Open();

                    //pdfDoc.Add(new Paragraph(sr.ReadToEnd()));

                    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
                    htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
                    ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);

                    cssResolver.AddCssFile(Server.MapPath("~/assets/CariEkstre_Style.css"), true);
                    IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(pdfDoc, pdfWriter)));

                    XMLWorker worker = new XMLWorker(pipeline, true);
                    XMLParser xmlParse = new XMLParser(true, worker);

                    xmlParse.Parse(sr);
                    xmlParse.Flush();

                    pdfWriter.CloseStream = false;

                    pdfDoc.Close();


                memoryStream.Position = 0;

                bytes = memoryStream.ToArray();

                memoryStream.Close();
                //-------------------------------
                // EMAIL gönderiliyor

                string from = "from@from.com";
                    string to = "to@to.com";
                    string bcc = "";
                    string cc = "";
                    string subject = "strSubject";
                    string body = "strBody";



                    MailMessage mMailMessage = new MailMessage();
                    mMailMessage.From = new MailAddress(from);
                    mMailMessage.To.Add(new MailAddress(to));

                    if ((bcc != null) && (bcc != string.Empty))
                    {
                        // Set the Bcc address of the mail message
                        mMailMessage.Bcc.Add(new MailAddress(bcc));
                    }     
                    if ((cc != null) && (cc != string.Empty))
                    {

                        mMailMessage.CC.Add(new MailAddress(cc));
                    }       
                    mMailMessage.Subject = subject;
                    mMailMessage.Body = body;


                    mMailMessage.Attachments.Add(new Attachment(new MemoryStream(bytes), "CariEkstre.pdf"));


                    mMailMessage.IsBodyHtml = true;
                    mMailMessage.Priority = MailPriority.Normal;
                    SmtpClient mSmtpClient = new SmtpClient();
                    mSmtpClient.Send(mMailMessage);

I checked my email box and I always see a BLANK PDF attached. I tried a lot of StackOverflow searching and googling; but I did not find where is the problem.

Any help will be appreciated.

Related posts

Recent Viewed