Click here to Skip to main content
14,829,674 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
have a PDF file which is generated by a third party software. I am trying to read the fields of the PDF file using iTextSharp. I need to search for a field and put a black image on it.

In the debug mode the AcroFields and the Keys are always none.(0)

I have tried different codes but i am not able to read the fields of the PDF file. I also have searched thru the forums but not able to figure it out.

I able to figure it out how to put an image but searching for specific field is not working.

In my code below i am just trying to print out, but the fields and the keys are zero



C#
var pdfReader = new PdfReader(fileName);
    var fieldList = GetFormFieldNames(pdfReader);
    var fieldList2 = GetFormFieldNamesWithValues(pdfReader);
    private static string GetFormFieldNames(PdfReader pdfReader)
    {
        return string.Join("\r\n", pdfReader.AcroFields.Fields

                                       .Select(x => x.Key).ToArray());
    }

    private static string GetFormFieldNamesWithValues(PdfReader     pdfReader)
    {
        return string.Join("\r\n", pdfReader.AcroFields.Fields

                                       .Select(x => x.Key + "=" +

                                        pdfReader.AcroFields.GetField(x.Key))

                                       .ToArray());
    }

   //I have also tried like this just to display the fields
        var pdf_filename = @"C:\DownloadTXT\DA9XGWKYB 5001.pdf";
        using (var reader = new PdfReader(pdf_filename))
        {
            var fields = reader.AcroFields.Fields;

            foreach (var key in fields.Keys)
            {
                var value = reader.AcroFields.GetField(key);
                Response.Write(key + " : " + value);
            }
        }
Posted
Updated 31-Mar-20 5:16am
v2
Comments
Richard Deeming 5-Oct-15 9:02am
   
Are you absolutely certain that the third-party software generates PDFs which contain fields? It could just be adding text to the PDF, without creating any fields.
kesav prakash 6-Oct-15 6:20am
   
pdf designed in Adobe LiveCycle Designer ES9.0 by client i cannot modify it.

kesav prakash 6-Oct-15 6:21am
   
the above code was working in some sample pdf only on my client pdf its not workinig
Richard Deeming 6-Oct-15 7:37am
   
That means the PDF your client has sent you doesn't contain any fields.

You'll either need to get your client to send you a new PDF which does use fields, or find another way to do what you're trying to achieve.

1 solution

Book page : Why are the AcroFields in my document empty?[^]

Rewrite the file:

PdfReader reader = new PdfReader(src);
PdfDictionary root = reader.Catalog;
PdfDictionary form = root.GetAsDict(PdfName.ACROFORM);
PdfArray fields = form.GetAsArray(PdfName.FIELDS);
PdfDictionary page;
PdfArray annots;
for (int i = 1; i <= reader.NumberOfPages; i++) {
    page = reader.GetPageN(i);
    annots = page.GetAsArray(PdfName.ANNOTS);
    for (int j = 0; j < annots.Size; j++) {
        fields.Add(annots.GetAsIndirectObject(j));
    }
}
PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create));
stamper.Close();
        reader.Close();
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900