c# - 为什么 foreach 在与 itextsharp 一起使用时会导致错误

标签 c# pdf visual-studio-2012 foreach

部分代码为:

private void ListFieldNames()
        {
            string pdfTemplate = @"c:\Temp\PDF\fw4.pdf";

            // title the form
            this.Text += " - " + pdfTemplate;

            // create a new PDF reader based on the PDF template document
            PdfReader pdfReader = new PdfReader(pdfTemplate);

            // create and populate a string builder with each of the 
            // field names available in the subject PDF
            StringBuilder sb = new StringBuilder();
            foreach (DictionaryEntry de in pdfReader.AcroFields.Fields)
            {
                sb.Append(de.Key.ToString() + Environment.NewLine);
            }

            // Write the string builder's content to the form's textbox
            textBox1.Text = sb.ToString();
            textBox1.SelectionStart = 0;
        }

我收到以下错误:

Error 1 Cannot convert type 'System.Collections.Generic.KeyValuePair<string,iTextSharp.text.pdf.AcroFields.Item>' to 'System.Collections.DictionaryEntry' c:\Users\usrs\Documents\Visual Studio 2012\Projects\PDFTest SLN\PDFTest\Form1.cs 50 13 PDFTest

我正在使用 VS 2012。

如何解决错误?

最佳答案

如错误所述:因为 FieldsSystem.Collections.Generic.KeyValuePair<string,iTextSharp.text.pdf.AcroFields.Item> 的集合不是 DictionaryEntry .

您应该明确使用 System.Collections.Generic.KeyValuePair<string,iTextSharp.text.pdf.AcroFields.Item>输入或使用 var关键字并让编译器确定类型。

我建议使用以下代码:

foreach (var de in pdfReader.AcroFields.Fields)
{
    sb.Append(de.Key.ToString() + Environment.NewLine);
}

关于c# - 为什么 foreach 在与 itextsharp 一起使用时会导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23921245/

相关文章:

java - openhtmltopdf/ Flying Saucer : many links in huge PDF are not clickable (PDF annotations not set)

c# - PHP 和 C# 兼容 Rijndael 托管 CBC 模式,256 位加密/解密

c# - 查找包含特定字母/符号的字符串的特定部分,然后创建对这些部分的引用

c# - ForeignKeyConstraint 要求子键值 (0) 存在于父表中

image - tcpdf:图像质量差

c# - 带有嵌套命名空间的编译器错误 "cannot resolve symbol"

c# - 如何最好地确定新应用程序的系统要求?

javascript - Chrome扩展程序:以PDF格式在网站上打印多个页面

c# - 为 COM Interop 构建 .NET DLL 时出现 "Access to the Registry Key Denied"

c# - visual studio c#远程调试关闭windows服务