Exemple #1
0
        public byte[] GetPDF(string pHTML)
        {
            byte[]     bPDF = null;
            FileStream fs   = new FileStream("Invioce.pdf", FileMode.Create, FileAccess.Write, FileShare.None);
            // MemoryStream ms = new MemoryStream();
            TextReader txtReader = new StringReader(pHTML);

            // 1: create object of a itextsharp document class
            Document doc = new Document(PageSize.A4, 25, 25, 25, 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, fs);

            // 3: we create a worker parse the document
            HTMLWorker htmlWorker = new HTMLWorker(doc);

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


            // 5: parse the html into the document
            htmlWorker.Parse(txtReader);

            // 6: close the document and the worker
            htmlWorker.EndDocument();
            htmlWorker.Close();
            doc.Close();

            bPDF = System.IO.File.ReadAllBytes("Invioce.pdf");

            return(bPDF);
        }
Exemple #2
0
        public static MemoryStream Create(string conteudo, string titulo, string assunto)
        {
            MemoryStream ms = new MemoryStream();

            using (TextReader textReader = new StringReader(conteudo))
            {
                Document document = new Document(PageSize.A4, 40, 40, 30, 30);

                PdfWriter pdfWriter = PdfWriter.GetInstance(document, ms);
                document.AddAuthor("Safe Life");
                document.AddCreationDate();
                document.AddTitle(titulo);
                document.AddCreator("Safe Life");
                document.AddSubject(assunto);
                System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
                pdfWriter.SetEncryption(null, encoding.GetBytes("sAfelIfe2017*"), PdfWriter.ALLOW_PRINTING, PdfWriter.STRENGTH128BITS);

                pdfWriter.CloseStream = false;

                HTMLWorker htmlWorker = new HTMLWorker(document);

                document.Open();
                htmlWorker.StartDocument();
                htmlWorker.Parse(textReader);


                htmlWorker.EndDocument();
                htmlWorker.Close();
                document.Close();
            }

            ms.Position = 0;
            return(ms);
        }
Exemple #3
0
        public static byte[] ConvertHtMLtoBytes(string pHtml)
        {
            var ms        = new MemoryStream();
            var txtReader = new StringReader(pHtml);

            // 1: create object of a itextsharp document class
            var doc = new Document(PageSize.A4, 25, 25, 25, 25);

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

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

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

            // 5: parse the html into the document
            htmlWorker.Parse(txtReader);

            // 6: close the document and the worker
            htmlWorker.EndDocument();
            htmlWorker.Close();
            doc.Close();

            var bPdf = ms.ToArray();

            return(bPdf);
        }
Exemple #4
0
        public void Render(ViewContext viewContext, TextWriter writer)
        {
            // generate view into string
            var sb = new System.Text.StringBuilder();
            TextWriter tw = new System.IO.StringWriter(sb);
            _result.View.Render(viewContext, tw);
            var resultCache = sb.ToString();

            var ms = new MemoryStream();
            var document = new Document();
            var pdfWriter = PdfWriter.GetInstance(document, ms);
            var worker = new HTMLWorker(document);
            document.Open();
            worker.StartDocument();

            pdfWriter.CloseStream = false;

            worker.Parse(new StringReader(resultCache));
            worker.EndDocument();
            worker.Close();
            document.Close();

            // this is as close as we can get to being "success" before writing output
            // so set the content type now
            viewContext.HttpContext.Response.ContentType = "application/pdf";
            pdfWriter.Flush();
            pdfWriter.Close();

            viewContext.HttpContext.Response.BinaryWrite(ms.ToArray());
        }
