将SQL数据导出为PDF。错误:HtmlParser

时间:2012-08-09 20:11:59

标签: c# sql pdf-writer

我想将Sql Data导出为PDF文件。 我使用了以下代码。 我的错误是 HtmlParser.Parse(Doc,xmlReader); 当前上下文中不存在“HtmlParser”名称

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using System.IO;
using System.Collections;
using System.Net;
using System.Data.SqlClient;
using System.Windows.Forms;


protected void btnPdf_Click(object sender, EventArgs e)
        {
            HtmlForm form = new HtmlForm();
            form.Controls.Add(StdA_grid);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hTextWriter = new HtmlTextWriter(sw);
            form.Controls[0].RenderControl(hTextWriter);
            string html = sw.ToString();
            Document Doc = new Document();

            //PdfWriter.GetInstance
            //(Doc, new FileStream(Request.PhysicalApplicationPath 
            //+ "\\AmitJain.pdf", FileMode.Create));

            PdfWriter.GetInstance
            (Doc, new FileStream(Environment.GetFolderPath
            (Environment.SpecialFolder.Desktop)
            + "\\AmitJain.pdf", FileMode.Create));
            Doc.Open();

            Chunk c = new Chunk
            ("Export GridView to PDF Using iTextSharp \n",
            FontFactory.GetFont("Verdana", 15));
            Paragraph p = new Paragraph();
            p.Alignment = Element.ALIGN_CENTER;
            p.Add(c);
            Chunk chunk1 = new Chunk
            ("By Amit Jain, amit_jain_online@yahoo.com \n",
            FontFactory.GetFont("Verdana", 8));
            Paragraph p1 = new Paragraph();
            p1.Alignment = Element.ALIGN_RIGHT;
            p1.Add(chunk1);

            Doc.Add(p);
            Doc.Add(p1);

            System.Xml.XmlTextReader xmlReader =
            new System.Xml.XmlTextReader(new StringReader(html));
            HtmlParser.Parse(Doc, xmlReader); // error shown on this line

            Doc.Close();
            string Path = Environment.GetFolderPath
            (Environment.SpecialFolder.Desktop)
            + "\\AmitJain.pdf";


            ShowPdf(Path);


        }

        private void ShowPdf(string strS)
        {
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "application/pdf";
            Response.AddHeader
            ("Content-Disposition", "attachment; filename=" + strS);
            Response.TransmitFile(strS);
            Response.End();
            //Response.WriteFile(strS);
            Response.Flush();
            Response.Clear();

        }

请为此提供一些解决方案,或者您可以建议我任何其他更好的程序。

1 个答案:

答案 0 :(得分:0)

尝试在您的设置中搜索HtmlParser课程。根据谷歌的快速搜索,在较新版本的iTextSharp中不存在HtmlParser。请参阅thisthis并搜索有关使用iTextSharp的任何帮助。从第二个链接,这里有一些可能有用的代码:

//make an arraylist ....with STRINGREADER since its no IO reading file...

            List<IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(htmlText), null);

            //add the collection to the document
            for (int k = 0; k < htmlarraylist.Count; k++)
            {
                document.Add((IElement)htmlarraylist[k]);
            }