Q:
Is there any way to get back the format of a form field in a PDF? ie.
if there is a textbox on a pdf, but has a format of number assigned to
it? what do i need to reference in my code?
PDFDoc doc = new PDFDoc(input_path + "form1.pdf");
doc.InitSecurityHandler();
FieldIterator end = doc.FieldEnd();
for (FieldIterator itr = doc.FieldBegin(); itr != end; itr.Next())
{
Console.WriteLine("Field name: {0:s}", itr.Current().GetName());
Console.WriteLine("Field partial name: {0:s}",
itr.Current().GetPartialName());
Console.Write("Field type: ");
Field.Type type = itr.Current().GetType();
switch (type) {
case Field.Type.e_button:
Console.WriteLine("Button"); break;
case Field.Type.e_text:
Console.WriteLine("Text"); break;
case Field.Type.e_choice:
Console.WriteLine("Choice"); break;
case Field.Type.e_signature:
Console.WriteLine("Signature"); break;
}
}
doc.Close();
Console.WriteLine("Done.");
-----
A:
Unfortunately field formatting is not part of PDF specification.
Acrobat supports this functionality via embedded JavaScript (which is
not part of the standard).
You can access embedded JavaScript as follows:
Field f = ...
// Get additional-actions dictionary defining the field's behavior
// in response to various trigger events
Obj aa = f.GetSDFObj().FindObj("AA")
if (aa != null) {
// Get JavaScript action to be performed before the field is
// formatted to display its current value. This action can
// modify the field's value before formatting.
Obj fmt = aa.FindObj("F");
if (fmt != null) {
// extract embedded JavaScript...
Obj stm = fmt.GetDecodedStream();
FilterReader reader = new FilterReader(stm);
reader.Read(...extract JS ....);
}
The extracted JavaScript string may look as follows:
AFNumber_Format(6, 1, 1, 0, "\u0024", true);
This JavaScript command sets the field format to a decimal number with
6 decimal places, no separator, and dollar ($) as a currency symbol.
You could potentially parse this string to infer the correct
formatting.