HtmlParser不起作用

时间:2016-03-22 07:17:56

标签: c# asp.net itextsharp

我使用了以下参考资料

using System;
using System.Collections;
using System.Configuration;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Specialized;
using System.Text;
using System.Data.SqlClient;
using System.Globalization;
using System.IO;
using System.Text;
using System.Xml;

这是代码:

protected void ExportToPDFClick(object sender, EventArgs e)
{
    Response.Clear();
    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    gvCustomers.RenderControl(htw);
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment; filename=MypdfFile.pdf");
    Document document = new Document();
    PdfWriter.GetInstance(document, Response.OutputStream);
    document.Open();
    string html = sb.ToString();
    XmlTextReader reader = new XmlTextReader(new StringReader(html));
    HtmlParser.Parse(document, reader); //did'nt work, line shows error tells  missing reference
    document.Close();
    sw.Close();
    Response.Flush();
    Response.End();
}

请参阅错误显示的注释。我错过了什么?或者还有其他选择吗?不起作用的陈述是:

   HtmlParser.Parse(document,reader);  

2 个答案:

答案 0 :(得分:0)

尝试更改此行

 XmlTextReader reader = new XmlTextReader(new StringReader(html));
    HtmlParser.Parse(document, reader);

到这个

HTMLWorker worker = new HTMLWorker(document);
    worker.Parse(new StringReader(html));
    document.Close();

确保添加 iTextSharp.text.html.simpleparser HTMLWorker

的命名空间

答案 1 :(得分:-1)

使用此

风格助手

internal static class ItextPDFStyleHelper
    {
        internal static StyleSheet ApplyStyleSheetToHtml()
        {
            var styles = new StyleSheet();
            styles.LoadTagStyle("body", "font-family", "Arial");
            styles.LoadTagStyle("table", "width", "95%");
            styles.LoadTagStyle("table", "font-size", "14px");
            styles.LoadTagStyle("table", "letter-spacing", ".004em");
            styles.LoadTagStyle("table", "border", "1");
            styles.LoadTagStyle("table", "border-spacing", "0px");
            styles.LoadTagStyle("table", "border-collapse", "collapse");

            styles.LoadTagStyle("td", "border", "1");
            styles.LoadTagStyle("td", "border-spacing", "0");
            styles.LoadTagStyle("td", "border-collapse", "collapse");

            styles.LoadTagStyle("th", "text-align", ".004em");
            styles.LoadTagStyle("th", "color", "1px solid black;");
            styles.LoadTagStyle("th", "bgcolor", "#f5a843");
            styles.LoadTagStyle("th", "border", "1");
            styles.LoadTagStyle("th", "border-spacing", "0");
            styles.LoadTagStyle("th", "border-collapse", "collapse");

            styles.LoadStyle("travel", "text-align", "center");
            styles.LoadStyle("travel", "Color", "black;");
            styles.LoadStyle("travel", "font-weight", "bold");
            styles.LoadStyle("travel", "bgcolor", "#f5a843");
            styles.LoadStyle("travel", "border", "1");
            styles.LoadStyle("travel", "border-spacing", "0");
            styles.LoadStyle("travel", "border-collapse", "collapse");
            return styles;
        }
    }

获取FileStream

            MemoryStream memStream = new MemoryStream();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=Data.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
            PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            FontFactory.RegisterDirectories();
            var styles = ItextPDFStyleHelper.ApplyStyleSheetToHtml();
            StringReader sr = new StringReader(Data);
            foreach (var element in HTMLWorker.ParseToList(sr, styles))
            {
                pdfDoc.Add(element);
            }
            pdfDoc.Close();
            Response.Write(pdfDoc);
            memStream.WriteTo(Response.OutputStream);

            memStream.Position = 0;

            var file = File(memStream, "application/pdf");
            Response.End();
            return file;

即使HTMLWorker类过时,它也能正常工作 但是如果你想使用XMLWorkerHelper

private FileStreamResult CreatePdfFileStreamResult(string Data)
        {
            MemoryStream memStream = new MemoryStream();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=Data.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
            PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            StringReader sr = new StringReader(Data);
            XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);            
            pdfDoc.Close();            
            Response.Write(pdfDoc);            
            memStream.WriteTo(Response.OutputStream);
            memStream.Position = 0;
            var file = File(memStream, "application/pdf");
            Response.End();
            return file;
        }