Exemple #5
0
        private void Html2Pdf(string htmlData, string pdfFilename)
        {
            using (MemoryStream ms = new MemoryStream())
                using (TextReader textReader = new StringReader(htmlData))
                {
                    iTextSharp.text.Document document = new iTextSharp.text.Document();

                    PdfWriter pdfWriter = PdfWriter.GetInstance(document, ms);
                    pdfWriter.CloseStream = false; // say to PdfWriter: do not close the MemoryStream

                    HTMLWorker htmlWorker = new HTMLWorker(document);

                    document.Open();
                    htmlWorker.StartDocument();
                    htmlWorker.Parse(textReader);

                    htmlWorker.EndDocument();
                    htmlWorker.Close();
                    document.Close();

                    ms.Position = 0; // reset position for further read
                    using (FileStream fs = System.IO.File.OpenWrite(pdfFilename))
                    {
                        using (ms)
                        {
                            ms.WriteTo(fs);
                        }
                    }
                }
        }
        private MemoryStream createPDF()
        {
            string html = BdyTextBox.Text;

            MemoryStream msOutput = new MemoryStream();

            TextReader reader = new StringReader(html);

            Document document = new Document(PageSize.A4, 30, 30, 30, 30);

            PdfWriter writer = PdfWriter.GetInstance(document, msOutput);

            HTMLWorker worker = new HTMLWorker(document);

            document.Open();

            worker.StartDocument();

            worker.Parse(reader);

            worker.EndDocument();

            worker.Close();

            document.Close();


            return(msOutput);
        }
        public static void htmltopdfWithHTML(string HTMLCode, string filePath, StyleSheet Css)
        {
            //StyleSheet styles = new StyleSheet();
            //styles.LoadStyle("seperator", "border-top", "#a9a9a9 2px solid");



            TextReader reader = new StringReader(HTMLCode);

            Document document = new Document(PageSize.A4, 10, 10, 10, 10);

            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create));

            HTMLWorker worker = new HTMLWorker(document);


            document.Open();
            worker.StartDocument();

            worker.Parse(reader);

            worker.EndDocument();
            worker.Close();
            document.Close();
        }
        private byte[] htmlTOPdf(string html)
        {
            byte[] pdfBytes;
            // string html = "<div style='background-color:yellow'>Demo !!</div>";
            MemoryStream msOutput = new MemoryStream();
            TextReader   reader   = new StringReader(html);
            // step 1: creation of a document-object
            Document document = new Document(PageSize.A4, 30, 30, 30, 30);

            // step 2:
            // we create a writer that listens to the document
            // and directs a XML-stream to a file
            PdfWriter writer = PdfWriter.GetInstance(document, msOutput);

            // step 3: we create a worker parse the document
            HTMLWorker worker = new HTMLWorker(document);

            // step 4: we open document and start the worker on the document
            document.Open();
            worker.StartDocument();

            // step 5: parse the html into the document
            worker.Parse(reader);

            // step 6: close the document and the worker
            worker.EndDocument();
            worker.Close();
            document.Close();
            pdfBytes = msOutput.ToArray();
            return(pdfBytes);
        }
Exemple #9
0
        public ActionResult ViewKBBReportInAppraisal(string kbbVehicleId, int appraisalId, int trimId, int PrintOption)
        {
            if (Session["Dealership"] == null)
            {
                return(RedirectToAction("LogOff", "Account"));
            }
            var dealer = (DealershipViewModel)Session["Dealership"];

            var reader = new StringReader(KellyBlueBookHelper.BuildKBBReportInHtmlForAppraisal(appraisalId, kbbVehicleId, trimId, dealer, PrintOption));

            var workStream = new MemoryStream();

            var document = new Document(PageSize.A4);

            PdfWriter.GetInstance(document, workStream).CloseStream = false;

            var worker = new HTMLWorker(document);

            document.Open();
            worker.StartDocument();
            worker.Parse(reader);
            worker.EndDocument();
            document.Close();

            byte[] byteInfo = workStream.ToArray();
            workStream.Write(byteInfo, 0, byteInfo.Length);
            workStream.Position = 0;

            return(new FileStreamResult(workStream, "application/pdf"));
        }
        private MemoryStream createPDF(string html)
        {
            MemoryStream msOutput = new MemoryStream();
            TextReader   reader   = new StringReader(html);

            // step 1: creation of a document-object
            Document document = new Document(PageSize.A4, 30, 30, 30, 30);

            // step 2:
            // we create a writer that listens to the document
            // and directs a XML-stream to a file
            PdfWriter writer = PdfWriter.GetInstance(document, msOutput);

            // step 3: we create a worker parse the document
            HTMLWorker worker = new HTMLWorker(document);

            // step 4: we open document and start the worker on the document
            document.Open();
            worker.StartDocument();

            // step 5: parse the html into the document
            worker.Parse(reader);

            // step 6: close the document and the worker
            worker.EndDocument();
            worker.Close();
            document.Close();

            return(msOutput);
        }
