2016-03-22 50 views
0

我已經使用了以下參考HTMLParser的沒有工作

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(); 
} 

看到評論,其中錯誤顯示。我錯過了什麼?還是有其他的選擇?該did'nt工作的說法是:

HtmlParser.Parse(document,reader); 
+0

itextSharp已更改命名空間,試試這個'iTextSharp.text.html.simpleparser.HTMLWorker.Parse(文檔閱讀器);' – Webruster

回答

-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; 
     } 
+0

Downvoting,因爲'HTMLWorker'已被棄用。你應該使用新的'XMLWorker'。 –

+0

@AmedeeVanGasse它給出了使用XMLWorker的錯誤。哪些引用需要添加? –

+0

您的IDE不會自動添加正確的參考嗎? –

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

+0

Downvoting,因爲'HTMLWorker'已被棄用。你應該使用新的'XMLWorker'。 –

+0

@AmedeeVanGasse最好是編輯答案,並提供OP – Webruster

+0

的最佳解決方案的確如此。但是,我現在在火車上使用不太理想的Android應用程序,並且在我到辦公室時,我會忘記這個問題。抱歉。 –