2011-11-09 4 views
2

現在、私は、gridviewをpdfファイルにエクスポートしようとしています。なぜtable.addcell(セル)はhtmlコードを表示するのですかint iTextSharp

私のコードは次のとおりです。

public void GvExportPDF(GridView gvListing, string fileName,int TotalColumns) 
{ 
     http.Response.ContentType = "application/pdf"; 

     http.Response.AddHeader("content-disposition", "attachment;filename= " + fileName + ".pdf"); 

     http.Response.Cache.SetCacheability(HttpCacheability.NoCache); 

     StringWriter sw = new StringWriter(); 
     HtmlTextWriter hw = new HtmlTextWriter(sw); 

     gvListing.AllowPaging = false; 

     PdfPTable table = new PdfPTable(TotalColumns); 

     foreach (GridViewRow rows in gvListing.Rows) 
     { 
      if (rows.RowType == DataControlRowType.Header) 
      { 
       for (int i = 0; i < rows.Cells.Count; i++) 
       { 
        PdfPCell cell = new PdfPCell(); 
        cell.Phrase = new Phrase(getCellText(rows.Cells[i])); 
        table.AddCell(cell); 
       } 
      } 
      else if (rows.RowType == DataControlRowType.DataRow) 
      { 

       System.Web.UI.WebControls.Image Image1 = (System.Web.UI.WebControls.Image)rows.FindControl("Image1"); 
       string url = Image1.ImageUrl; 
       Image1.Visible = false; 

       iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(new Uri(url)); 
       jpg.ScaleToFit(8f, 10f); 
       jpg.Border = Rectangle.NO_BORDER; 
       for (int i = 0; i < rows.Cells.Count; i++) 
       { 
        if (i == 0) 
         table.AddCell(jpg); 
        else 
        { 

         PdfPCell cell = new PdfPCell(new Phrase(HttpContext.Current.Server.HtmlDecode(getCellText(rows.Cells[i])))); 


         table.AddCell(cell); 
        } 
       } 

      } 
     } 
     //gvListing.RenderControl(hw); 
     StringReader sr = new StringReader(sw.ToString()); 
     Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); 
     HTMLWorker htmlparser = new HTMLWorker(pdfDoc); 
     PdfWriter.GetInstance(pdfDoc, http.Response.OutputStream); 
     try 
     { 
      pdfDoc.Open(); 
      pdfDoc.Add(table); 
     } 
     catch (DocumentException dex) 
     { 
      //http.Response.Write(dex.Message); 
      System.Diagnostics.Debug.WriteLine(dex.Message); 
     } 
     catch (IOException ioex) 
     { 
      //http.Response.Write(ioex.Message); 
      System.Diagnostics.Debug.WriteLine(ioex.Message); 
     } 
     catch (Exception ex) 
     { 
      //http.Response.Write(ex.Message); 
      System.Diagnostics.Debug.WriteLine(ex.Message); 
     } 

     finally 
     { 
      pdfDoc.Close(); 
      //http.Response.Write(StyleOfExportTable()); 
      http.Response.Output.Write(pdfDoc); 
      http.Response.End(); 
     } 
    } 

    private string getCellText(TableCell cell) 
    { 
     StringBuilder sb = new StringBuilder(cell.Text); 

     foreach (Control c in cell.Controls) 
     { 
      if (c.Visible) 
      { 
       string controlText = getTextProperty(c); 
       if (controlText != null) 
       { 
        if (sb.Length > 0) sb.Append(" "); 
        sb.Append(controlText); 
       } 
      } 
     } 
     return sb.ToString(); 
    } 

    private string getTextProperty(object o) 
    { 
     PropertyInfo propertyInfo = o.GetType().GetProperty("Text"); 
     if (propertyInfo != null) 
     { 
      MethodInfo getMethod = propertyInfo.GetGetMethod(); 
      if (getMethod != null) 
      { 
       return (string)getMethod.Invoke(o, null); 
      } 
     } 
     return string.Empty; 
    } 

これは、PDFファイルをエクスポートすることができますし、私が欲しいの合計列をエクスポートすることができますが、画像のほかに、他のセルがちょうどhtmlコード<table><tr><td>text</td></tr></table>を表示します。

これは何ですか、どうすれば解決できますか?

+0

あなたは 'sw'、' hw'、 'sr'、' htmlparser'を使っていますか? –

+0

ya、Chris Hass私はhtmlparserとstringbuilderを使用しています – 456qwe123asd

答えて

0

私は直接的な答えはありませんが、私は、あなたがPdfPCellの内容を設定するために何をしているのかという問題がほとんど確実であることを伝えることができます。 iTextSharpの中核にはHTMLの概念がないため、ほぼすべてのコマンドはPDFベースで、いくつかの.Net固有のものがそこにあります。 です。パーサーはHTMLをPDFベースのコマンドに変換しますが、使用していません。 iTextSharpが文字列リテラルとして扱っているHTMLを返すのは、セルの内容を取得するために何をしているのかです。アルゴリズムをテキストのみで返すか、iTextSharp.text.html.simpleparser.HTMLWorker.ParseToListを使用してHTMLをiTextSharpオブジェクトに変換する必要があります。

+0

おかげでクリス、しかし、私はこれを置くべきですか? – 456qwe123asd

関連する問題