Exemple #11
0
        public ActionResult CreatePDF(int id)
        {
            RaceModel  result = MgtRace.GetInstance().GetRaceWithPoints(id).ToModel(false, true);
            Document   doc    = new Document(PageSize.LETTER, 50, 50, 50, 50);
            string     html   = RenderRazorViewToString("~/Views/Race/CreatePDF.cshtml", result);
            TextReader reader = new StringReader(html);

            using (MemoryStream output = new MemoryStream())
            {
                PdfWriter wri = PdfWriter.GetInstance(doc, output);

                // step 3: we create a worker parse the document
                HTMLWorker worker = new HTMLWorker(doc);

                doc.Open();

                worker.StartDocument();

                // step 5: parse the html into the document
                worker.Parse(reader);

                // step 6: close the document and the worker
                worker.EndDocument();
                worker.Close();

                doc.Close();
                return(File(output.ToArray(), "application/pdf", result.Title + ".pdf"));
            }
        }
Exemple #12
0
        public static byte[] GetPDF(string pHTML)
        {
            byte[] bPDF = null;

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

            // 1: create object of a itextsharp document class
            Document doc = new Document(PageSize.A4, 25, 25, 25, 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);

            // 3: we create a worker parse the document
            HTMLWorker htmlWorker = new HTMLWorker(doc);

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

            // 5: parse the html into the document
            htmlWorker.Parse(txtReader);

            // 6: close the document and the worker
            htmlWorker.EndDocument();
            htmlWorker.Close();
            doc.Close();

            bPDF = ms.ToArray();

            return(bPDF);
        }
        //Requiere: una string parseada de la vista que se quiere convertir en PDF
        //Modifica: se encarga de pasar la vista HTML a un documento de itextsharp
        //Regresa: un byte con el documento
        public byte[] GetPDF(string pHTML)
        {
            byte[] bPDF = null;

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

            // Se crea un documneto de itextsharp
            Document doc = new Document(PageSize.A4, 25, 25, 25, 25);

            PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms);

            // el htmlworker parsea el documento
            HTMLWorker htmlWorker = new HTMLWorker(doc);

            doc.Open();
            htmlWorker.StartDocument();

            // parsea el html en el doc
            htmlWorker.Parse(txtReader);

            htmlWorker.EndDocument();
            htmlWorker.Close();
            doc.Close();

            bPDF = ms.ToArray();

            return(bPDF);
        }
Exemple #14
0
        public void Render(ViewContext viewContext, TextWriter writer)
        {
            // generate view into string
            var        sb = new System.Text.StringBuilder();
            TextWriter tw = new System.IO.StringWriter(sb);

            _result.View.Render(viewContext, tw);
            var resultCache = sb.ToString();

            var ms        = new MemoryStream();
            var document  = new Document();
            var pdfWriter = PdfWriter.GetInstance(document, ms);
            var worker    = new HTMLWorker(document);

            document.Open();
            worker.StartDocument();

            pdfWriter.CloseStream = false;


            worker.Parse(new StringReader(resultCache));
            worker.EndDocument();
            worker.Close();
            document.Close();

            // this is as close as we can get to being "success" before writing output
            // so set the content type now
            viewContext.HttpContext.Response.ContentType = "application/pdf";
            pdfWriter.Flush();
            pdfWriter.Close();

            viewContext.HttpContext.Response.BinaryWrite(ms.ToArray());
        }
        public void Relatorio()
        {
            try
            {
                FisioterapeutaBusiness business = new FisioterapeutaBusiness();
                List <Fisioterapeuta>  lista    = business.ConsultarFisioterapeuta();

                StringBuilder conteudo = new StringBuilder();
                conteudo.Append("<h2>Relatório de fisioterapeutas</h2>");
                conteudo.Append("<p>TM Sistemas</p>");
                conteudo.Append($"<p>Relatório gerado em: {DateTime.Now}</p>");
                conteudo.Append("<br/>");
                conteudo.Append("<br/>");
                conteudo.Append("<table border='1' style='width: 100%'>");
                conteudo.Append("<tr>");
                conteudo.Append("<th>Nome</th>");
                conteudo.Append("<th>CPF / CNPJ</th>");
                conteudo.Append("<th>Crefito 2</th>");
                conteudo.Append("<th>Celular</th>");
                conteudo.Append("<th>Email</th>");
                conteudo.Append("</tr>");
                foreach (Fisioterapeuta fisioterapeuta in lista)
                {
                    conteudo.Append("<tr>");
                    conteudo.Append($"<td>{fisioterapeuta.FisiNome}</td>");
                    conteudo.Append($"<td>{fisioterapeuta.FisiCpfCnpj}</td>");
                    conteudo.Append($"<td>{fisioterapeuta.FisiCrefito}</td>");
                    conteudo.Append($"<td>{fisioterapeuta.FisiCelular}</td>");
                    conteudo.Append($"<td>{fisioterapeuta.FisiEmail}</td>");
                    conteudo.Append("</tr>");
                }
                conteudo.Append("</table>");
                byte[]       pdf    = null;
                MemoryStream ms     = new MemoryStream();
                TextReader   reader = new StringReader(conteudo.ToString());
                Document     doc    = new Document(PageSize.A4.Rotate(), 50, 50, 50, 50);
                PdfWriter    writer = PdfWriter.GetInstance(doc, ms);
                HTMLWorker   html   = new HTMLWorker(doc);
                doc.Open();
                html.StartDocument();
                html.Parse(reader);
                html.EndDocument();
                html.Close();
                doc.Close();
                pdf = ms.ToArray();
                Response.Clear();
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment; filename=Fisioterapeutas.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.BinaryWrite(pdf);
                Response.End();
            }
            catch (Exception e)
            {
                TempData["Mensagem"] = e.Message;
            }
        }
