【问题标题】:Error in C# code while using ITextSharp使用 ITextSharp 时 C# 代码出错
【发布时间】:2011-05-16 03:18:18
【问题描述】:

我正在使用来自其他 question 的代码,我收到的错误是

错误 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 文件中。请确认我所做的是否正确。

【问题讨论】:

    标签: c# itextsharp


    【解决方案1】:

    此代码中存在名称冲突 - 您是 using iTextSharp.text 命名空间并尝试使用标准 System.Collections.Generic.List&lt;T&gt; 类。

    您要么需要删除 using iTextSharp.text 并将其类与显式命名空间一起使用,要么对 List&lt;T&gt; 使用显式命名空间。

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

    第三种解决方案是使用aliases。

    对于第二个错误,您需要导入HTMLWorker 命名空间。放

    using iTextSharp.text.html.simpleparser;
    

    在顶部。

    【讨论】:

      【解决方案2】:

      1.给你的列表加上前缀

      System.Collections.Generics.List<IElement> htmlarraylist
      

      2.貌似你没有导入HTMLWorker的命名空间

      编辑:我搜索了你,命名空间可能是这三个中的任何一个。我怀疑它可能是最后一个,但我不确定。

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

      【讨论】:

      • HTMLWorker 的导入是什么?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      • 1970-01-01
      • 2023-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多