On 03/03/12 10:26, Larry Evans wrote:
> What would be a good way of using itext to modify:
>
> http://www.irs.gov/pub/irs-pdf/f1040.pdf
>
> so that it behaves somewhat like a spreadsheet.
> What I mean is that all form fields are divided into
> modifiable or readonly based upon whether or not the
> value in the field depends on other fields. I have
> modified the FormInformation.java example:
>
> http://itextpdf.com/examples/iia.php?id=121
>
> and saved this modification to:
>
> FormInformationF1040.java
>
> which is attached. The field names in the f1040.pdf are very cryptic,
> as shown by some of the output lines of FormInformationF1040.java,
> which is attached as:
>
> f1040_info.txt
>
> To provide a less cryptic names for the fields,
> FormInformationF1040.java uses java enums (see field_enum in the code)
> then uses an instance of ImmutableBiMap from:
>
>
> http://search.maven.org/remotecontent?filepath=com/google/guava/guava/11.0.2/guava-11.0.2.jar
>
> to map between the cryptic names and the enums. Unfortunately, the
> only means of discerning this map is to repeatedly:
>
> 1) fill a field with some unique string
> (something like an abbreviated form of the enumerator from
> field_enums)
>
> 2) run the program to produce a new f1040_info.txt
>
> 3) Inspect f1040_info.txt to manually match the unique string
> with the cryptic name, and then append the .put calls to
> Immutable.Builder<field_enums,String() with the appropriate
> values.
>
> Of course this is tedious and if anyone can suggest an easier way, I'd
> appreciate hearing about it.
>
[snip]
One less tedious method is to create a bijective map from page number
and tab order to the cryptic names, as done by the attached .java file
whose partial output is in the attached .txt file.
One could then form enum names from the lhs of the table in the .txt
file possible suffixed with some description found by simply tabbing
through the f1040.pdf and seeing where the cursor ends up.
-Larry
/*
* ChangeLog:
* @@2012-03-05.0500CST
* WHO: Larry Evans
* WHAT:
* Copied from:
* http://examples.itextpdf.com/src/part4/chapter13/InspectForm.java
* which was partly contained in Listing 13.15 of the book:
* "iText in Action - 2nd Edition"
* written by Bruno Lowagie (ISBN: 9781935182610)
* @@2012-03-05.0600CST
* WHO: Larry Evans
* WHAT:
* 1)Changed input pdf to f1040.pdf from irs.gov.
* 2)Changed loop body to print table of:
* (PageNum,TabOrder) -> FieldName.
* WHY:
* As preparation for providing a bijective mapping
* from readable field names
* to the existing cryptic field names.
*/
package part4.chapter13;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.TreeMap;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseField;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfReader;
public class F1040TabOrderName {
/** The form to be inspected */
public static final String FORM = "resources/pdfs/f1040.pdf";
/** A text file containing information about a form. */
public static final String RESULTTXT = "results/part4/chapter13/f1040TabOrderName.txt";
/**
* Inspects a PDF file src with the file dest as result.
* Result will have (page number,taborder) vs field_name table
* sorted on (page number.taborder).
*
* @param src the original PDF
* @param dest the resulting PDF
* @throws IOException
* @throws DocumentException
*/
public void inspectPdf(String src, String dest)
throws IOException, DocumentException {
PrintWriter out = new PrintWriter(new FileOutputStream(dest));
PdfReader reader = new PdfReader(src);
AcroFields form = reader.getAcroFields();
Map<String,AcroFields.Item> fields = form.getFields();
AcroFields.Item item;
TreeMap<String,String> pageTaborder_fieldName=new TreeMap<String,String>();
for (Map.Entry<String,AcroFields.Item> entry : fields.entrySet()) {
item = entry.getValue();
int tab_order=item.getTabOrder(0).intValue();
int page_number=item.getPage(0).intValue();
String pageTaborder=String.format("p%02d.t%03d", page_number, tab_order);
String fieldName=entry.getKey();
pageTaborder_fieldName.put(pageTaborder,fieldName);
}
for (Map.Entry<String,String> entry : pageTaborder_fieldName.entrySet()) {
out.println(entry.getKey()+"->"+entry.getValue());
}
out.flush();
out.close();
}
/**
* Inspects a form.
* @param args no arguments needed
* @throws IOException
* @throws DocumentException
*/
public static void main(String[] args) throws IOException, DocumentException {
new F1040TabOrderName().inspectPdf(FORM, RESULTTXT);
}
}
p01.t000->topmostSubform[0].Page1[0].HeaderPg1[0].p1-t1[0]
p01.t001->topmostSubform[0].Page1[0].HeaderPg1[0].p1-t2[0]
p01.t002->topmostSubform[0].Page1[0].HeaderPg1[0].p1-t3[0]
p01.t003->topmostSubform[0].Page1[0].p1-t4[0]
p01.t004->topmostSubform[0].Page1[0].p1-t5[0]
p01.t005->topmostSubform[0].Page1[0].p1-t11[0]
p01.t006->topmostSubform[0].Page1[0].p1-t6[0]
p01.t007->topmostSubform[0].Page1[0].p1-t7[0]
p01.t008->topmostSubform[0].Page1[0].SpouseSSN[0].p1-t14[0]
p01.t009->topmostSubform[0].Page1[0].Address[0].p1-t8[0]
p01.t010->topmostSubform[0].Page1[0].Address[0].p1-t9[0]
p01.t011->topmostSubform[0].Page1[0].Address[0].p1-t10[0]
p01.t012->topmostSubform[0].Page1[0].Address[0].f1_011[0]
p01.t013->topmostSubform[0].Page1[0].Address[0].f1_012[0]
p01.t014->topmostSubform[0].Page1[0].Address[0].f1_013[0]
p01.t015->topmostSubform[0].Page1[0].c1_02_0_[0]
p01.t016->topmostSubform[0].Page1[0].c1_03[0]
p01.t017->c1_04
p01.t020->topmostSubform[0].Page1[0].Lines1-3[0].p1-t17[0]
------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples:
http://itextpdf.com/themes/keywords.php