Exemple #16
0
        public void Relatorio()
        {
            try
            {
                AtendimentoBusiness business = new AtendimentoBusiness();
                List <Atendimento>  lista    = business.ConsultarAtendimento();

                StringBuilder conteudo = new StringBuilder();
                conteudo.Append("<h2>Relatório de atendimentos</h2>");
                conteudo.Append("<p>TM Sistemas</p>");
                conteudo.Append($"<p>Relatório gerado em: {DateTime.Now}</p>");
                conteudo.Append("<br/>");
                conteudo.Append("<br/>");
                conteudo.Append("<table border='1' style='width: 100%'>");
                conteudo.Append("<tr>");
                conteudo.Append("<th>Nome do paciente</th>");
                conteudo.Append("<th>Nome do fisioterapeuta</th>");
                conteudo.Append("<th>Data</th>");
                conteudo.Append("<th>Hora</th>");
                conteudo.Append("<th>Conduta</th>");
                conteudo.Append("</tr>");
                foreach (Atendimento atendimento in lista)
                {
                    conteudo.Append("<tr>");
                    conteudo.Append($"<td>{atendimento.Paciente.PaciNome}</td>");
                    conteudo.Append($"<td>{atendimento.Fisioterapeuta.FisiNome}</td>");
                    conteudo.Append($"<td>{atendimento.AtenData.ToString("dd/MM/yyyy")}</td>");
                    conteudo.Append($"<td>{atendimento.AtenHora.ToString("HH:mm")}</td>");
                    conteudo.Append($"<td>{atendimento.AtenConduta}</td>");
                    conteudo.Append("</tr>");
                }
                conteudo.Append("</table>");
                byte[]       pdf    = null;
                MemoryStream ms     = new MemoryStream();
                TextReader   reader = new StringReader(conteudo.ToString());
                Document     doc    = new Document(PageSize.A4.Rotate(), 50, 50, 50, 50);
                PdfWriter    writer = PdfWriter.GetInstance(doc, ms);
                HTMLWorker   html   = new HTMLWorker(doc);
                doc.Open();
                html.StartDocument();
                html.Parse(reader);
                html.EndDocument();
                html.Close();
                doc.Close();
                pdf = ms.ToArray();
                Response.Clear();
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment; filename=Atendimentos.pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.BinaryWrite(pdf);
                Response.End();
            }
            catch (Exception e)
            {
                TempData["Mensagem"] = e.Message;
            }
        }
