iTextSharp PDF coming blank

Asked

Viewed 129 times

1

Good afternoon guys, I have the following code:

 public void GerarPDF(long id)
    {
        string HTMLemString = RenderizaHtmlComoString("~/Views/item/item.cshtml", id);
        var regex = new Regex("(\\<script(.+?)\\</script\\>)|(\\<style(.+?)\\</style\\>)",
            RegexOptions.Singleline | RegexOptions.IgnoreCase);
        HTMLemString = regex.Replace(HTMLemString, "");
        string CSSdocumento = CSSemString();
        Byte[] bytes;

        using (var ms = new MemoryStream())
        {
            using (var doc = new Document())
            {
                using (var writer = PdfWriter.GetInstance(doc, ms))
                {
                    doc.Open();
                    var HTMLconversão = '@' + HTMLemString;
                    var CSSconversão = '@' + CSSdocumento;

                    using (var msCss = new MemoryStream(System.Text.ASCIIEncoding.UTF8.GetBytes(CSSconversão)))
                    {
                        using (var msHtml = new MemoryStream(System.Text.ASCIIEncoding.UTF8.GetBytes(HTMLconversão)))
                        {
                            iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, msCss);
                        }
                    }

                    doc.Close();
                }
            }

            bytes = ms.ToArray();
        }

        new XmlController().SalvarPDF(bytes);

    }

The code that generates the PDF, and below where I get the PDF and use it:

 byte[] PDF = PDFparaAnexo;
 List<MemoryStream> anexos = new List<MemoryStream>();
        anexos.Add(new MemoryStream(PDF));

And below where I gather the file that is in a memorystream, with its name:

int i = 0;
            foreach (Stream anx in pAnex)
            {
                try
                {
                    Attachment attached = new Attachment(anx, pFileName[i]);
                    mm.Attachments.Add(attached);
                    i++;
                }
                catch (Exception ex)
                {

                }

            }

Only that the file is going in PDF format but this blank, someone would know why?

  • Already found the problem? Already tested this regex to see if it is removing all text?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.