部分代码为:
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。
如何解决错误?
最佳答案
如错误所述:因为 Fields
是 System.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/