Exemple #17
0
        private bool ConvertFiles(ref bool atLeastOneSuccess)
        {
            var success = false;
            var files   = SelectFiles();

            if (files.Length > 0)
            {
                foreach (FileInf file in files)
                {
                    try
                    {
                        string pdfPath = Path.Combine(Workflow.WorkflowTempFolder,
                                                      string.Format("{0}_{1:yyyy-MM-dd-HH-mm-ss-fff}.pdf", Path.GetFileNameWithoutExtension(file.FileName), DateTime.Now));

                        var doc = new Document();
                        PdfWriter.GetInstance(doc, new FileStream(pdfPath, FileMode.Create));
#pragma warning disable CS0612
                        var worker = new HTMLWorker(doc);
#pragma warning restore CS0612
                        doc.Open();
                        worker.StartDocument();
                        worker.Parse(new StreamReader(new FileStream(file.Path, FileMode.Open)));
                        worker.EndDocument();
                        worker.Close();
                        // Close the document
                        doc.Close();

                        Files.Add(new FileInf(pdfPath, Id));
                        InfoFormat("PDF {0} generated from the file {1}", pdfPath, file.Path);

                        if (!atLeastOneSuccess)
                        {
                            atLeastOneSuccess = true;
                        }
                    }
                    catch (ThreadAbortException)
                    {
                        throw;
                    }
                    catch (Exception e)
                    {
                        ErrorFormat("An error occured while generating the PDF of the file {0}", e, file.Path);
                        success = false;
                    }
                }
            }
            return(success);
        }
Exemple #18
0
        public static byte[] ConvertToPdf(string htmlContent)
        {
            var ms     = new MemoryStream();
            var reader = new StringReader(htmlContent);
            var doc    = new Document(PageSize.A4, 50, 50, 50, 50);
            var writer = PdfWriter.GetInstance(doc, ms);
            var html   = new HTMLWorker(doc);

            doc.Open();
            html.StartDocument();
            html.Parse(reader);
            html.EndDocument();
            html.Close();
            doc.Close();
            return(ms.ToArray());
        }
Exemple #19
0
        /// <summary>
        /// This method reads an html file
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        string ReadHtml(string path)
        {
            MemoryStream output   = new MemoryStream();
            string       htmlData = output.ToString();
            TextReader   reader   = new StreamReader(path);
            Document     document = new Document(PageSize.A4, 30, 30, 30, 30);
            PdfWriter    writer   = PdfWriter.GetInstance(document, output);
            HTMLWorker   worker   = new HTMLWorker(document);

            document.Open();
            worker.StartDocument();
            worker.Parse(reader);
            worker.EndDocument();
            worker.Close();
            document.Close();
            return(htmlData);
        }
Exemple #20
0
        public ActionResult InscritsToPDF(int id)
        {
            InscritsView view = new InscritsView();

            List <Participant>   partipants = MgtParticipant.GetInstance().GetAllByIdCourse(id);
            List <PersonneModel> personnes  = new List <PersonneModel>();

            foreach (Participant part in partipants)
            {
                PersonneModel model = MgtPersonne.GetInstance().GetPersonneById(part.IdPersonne).ToModel();
                model.participant = part.ToModel();
                personnes.Add(model);
            }

            view.Course       = MgtRace.GetInstance().GetRace(id).ToModel();
            view.personnes    = personnes;
            view.nbInscrits   = personnes.Count;
            view.inscriptions = initInscriptions(partipants);

            Document   doc    = new Document(PageSize.LETTER, 50, 50, 50, 50);
            string     html   = RenderRazorViewToString("~/Views/Race/InscritsToPDF.cshtml", view);
            TextReader reader = new StringReader(html);

            using (MemoryStream output = new MemoryStream())
            {
                PdfWriter wri = PdfWriter.GetInstance(doc, output);

                // step 3: we create a worker parse the document
                HTMLWorker worker = new HTMLWorker(doc);

                doc.Open();

                worker.StartDocument();

                // step 5: parse the html into the document
                worker.Parse(reader);

                // step 6: close the document and the worker
                worker.EndDocument();
                worker.Close();

                doc.Close();
                return(File(output.ToArray(), "application/pdf", "Inscriptions" + view.Course.Title + ".pdf"));
            }
        }
