public byte[] GetPDF(string pHTML)
        {
            byte[] bPDF = null;

            MemoryStream ms        = new MemoryStream();
            TextReader   txtReader = new StringReader(pHTML);

            var styles = new StyleSheet();

            styles.LoadTagStyle("table", "border", 1 + "px");
            styles.LoadTagStyle("th", "border", 1 + "px");
            styles.LoadTagStyle("td", "border", 1 + "px");

            styles.LoadTagStyle("table", "border-collapse", "collapse");
            styles.LoadTagStyle("th", "border-collapse", "collapse");
            styles.LoadTagStyle("td", "border-collapse", "collapse");

            // 1: create object of a itextsharp document class
            Document doc = new Document(new Rectangle(990, 712), 30, 30, 120, 25);

            // 2: we create a itextsharp pdfwriter that listens to the document and directs a XML-stream to a file
            PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms);

            using (var htmlWorker = new HTMLWorkerExtended(doc))
            {
                //try
                //{
                // 3: we create a worker parse the document
                //   HTMLWorker htmlWorker = new HTMLWorker(doc);


                //htmlWorker.SetStyleSheet(styles);
                // 4: we open document and start the worker on the document
                doc.Open();
                htmlWorker.StartDocument();

                // 5: parse the html into the document

                htmlWorker.Open();
                //htmlWorker.Parse(new StringReader("hello world"));
                htmlWorker.Parse(txtReader);
                //   XMLWorkerHelper.GetInstance().ParseXHtml(oPdfWriter, doc, txtReader);

                // 6: close the document and the worker
                htmlWorker.EndDocument();
                htmlWorker.Close();
                doc.Close();
                //}
                //catch (IOException ex)
                //{
                //  ex.ToString();
                //}
            }

            bPDF = ms.ToArray();

            return(bPDF);
        }
Пример #2
0
    /// <summary>
    /// Exporta un archivo en formato PDF para que el usuario lo pueda descargar
    /// </summary>
    /// <param name="archivo">El archivo que se desea exportar</param>
    public void ExportarPdf(ArchivoDTO archivo, bool rotar)
    {
        HttpContext.Current.Response.ContentType = "application/pdf";
        HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + archivo.Nombre + ".pdf");
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

        StringWriter   stringWriter   = new StringWriter();
        HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);

        stringWriter.Write(archivo.Datos);
        //StringReader stringReader = new StringReader(stringWriter.ToString());

        Document pdfDoc = new Document(rotar ? PageSize.A4.Rotate() : PageSize.A4, 20, 10, 10, 10);

        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

        PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
        pdfDoc.Open();

        PdfPTable t = new PdfPTable(2);

        t.SetWidthPercentage(new Single[] { 20F, 50F }, PageSize.A4);
        t.WidthPercentage = 98;

        /// HEADER
        Phrase texto = new Phrase();

        iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(AppDomain.CurrentDomain.BaseDirectory + "App_Themes\\Imagenes\\logo_Anses_Impresion.PNG");
        logo.ScaleAbsolute(50f, 30f);
        logo.SetAbsolutePosition(10, 800);

        iTextSharp.text.Font font_Bold = FontFactory.GetFont("arial", 18, iTextSharp.text.Font.BOLD);
        Paragraph            parrafo   = new Paragraph();

        if (archivo.Titulo.Contains("\n"))
        {
            Phrase p = new Phrase();
            p.Add(new Chunk(@archivo.Titulo, font_Bold));
            parrafo.Add(p);
            parrafo.Alignment = Element.ALIGN_RIGHT;
        }
        else
        {
            parrafo           = new Paragraph(@archivo.Titulo, font_Bold);
            parrafo.Alignment = Element.ALIGN_CENTER;
        }

        parrafo.SpacingBefore = 0;
        parrafo.SpacingAfter  = 0;
        parrafo.Add(logo);
        pdfDoc.Add(parrafo);

        using (TextReader htmlViewReader = new StringReader(stringWriter.ToString()))
        {
            using (var htmlWorker = new HTMLWorkerExtended(pdfDoc))
            {
                htmlWorker.Open();
                htmlWorker.Parse(htmlViewReader);
            }
        }

        pdfDoc.Close();
        HttpContext.Current.Response.Write(pdfDoc);
        HttpContext.Current.Response.OutputStream.Flush();
    }