/*
* List form fields and emit their properties and values.
*
* Required software: pCOS interface 9 (PDFlib+PDI 10, TET 5.x, PLOP 5.x)
*
* Required data: PDF document
*/
package com.pdflib.cookbook.pcos.interactive;
import com.pdflib.IpCOS;
import com.pdflib.cookbook.pcos.pcos_cookbook_example;
public class formfields extends pcos_cookbook_example {
/* This is where the data files are. Adjust as necessary. */
private final static String SEARCH_PATH = "../input";
public void example_code(IpCOS p, int doc) throws Exception {
System.out.println("File name: " + p.pcos_get_string(doc, "filename"));
int fieldcount = (int) p.pcos_get_number(doc, "length:fields");
if (fieldcount == 0) {
System.out.println("No form fields found\n");
}
else {
System.out.println(fieldcount + " form fields found (including administrative nodes in the forms hierarchy)");
for (int f = 0; f < fieldcount; f++) {
print_field(p, doc, f);
}
}
}
/**
* Print information about a single field.
*
* @param p
* IpCOS object
* @param doc
* document handle
* @param f
* number of field in "fields" pseudo object array
* @throws Exception
*/
private void print_field(IpCOS p, int doc, int f) throws Exception {
System.out.print("field " + f + ": level="
+ (int) p.pcos_get_number(doc, "fields[" + f + "]/level") + " ");
String res = p.pcos_get_string(doc, "fields[" + f + "]/fullname");
System.out.print("fullname='" + res + "'; ");
/* Check the field type /FT */
String objtype = p.pcos_get_string(doc, "type:fields[" + f + "]/FT");
if (objtype.equals("name")) {
String fieldtype = p.pcos_get_string(doc, "fields[" + f + "]/FT");
System.out.print("type='" + fieldtype + "' ");
if (fieldtype.equals("Btn")) {
print_button_field(p, doc, f);
}
else if (fieldtype.equals("Ch")) {
print_choice_field(p, doc, f);
}
else if (fieldtype.equals("Tx")) {
print_text_field(p, doc, f);
}
else if (fieldtype.equals("Sig")) {
System.out.println("See pCOS Cookbook topic 'signatures' to learn how to retrieve signature details");
}
}
else {
System.out.println("non-terminal field without any type");
}
}
/**
* Print information about a button field.
*
* @param p
* IpCOS object
* @param doc
* document handle
* @param f
* number of field in "fields" pseudo object array
*
* @throws Exception
*/
private void print_button_field(IpCOS p, int doc, int f)
throws Exception {
// Retrieve the field type
String type = p.pcos_get_string(doc, "fields[" + f + "]/type");
System.out.println("(" + type + " field)");
print_value(p, doc, f, " ");
}
/**
* Print information about a choice field (type /Ch).
*
* @param p
* IpCOS object
* @param doc
* document handle
* @param f
* number of field in "fields" pseudo object array
*
* @throws Exception
*/
private void print_choice_field(IpCOS p, int doc, int f)
throws Exception {
String prefix = " ";
String objtype;
// Retrieve the field type
String type = p.pcos_get_string(doc, "fields[" + f + "]/type");
System.out.println("(" + type + ")");
print_value(p, doc, f, prefix);
/* List options */
objtype = p.pcos_get_string(doc, "type:fields[" + f + "]/Opt");
if (objtype.equals("array")) {
int num_opt = (int) p.pcos_get_number(doc, "length:fields[" + f
+ "]/Opt");
for (int i = 0; i < num_opt; i += 1) {
String option = null;
objtype = p.pcos_get_string(doc, "type:fields[" + f + "]/Opt["
+ i + "]");
if (objtype.equals("array")) {
option = p.pcos_get_string(doc, "fields[" + f + "]/Opt["
+ i + "][1]");
}
else if (objtype.equals("string")) {
option = p.pcos_get_string(doc, "fields[" + f + "]/Opt["
+ i + "]");
}
if (option != null) {
System.out.println(prefix + "option " + (i + 1) + "='"
+ option + "'");
}
}
}
}
/**
* Print information about a text field (type /FT).
*
* @param p
* IpCOS object
* @param doc
* document handle
* @param f
* number of field in "fields" pseudo object array
*
* @throws Exception
*/
private void print_text_field(IpCOS p, int doc, int f) throws Exception {
String prefix = " ";
String fieldtype = "text";
String objtype;
/* Check for barcode field */
objtype = p.pcos_get_string(doc, "type:fields[" + f + "]/PMD");
if (objtype.equals("dict")) {
objtype = p.pcos_get_string(doc, "type:fields[" + f + "]/PMD/Symbology");
if (objtype.equals("name")) {
/* Check barcode type ("symbology") */
fieldtype = p.pcos_get_string(doc, "fields[" + f + "]/PMD/Symbology") + " barcode";
}
}
System.out.println("(" + fieldtype + " field)");
/* Print value if available */
print_value(p, doc, f, prefix);
/* Print the "quadding" (justification) */
String[] formatnames = { "left-justified", "centered",
"right-justified", "unknown" };
objtype = p.pcos_get_string(doc, "type:fields[" + f + "]/Q");
int format = 0; /* 0 is default */
if (objtype.equals("number")) {
format = (int) p.pcos_get_number(doc, "fields[" + f + "]/Q");
}
if (format < 0 || format > 2)
format = 3;
System.out.println(prefix + "format=" + formatnames[format]);
}
/**
* Print a single /V value.
*
* @param p
* IpCOS object
* @param doc
* document handle
* @param f
* number of field in "fields" pseudo object array
* @param prefix
* prefix for indentation
* @throws Exception
*/
private void print_value(IpCOS p, int doc, int f, String prefix)
throws Exception {
String objtype = p.pcos_get_string(doc, "type:fields[" + f + "]/V");
if (objtype.equals("name") || objtype.equals("string")) {
String res = p.pcos_get_string(doc, "fields[" + f + "]/V");
System.out.println(prefix + "value='" + res + "'");
}
else if (objtype.equals("stream")) {
byte[] res_stream = p.pcos_get_stream(doc, "fields[" + f + "]/FT",
"");
System.out.println(prefix + "value='" + res_stream + "'");
}
}
public formfields(String[] argv, String readable_name, String search_path) {
super(argv, readable_name, search_path);
}
public static void main(String argv[]) {
formfields example = new formfields(argv, "Form fields", SEARCH_PATH);
example.execute();
}
}