部分代码是:
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。
如何解决错误?
答案 0 :(得分:5)
如错误所示:因为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);
}