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);
        }
        public byte[] Render(string htmlText, string pageTitle, string orientation)
        {
            byte[] renderedBuffer;

            using (var outputMemoryStream = new MemoryStream())
            {
                try
                {
                    if (orientation == "portrait")
                    {
                        using (var pdfDocument = new Document(PageSize.A4, HorizontalMargin, HorizontalMargin, VerticalMargin, VerticalMargin))
                        {
                            PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDocument, outputMemoryStream);
                            pdfWriter.CloseStream = false;
                            pdfWriter.PageEvent   = new PrintHeaderFooter {
                                Title = pageTitle
                            };
                            pdfDocument.Open();
                            using (var htmlViewReader = new StringReader(htmlText))
                            {
                                using (var htmlWorker = new HTMLWorker(pdfDocument))
                                {
                                    htmlWorker.Parse(htmlViewReader);
                                }
                            }
                        }
                    }
                    else
                    {
                        using (var pdfDocument = new Document(PageSize.A4.Rotate(), HorizontalMargin, HorizontalMargin, VerticalMargin, VerticalMargin))
                        {
                            PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDocument, outputMemoryStream);
                            pdfWriter.CloseStream = false;
                            pdfWriter.PageEvent   = new PrintHeaderFooter {
                                Title = pageTitle
                            };
                            pdfDocument.Open();
                            using (var htmlViewReader = new StringReader(htmlText))
                            {
                                StyleSheet Style = new StyleSheet();
                                Dictionary <string, object> Provider = new Dictionary <string, object>();
                                foreach (IElement el in iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(htmlViewReader, Style))
                                {
                                    if (el is PdfPTable)
                                    {
                                        //((PdfPTable)el).HeaderRows = 1;
                                        //if ((PdfPTable)el == 791.00)
                                        if (((iTextSharp.text.pdf.PdfPTable)(el)).TotalWidth == 791.5)
                                        {
                                            ((PdfPTable)el).HeaderRows = 1;
                                        }
                                        if (((iTextSharp.text.pdf.PdfPTable)(el)).TotalWidth == 650)
                                        {
                                            pdfDocument.NewPage();
                                        }
                                        if (((iTextSharp.text.pdf.PdfPTable)(el)).TotalWidth == 750)
                                        {
                                            pdfDocument.NewPage();
                                        }
                                    }
                                    pdfDocument.Add(el);
                                }
                                using (var htmlWorker = new HTMLWorkerExtended(pdfDocument))
                                {
                                    htmlWorker.Parse(htmlViewReader);
                                }
                            }
                        }
                    }
                    renderedBuffer = new byte[outputMemoryStream.Position];
                    outputMemoryStream.Position = 0;
                    outputMemoryStream.Read(renderedBuffer, 0, renderedBuffer.Length);
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
            }
            return(renderedBuffer);
        }
Beispiel #3
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();
    }