C# iTextSharp文件名显示不';行不通

C# iTextSharp文件名显示不';行不通,c#,pdf,itext,pdf-form,C#,Pdf,Itext,Pdf Form,我有一个示例C#代码,实际上是从一篇文章中获取的,用于解释如何从PDF文件中列出字段名。代码取自 在获取代码之前,我已将以下DLL添加到我的项目参考中: itextsharp.dll itextsharp.pdfa.dll itextsharp.xmlworker.dll itextsharp.xtra.dll 然后,我用一个文本框创建了一个简单表单,并将文本框的multiline属性设置为True 在表单加载时,我调用了一个方法ListFieldNames(),根据文章将字段列在文本框中

我有一个示例C#代码,实际上是从一篇文章中获取的,用于解释如何从PDF文件中列出字段名。代码取自

在获取代码之前,我已将以下DLL添加到我的项目参考中:

  • itextsharp.dll
  • itextsharp.pdfa.dll
  • itextsharp.xmlworker.dll
  • itextsharp.xtra.dll
然后,我用一个文本框创建了一个简单表单,并将文本框的multiline属性设置为True

在表单加载时,我调用了一个方法
ListFieldNames()
,根据文章将字段列在文本框中

代码如下:

private void ListFieldNames()
    {
        string pdfTemplate = "C:\\Users\\aalsahli\\Desktop\\I-9Form.pdf";
        this.Text += " - " + pdfTemplate;

        PdfReader pdfReader = new PdfReader(pdfTemplate);

        StringBuilder sb = new StringBuilder();
        foreach (var de in pdfReader.AcroFields.Fields)
        {
            sb.Append(de.Key.ToString() + Environment.NewLine);
        }
        textBox1.Text = sb.ToString();
        textBox1.SelectionStart = 0;
    }
当我运行应用程序时,文本框中没有显示任何内容,也没有收到任何错误消息

当我尝试调试应用程序时,我发现代码根本没有进入for循环,这意味着行
sb.Append(de.Key.ToString()+Environment.NewLine)从未执行过

以下是我运行应用程序时得到的输出:

'ReadPDF.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'ReadPDF.exe' (CLR v4.0.30319: DefaultDomain): Loaded 'C:\Users\aalsahli\source\repos\ReadPDF\ReadPDF\bin\Debug\ReadPDF.exe'. Symbols loaded.
'ReadPDF.exe' (CLR v4.0.30319: ReadPDF.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'ReadPDF.exe' (CLR v4.0.30319: ReadPDF.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'ReadPDF.exe' (CLR v4.0.30319: ReadPDF.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'ReadPDF.exe' (CLR v4.0.30319: ReadPDF.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'ReadPDF.exe' (CLR v4.0.30319: ReadPDF.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'ReadPDF.exe' (CLR v4.0.30319: ReadPDF.exe): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
'ReadPDF.exe' (CLR v4.0.30319: ReadPDF.exe): Loaded 'C:\Users\aalsahli\source\repos\ReadPDF\ReadPDF\bin\Debug\itextsharp.dll'. Module was built without symbols.

你有多少个字段?在foreach之前打印/验证loop@Mate,计数为0,这很有趣!但是文件位置是正确的。我不能自信地告诉您为什么字段计数为零,但是看看这个类似的问题(和答案)可能会对您有所帮助:您可以通过nuget添加itextsharp阅读常见问题解答:(版本5答案)或(iText 7答案;首选)。至于找出XML数据的结构:询问美国政府或提取并解释数据定义。