2010-11-26 24 views
1

我使用來自其他question代碼和我正的誤差作爲錯誤而使用iTextSharp的

錯誤1所述的非通用型 「iTextSharp.text.List」不能使用 與類型參數

錯誤2名稱「HTMLWorker」不 在當前上下文中存在

錯誤3類型或命名空間名稱 「HTMLWorker」找不到(是 你缺少using指令或 集引用)

到目前爲止我的代碼如下:?

protected void Button2_Click(object sender, EventArgs e) 
{ 
    //Extract data from Page (pd). 
    Label16.Text = Editor1.Content; // Attribute  

    // makae ready HttpContext 
    HttpContext.Current.Response.Clear(); 
    HttpContext.Current.Response.ContentType = "application/pdf"; 

    // Create PDF document 
    Document pdfDocument = new Document(PageSize.A4, 80, 50, 30, 65); 
    //PdfWriter pw = PdfWriter.GetInstance(pdfDocument, HttpContext.Current.Response.OutputStream); 
    PdfWriter.GetInstance(pdfDocument, HttpContext.Current.Response.OutputStream); 

    pdfDocument.Open(); 
    //WebClient wc = new WebClient(); 
    string htmlText = Editor1.Content; 

    List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(htmlText), null); 
    for (int k = 0; k < htmlarraylist.Count; k++) 
    { 
     pdfDocument.Add((IElement)htmlarraylist[k]); 
    } 

    //pdfDocument.Add(new Paragraph(IElement)); 
    pdfDocument.Close(); 
    HttpContext.Current.Response.End(); 

}

請幫我解決這個錯誤。我正在嘗試從htmleditor獲取內容(非html)並顯示在pdf文件中。請確認我所做的是否正確。

回答

3

1.Prefix你的名單像

System.Collections.Generics.List<IElement> htmlarraylist 

2.Looks像您沒有導入HTMLWorker

編輯的命名空間:我用Google搜索你,命名空間可以是任何的這些我懷疑這可能是最後一個,但我不確定。

using iTextSharp.text; 
using iTextSharp.text.pdf; 
using iTextSharp.text.html.simpleparser; 
+0

什麼是HTMLWorker導入? – Ishan 2010-11-26 10:54:14

3

此代碼中存在名稱衝突 - 您是using iTextSharp.text命名空間並嘗試使用標準System.Collections.Generic.List<T>類。

要麼你需要刪除using iTextSharp.text並使用它的類與顯式名稱空間或使用顯式名稱空間爲List<T>

System.Collections.Generic.List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(htmlText), null); 

第三種解決方案是使用aliases

而對於第二個錯誤,您需要導入HTMLWorker名稱空間。把

using iTextSharp.text.html.simpleparser; 

在頂部。