示例#1
0
        private void ExportGridToPDF()
        {
            string dt = System.DateTime.Now.ToString("MM/yyyy");

            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=StudentFeesReport" + dt + ".pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter   sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);

            iTextSharp.text.html.simpleparser.StyleSheet ST = new iTextSharp.text.html.simpleparser.StyleSheet();
            ST.LoadTagStyle("body", "encoding", "Identity-H");

            Panel1.RenderControl(hw);
            StringReader sr     = new StringReader(sw.ToString());
            Document     pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);

            iTextSharp.text.html.simpleparser.HTMLWorker htmlparser = new iTextSharp.text.html.simpleparser.HTMLWorker(pdfDoc);
            htmlparser.Style = ST;
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End();
            GridView1.AllowPaging = true;
            GridView1.DataBind();
        }
示例#2
0
        public string Convert(string html, string location, string pdfName = "")
        {
            string retValue = string.Empty;

            try
            {
                if (string.IsNullOrEmpty(pdfName))
                {
                    pdfName = "GenericPDF";
                }

                string path = location + string.Format("{0}.pdf", pdfName);
                if (File.Exists(path))
                {
                    File.Delete(path);
                }

                // Step 1: Creating System.IO.FileStream object
                using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
                    // Step 2: Creating iTextSharp.text.Document object
                    using (Document doc = new Document())
                        // Step 3: Creating iTextSharp.text.pdf.PdfWriter object
                        // It helps to write the Document to the Specified FileStream
                        using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
                        {
                            // Step 4: Openning the Document
                            doc.Open();

                            iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
                            iTextSharp.text.html.simpleparser.HTMLWorker hw     = new iTextSharp.text.html.simpleparser.HTMLWorker(doc);
                            string newhtml = "";
                            newhtml = html.Replace(System.Environment.NewLine, "");
                            hw.Parse(new StringReader(newhtml));
                            //document.Close();

                            //// Step 5: Adding a paragraph
                            //// NOTE: When we want to insert text, then we've to do it through creating paragraph
                            //doc.Add( new Paragraph( html ) );

                            // Step 6: Closing the Document
                            doc.Close();
                        }
            }
            catch (DocumentException de)
            {
                retValue = string.Format("Error: {0}", de.Message);
            }
            catch (IOException ioe)
            {
                retValue = string.Format("Error: {0}", ioe.Message);
            }
            return(retValue);
        }
示例#3
0
        public static iTextSharp.text.html.simpleparser.StyleSheet GetStyles()
        {
            iTS.html.simpleparser.StyleSheet styles =
                new iTextSharp.text.html.simpleparser.StyleSheet();

            styles.LoadTagStyle("ul", "indent", "10");
            styles.LoadTagStyle("ol", "indent", "10");
            styles.LoadTagStyle("br", "font-size", "50%");
            //styles.LoadTagStyle("center", "text-align", "center");
            //styles.LoadTagStyle("p.center", "text-align", "center");
            //styles.LoadTagStyle("p", )

            return(styles);
        }
示例#4
0
        public void HTMLToPdf(string HTML, string FilePath)
        {
            Document document = new Document();

            PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\Chap0101.pdf", FileMode.Create));
            document.Open();
            Image pdfImage = Image.GetInstance(Server.MapPath("\\store.png"));

            pdfImage.ScaleToFit(100, 50);

            pdfImage.Alignment = iTextSharp.text.Image.UNDERLYING; pdfImage.SetAbsolutePosition(180, 760);

            document.Add(pdfImage);
            iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
            iTextSharp.text.html.simpleparser.HTMLWorker hw     = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
            hw.Parse(new StringReader(HTML));
            document.Close();
            Response.ContentType = "application/pdf";

            //Set default file Name as current datetime
            Response.AddHeader("content-disposition", "attachment; filename=" + DateTime.Now.ToString("yyyyMMdd") + ".pdf");
            System.Web.HttpContext.Current.Response.Write(document);
            ShowPdf("\\Chap0101.pdf");
        }
示例#5
0
        public void HTMLToPdf(string HTML, string FilePath)
        {
            Document document = new Document();

            PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\Chap0101.pdf", FileMode.Create));
            document.Open();
            Image pdfImage = Image.GetInstance(Server.MapPath("\\store.png"));

            pdfImage.ScaleToFit(100, 50);

            pdfImage.Alignment = iTextSharp.text.Image.UNDERLYING; pdfImage.SetAbsolutePosition(180, 760);

            document.Add(pdfImage);
            iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
            iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
            hw.Parse(new StringReader(HTML));
            document.Close();
            Response.ContentType = "application/pdf";

            //Set default file Name as current datetime
            Response.AddHeader("content-disposition", "attachment; filename=" + DateTime.Now.ToString("yyyyMMdd") + ".pdf");
            System.Web.HttpContext.Current.Response.Write(document);
            ShowPdf("\\Chap0101.pdf");
        }