Exemple #21
0
        public byte[] GetPDF(string conteudo)
        {
            byte[]       pdf    = null;
            MemoryStream ms     = new MemoryStream();
            TextReader   reader = new StringReader(conteudo);
            Document     doc    = new Document(PageSize.A4, 50, 50, 50, 50);
            PdfWriter    writer = PdfWriter.GetInstance(doc, ms);
            HTMLWorker   html   = new HTMLWorker(doc);

            doc.Open();
            html.StartDocument();
            html.Parse(reader);
            html.EndDocument();
            html.Close();
            doc.Close();
            pdf = ms.ToArray();
            return(pdf);
        }
Exemple #22
0
        public static byte[] GetPDF(string pHTML)
        {
            byte[] bPDF = null;



            MemoryStream ms        = new MemoryStream();
            TextReader   txtReader = new StringReader(pHTML);
            //var fontFamily = FontFactory.GetFont("Arial", 14, Font.BOLD);

            // 1: create object of a itextsharp document class
            Document doc = new Document(PageSize.LETTER, 60f, 60f, 50f, 50f);


            // 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);


            // 3: we create a worker parse the document
            HTMLWorker htmlWorker = new HTMLWorker(doc);

            //var style = new StyleSheet();
            //style.LoadTagStyle("body", "size", "19px");
            //style.LoadTagStyle("body", "font-family", "Calibri");
            //htmlWorker.SetStyleSheet(style);

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

            // 5: parse the html into the document
            htmlWorker.Parse(txtReader);



            // 6: close the document and the worker
            htmlWorker.EndDocument();
            htmlWorker.Close();
            doc.Close();

            bPDF = ms.ToArray();

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

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

            // 1: create object of a itextsharp document class
            Document doc = new Document(PageSize.A4, 25, 25, 25, 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);

            // 3: we create a worker parse the document
            HTMLWorker htmlWorker = new HTMLWorker(doc);

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

            // step 4.1: register a unicode font and assign it an allias
            FontFactory.Register("C:\\Windows\\Fonts\\ARIALUNI.TTF", "arial unicode ms");

            // step 4.2: create a style sheet and set the encoding to Identity-H
            StyleSheet ST = new StyleSheet();

            ST.LoadTagStyle("body", "encoding", "Identity-H");

            // step 4.3: assign the style sheet to the html parser
            htmlWorker.SetStyleSheet(ST);

            htmlWorker.StartDocument();

            // 5: parse the html into the document
            htmlWorker.Parse(txtReader);

            // 6: close the document and the worker
            htmlWorker.EndDocument();
            htmlWorker.Close();
            doc.Close();

            bPDF = ms.ToArray();

            return(bPDF);
        }
        private byte[] GetPDF(string inputHtml)
        {
            byte[] pdfBytes          = null;
            var    ms                = new MemoryStream();
            var    txtReader         = new StringReader(inputHtml);
            var    doc               = new Document(PageSize.A4, 25, 25, 25, 25);
            var    pdfWriterInstnace = PdfWriter.GetInstance(doc, ms);
            var    htmlWorker        = new HTMLWorker(doc);

            doc.Open();
            htmlWorker.StartDocument();
            htmlWorker.Parse(txtReader);
            htmlWorker.EndDocument();
            htmlWorker.Close();
            doc.Close();
            pdfBytes = ms.ToArray();

            return(pdfBytes);
        }
Exemple #25
0
        public byte[] RenderFromHtml(string html)
        {
            var output = new MemoryStream();

            using (var reader = new StringReader(html)) {
                var document = new Document();
                var worker   = new HTMLWorker(document);

                document.Open();
                worker.StartDocument();
                worker.Parse(reader);

                worker.EndDocument();
                worker.Close();
                document.Close();

                return(output.ToArray());
            }
        }
Exemple #26
0
        public MemoryStream WritePdf(string bodyPdf)
        {
            var workStream = new MemoryStream();
            var reader     = new StringReader(bodyPdf);
            var document   = new Document(PageSize.A4);

            PdfWriter.GetInstance(document, workStream).CloseStream = false;
            var worker = new HTMLWorker(document);

            document.Open();
            worker.StartDocument();
            worker.Parse(reader);
            worker.EndDocument();
            document.Close();
            byte[] byteInfo = workStream.ToArray();
            workStream.Write(byteInfo, 0, byteInfo.Length);
            workStream.Position = 0;
            return(workStream);
        }
