循环遍历iTextsharp

时间:2017-02-21 20:16:52

标签: c# itext

这个方法我们现在在切换到AWS后工作我得到一个奇怪的错误:

public static Dictionary<string, string> GetFormFieldNames(string pdfPath)
{
    var fields = new Dictionary<string, string>();

    foreach (DictionaryEntry entry in reader.AcroFields.Fields)
    {
        fields.Add(entry.Key.ToString(), string.Empty);
    }

    return fields;
}
  

Cannont转换类型   System.Collections.Generic.KeyValuePair<String.iTextSharp.text.pdf.AcroFields.item>   到System.CollectionsEntry

我理解错误是显而易见的,但我似乎无法使类型正确。为什么这种方法会停止工作?我在这里做错了什么?

2 个答案:

答案 0 :(得分:1)

您可以使用var而不是类型定义,然后让Intellisence为您完成。

public static Dictionary<string, string> GetFormFieldNames(string pdfPath)
{
    var fields = new Dictionary<string, string>();

    foreach (var entry in reader.AcroFields.Fields)
    {
        fields.Add(entry.*use intellisense here*, string.Empty);
    }

    return fields;
}

答案 1 :(得分:0)

错误消息没有意义。 KeyValuePair有两个类型参数,但您发布的消息只有一个。 .NET中也没有System.CollectionsEntry类型或命名空间,因此我怀疑您是否截断了实际的错误消息。无论如何,我猜测Fields的类型是System.Collections.Generic.KeyValuePair&lt; string,iTextSharp.text.pdf.AcroFields.Item&gt;。你可以使用foreach(读取器中的var kvp.AcroFields.Fields)。

至于为什么事情发生了变化,&#34;也许你改变了.NET版本? DictionaryEntry是迭代Hashtables的pre-generics方式。