How do I get back the format of a form field in a PDF?

234 views
Skip to first unread message

Support

unread,
Nov 16, 2007, 3:53:00 PM11/16/07
to PDFTron PDFNet SDK
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.

Support

unread,
Dec 6, 2007, 8:01:14 PM12/6/07
to PDFTron PDFNet SDK
Q:

Thanks for the quick response. I tried this change but console echo'd
the following:
Exception:
Message: GetDecodedStream() can't be invoked on Obj of this
type.
Function : PDFTRON::SDF::Obj::IllegalFunctionException

---
A: The problem is that JavaScript field may be also a PDF string.
You may want to try the following snippet instead.

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 js = fmt.FindObj("JS");
if (js != null) {
if (js.IsString()) {
string java_script = js.GetAsPDFText();
}
else {
// Obj stm = js.GetDecodedStream();
// FilterReader reader = new FilterReader(stm);
// reader.Read(...extract JS ....);
}
}
}
Reply all
Reply to author
Forward
0 new messages