0

I have a sample C# code that I actually took it from an article to explain how to list field names from PDF file. The code was taken from here

Before I took the code, I have added the following DLLs to my project's reference:

  • itextsharp.dll
  • itextsharp.pdfa.dll
  • itextsharp.xmlworker.dll
  • itextsharp.xtra.dll

Then I created one simple form with one TextBox and I set multiline property to True for the TextBox

On form load, I called a method ListFieldNames() to list the fields into the TextBox according to the article.

Below is the code:

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;
    }

When I run the application, nothing displayed to the TextBox and I didn't get any error message.

When I tried to debug the application, I found out that the code never get in the for loop at all which means the line sb.Append(de.Key.ToString() + Environment.NewLine); was never executed

Here is the Output that I get when I run the application:

'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.
Ahmed Ali
  • 127
  • 2
  • 13
  • How many AcroFields.Fields do you have? Print/verify it before foreach loop – Mate May 23 '18 at 17:01
  • @Mate, the count is 0, this is interesting! But the file location is correct. – Ahmed Ali May 23 '18 at 17:05
  • I can't tell you with any confidence why the field count is zero, but perhaps looking at this similar question (and answer) might help you: https://stackoverflow.com/questions/22909979/itextsharp-acrofields-are-empty –  May 23 '18 at 17:07
  • Yes, but PDF files've always been a headache. You need to check how the fields were created and how itextsharp recognizes them. My recomendation is: patience – Mate May 23 '18 at 17:12
  • 2
    You could add itextsharp via nuget https://www.nuget.org/packages/iTextSharp/ – Mate May 23 '18 at 17:17
  • @Mate, yes, I did already with no luck – Ahmed Ali May 23 '18 at 17:23
  • The issue is definitely caused by the file, I have tested another PDF form and it worked nicely. I don't know if there is anything can be done to the file though! – Ahmed Ali May 23 '18 at 18:12
  • Exact. You can try with more files and some won't work. Ideally, they should always be generated in the same way. Elgonzo's comment talks about how to repair it. It's in java, but you can follow https://developers.itextpdf.com/examples/itext-action-second-edition/chapter-13#491-fixbrokenform.java – Mate May 23 '18 at 18:22
  • Please share the pdf in question. – mkl May 23 '18 at 18:58
  • If the form is an XFA form, the number of AcroForm fields can be zero too. In that case, there are no AcroForm fields, only fields defined in XML using the XML Forms Architecture. – Bruno Lowagie May 23 '18 at 20:29
  • @BrunoLowagie, this is a very good hint – Ahmed Ali May 23 '18 at 20:34
  • @BrunoLowagie, the file is the I-9 form from: [US gov](https://www.uscis.gov/system/files_force/files/form/i-9.pdf?download=1). Do you think it will work? If not, is there a way to fix this and make it work? – Ahmed Ali May 24 '18 at 15:06
  • 1
    Read the FAQ: https://developers.itextpdf.com/question/how-fill-out-pdf-file-programmatically-dynamic-xfa (version 5 answer) or https://developers.itextpdf.com/content/best-itext-questions-stackoverview/interactive-forms/itext7-how-fill-out-pdf-file-programmatically-dynamic-xfa (iText 7 answer; preferred). As for finding out the structure of the XML data: ask the US government or extract and interprete the data definition. – Bruno Lowagie May 24 '18 at 15:19
  • @BrunoLowagie, thanks – Ahmed Ali May 24 '18 at 15:27

0 Answers0