Exemple #27
0
        public MemoryStream ParseHtml(string html)
        {
            MemoryStream ms        = new MemoryStream();
            Document     document  = new Document();
            PdfWriter    pdfWriter = PdfWriter.GetInstance(document, ms);
            HTMLWorker   worker    = new HTMLWorker(document);

            document.Open();
            worker.StartDocument();
            pdfWriter.CloseStream = false;
            worker.Parse(new StringReader(html));
            worker.EndDocument();
            worker.Close();
            document.CloseDocument();
            document.Close();
            pdfWriter.Flush();
            pdfWriter.Close();

            return(ms);
        }
Exemple #28
0
        public static byte[] GetPDF(string pHTML)
        {
            byte[] bPDF = null;

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

            // 1: create object of a itextsharp document class
            Document doc = new Document(PageSize.A4, 25, 25, 25, 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);

            // 3: we create a worker parse the document
            HTMLWorker htmlWorker = new HTMLWorker(doc);

            StyleSheet styles = new StyleSheet();

            //styles.LoadTagStyle("th", "face", "helvetica");
            //styles.LoadTagStyle("span", "size", "10px");
            //styles.LoadTagStyle("span", "face", "helvetica");
            //styles.LoadTagStyle("td", "size", "10px");
            styles.LoadTagStyle("body", HtmlTags.FONTFAMILY, "times-roman");
            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.Parse(txtReader);

            // 6: close the document and the worker
            htmlWorker.EndDocument();
            htmlWorker.Close();
            doc.Close();

            bPDF = ms.ToArray();

            return(bPDF);
        }
        private void createPDF(string html)
        {
            //MemoryStream msOutput = new MemoryStream();
            TextReader reader   = new StringReader(html);// step 1: creation of a document-object
            Document   document = new Document(PageSize.A4, 30, 30, 30, 30);

            Response.ContentType = "Application/pdf";
            // step 2:
            // we create a writer that listens to the document
            // and directs a XML-stream to a file
            PdfWriter writer = PdfWriter.GetInstance(document, Response.OutputStream);//new FileStream("Test.pdf", FileMode.Create));

            // step 3: we create a worker parse the document
            HTMLWorker worker = new HTMLWorker(document);

            // step 4: we open document and start the worker on the document
            document.Open();

            // step 4.1: register a unicode font and assign it an allias
            FontFactory.Register("C:\\Windows\\Fonts\\ARIALUNI.TTF", "arial unicode ms");

            // step 4.2: create a style sheet and set the encoding to Identity-H
            iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet();
            ST.LoadTagStyle("body", "encoding", "Identity-H");

            // step 4.3: assign the style sheet to the html parser
            worker.SetStyleSheet(ST);

            worker.StartDocument();

            // step 5: parse the html into the document
            worker.Parse(reader);

            // step 6: close the document and the worker
            worker.EndDocument();
            worker.Close();
            document.Close();
            Response.End();
        }
Exemple #30
0
        private byte[] CreatePDF(string htmlReplaced)
        {
            var          document   = new Document(PageSize.A4.Rotate(), 0, 0, 0, 0);
            MemoryStream memStream  = new MemoryStream();
            var          writer     = PdfWriter.GetInstance(document, memStream);
            var          htmlReader = new StringReader(htmlReplaced);
            var          htmlWorker = new HTMLWorker(document);

            document.Open();

            document.Add(img);

            htmlWorker.StartDocument();

            htmlWorker.Parse(htmlReader);

            htmlWorker.EndDocument();
            htmlWorker.Close();

            document.Close();
            return(memStream.ToArray());
        }
        public FileResult DownloadAppointments(string htmlApts, DateTime requestDate)
        {
            using (MemoryStream stream = new System.IO.MemoryStream())
            {
                StringReader sr     = new StringReader(htmlApts);
                Document     pdfDoc = new Document(PageSize.A4, 10f, 10f, 30f, 20f);

                var writer = PdfWriter.GetInstance(pdfDoc, stream);

                var htmlWorker = new HTMLWorker(pdfDoc);

                pdfDoc.Open();
                htmlWorker.StartDocument();
                htmlWorker.Parse(sr);

                htmlWorker.EndDocument();
                htmlWorker.Close();
                pdfDoc.Close();

                return(File(stream.ToArray(), "application/pdf", "Appointments_" + requestDate.ToString("dd-MM-yyyy") + ".pdf"));
            }
        }