Shane Soderstrom
Shane Soderstrom

Reputation: 23

How do I pull all values from PDF?

I have a working solution that opens a PDF file and grabs the text. Unfortunately the values that I need are in form fields. I've tried a few ways to get the values but I can only get what appears to be the form name. The key values are correct, but the value received is wrong.

Key ValueReturned Company Name iText.Forms.Fields.PdfTextFormField Phone Number iText.Forms.Fields.PdfTextFormField Business Contact Data iText.Forms.Fields.PdfTextFormField Name iText.Forms.Fields.PdfTextFormField

The values in the form fields are not being returned. Is there a better way to do this?

using System;
using System.Collections.Generic;
using iText.Forms;
using iText.Forms.Fields;
using iText.Kernel.Pdf;

namespace ConsoleApplication1 {
    class Class1 {      
        public string pdfthree(string pdfPath) {            
            PdfReader reader = new PdfReader(pdfPath);
            PdfDocument document = new PdfDocument(reader);
            PdfAcroForm acroForm = PdfAcroForm.GetAcroForm(document, false);
            IDictionary<string, PdfFormField> Map = new Dictionary<string, PdfFormField>();

            Map = acroForm.GetFormFields();
            acroForm.GetField("Name");
            string output = "";

            foreach (String fldName in Map.Keys) {
                output += fldName + ": " + Map[fldName].ToString() + "\n";
            }

            System.IO.File.WriteAllText(pdfPath, output);
            document.Close();
            reader.Close();
            return output;
        }
    }
}

Upvotes: 2

Views: 2037

Answers (1)

Alexey Subach
Alexey Subach

Reputation: 12312

Instead of calling PdfFormField#ToString(), you should call PdfFormField#GetValueAsString() to get the value of the field.

Full code:

using System;
using System.Collections.Generic;
using iText.Forms;
using iText.Forms.Fields;
using iText.Kernel.Pdf;

namespace ConsoleApplication1 {
    class Class1 {      
        public string pdfthree(string pdfPath) {            
            PdfReader reader = new PdfReader(pdfPath);
            PdfDocument document = new PdfDocument(reader);
            PdfAcroForm acroForm = PdfAcroForm.GetAcroForm(document, false);
            IDictionary<string, PdfFormField> Map = new Dictionary<string, PdfFormField>();

            Map = acroForm.GetFormFields();
            acroForm.GetField("Name");
            string output = "";

            foreach (String fldName in Map.Keys) {
                output += fldName + ": " + Map[fldName].GetValueAsString() + "\n";
            }

            System.IO.File.WriteAllText(pdfPath, output);
            document.Close();
            reader.Close();
            return output;
        }
    }
}

Upvotes: 2

Related Questions