2012-08-09 3 views
0

SQL 데이터를 PDF 파일로 내보내려고합니다. 다음 코드를 사용했습니다. 나는 오류가 발생했습니다 HtmlParser.Parse (Doc, xmlReader); 이름 'HtmlParser'현재 컨텍스트SQL 데이터를 PDF로 내 보냅니다. 오류 : 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, [email protected] \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(); 

     } 

에 존재하지 않는 것은 같은 몇 가지 솔루션을 주거나 당신은 내게 다른 더 좋은 방법을 제안 할 수 있습니다하십시오.

+0

어떤 오류가 발생합니까? –

+0

이름 'HtmlParser'이 (가) 현재 컨텍스트에 존재하지 않습니다. –

답변

0

설정에서 HtmlParser 클래스를 검색해보십시오. 빠른 Google 검색에 따르면 HtmlParser은 iTextSharp의 최신 버전에는 존재하지 않습니다. this, this을 참조하고 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]); 
      } 
+0

위의 코드를 어디에 추가 할 수 있습니까? 그게 너무 혼란스러워! –

+0

클래스 브라우저에서'iTextSharp.text.html.simpleparser.HTMLWorker' 클래스의 메소드/속성을 확인하십시오. 어쩌면'HTMLParser.Parse()'메소드 대신에 드롭 인 대체물이있을 것이다. 회상시 가장 좋은 코드 샘플은 아니지만 도움이 될 것으로 생각합니다. 당신도 똑같이 할 수 있습니다. – darkphoenix