I have a PDF form with filled out fields. If I try to read the acrofields they are empty. But in the PDF I can change the values and save them.
namespace Tutorial {
public class FillForm {
public const String SRC = "../../../resources/pdf/dnd-character.pdf";
public const String DEST = "../../../results/fill_dnd.pdf";
public static void Main(String[] args) {
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new FillForm().ManipulatePdf(SRC, DEST);
}
public virtual void ManipulatePdf(String src, String dest) {
PdfDocument pdf = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
PdfAcroForm form = PdfFormCreator.GetAcroForm(pdf, true);
IDictionary<String, PdfFormField> fields = form.GetAllFormFields();
Console.WriteLine(fields.Count);
foreach(KeyValuePair<string, PdfFormField> entry in fields)
{
Console.WriteLine(entry.Key);
}
pdf.Close();
}
}
}
Here is the pdf: PDF
My code works with other PDFs, so I feel confident its a PDF issue. Here is a PDF it works with: PDF2
I tried looking into this myself, but the best I could find is this outdated stack question here: ItextSharp – Acrofields are empty
PdfArray fields = form.GetAsArray(PdfName.Fields);
Again, this solution uses form.GetAsArray(PdfName.Fields);
so this solution fails for me too. I also checked for Xfa like in the question and that does not seem to present
XfaForm xfa = form.GetXfaForm();
xfa.IsXfaPresent() // returns false
However, this question led me to look at the PDF Hierarchy in RUPS, and I noticed a difference. Here is the hierarchy of the PDF that works:
The highlighted /T is the reference I want to use to get the string inside /V
Here is the hierarchy of the pdf that doesn’t work:
Again, Want /V using /T (or any other way to specifically get /V really). So, how do I work with this seemingly bad pdf to get the information I want?