On 03/04/12 12:43, Larry Evans wrote:
> On 03/04/12 10:32, Larry Evans wrote:
> [snip]
>> However, what I need is to get a PdfFormField from and AcroFields
>> instance and a field name.  I then need to use that PdfFormField
>> to specify an action for the field.
>>
>> The AcroFields class does have a method:
>>
>> http://api.itextpdf.com/itext/com/itextpdf/text/pdf/AcroFields.html#getFieldItem%28java.lang.String%29
>>
>> however, looking at the AcrodFields.Item class shows
>> nothing, AFAICT, that will produce a PdfFormField which
>> I could then use to set some action for the field.
> [snip]
> 
> A brief look here:
> 
>   http://www.itextpdf.com/examples/iia.php?id=238
> 
> suggests maybe a way to use AcroFields.Item to set the action for a
> field.
> 
Apparently, attaching an action to an f1040.pdf field as not as easy
is copy/past from that example.  With the attached, which is
AddJavaScriptToForm.java modified to use f1040.pdf as the form
to which an action is to be added, nothing happens when
the "Your first name and initial" field is modified.

So, how does one figure out how to add an action to a field
on the f1040 form?

TIA.

-Larry

/*
 * ChangeLog:
 *   @@2012-03-14.1107CST
 *     WHO: Larry Evans
 *     WHAT:
 *       Resorted to trial and error to figure out how to set action for selected TextField
 *       Made several changes just to avoid getting null pointer exception.
 *       Achieved that; however, no app.alert window appears when FIELDINP is
 *       formatted.  Hence, the method chosen, which avoided any run-time
 *       exception, did not achieve desired result of attaching some javascript
 *       action triggered by formatting event to a field.
 *     WHY:
 *       Closer reading of the AddJavaScriptToForm.java file revealed the form
 *       created had no relationship ot any irs f1040 form; hence, trying to use similar
 *       code to AddJavaScript to actual f1040 form was foolish.
 *    
 *       Also, attempts at using rups to examine the f1040.pdf to get some hint
 *       at how to get the actions dictionaly failed:
 *         http://article.gmane.org/gmane.comp.java.lib.itext.general/61335
 *       Hence, the need for trial and error.
 *     NEXT:
 *       Read the itext book more, or some Acrobat User Guide to try and
 *       infer how to get the action dictionary from an AcroFields.Item instance.
 *   @@2012-03-12.2212CST
 *     WHO: Larry Evans
 *     WHAT:
 *       Modified to mimic code in Listing 13.16 on page 450 of book.
 *     WHY:
 *       Because IRS form was mentioned on previous page, which hints that
 *       maybe the Listing 13.16 method of attaching actions to a form
 *       is better suited to IRS forms.
 *   @@2012-03-05.0500CST
 *     WHO: Larry Evans
 *     WHAT:
 *       Copied from:
 *          http://examples.itextpdf.com/src/part4/chapter13/ReplaceURL.java
 *       which was partly contained in Listing 13.15 of the book:
 *         "iText in Action - 2nd Edition"
 *       written by Bruno Lowagie (ISBN: 9781935182610)
 */

package part4.chapter13;

import java.io.FileOutputStream;
import java.io.IOException;

import part2.chapter08.ChildFieldEvent;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.GrayColor;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfFormField;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfString;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PushbuttonField;
import com.itextpdf.text.pdf.TextField;

public class F1040AddAction {

    /** The input form */
    public static final String FORMINP 
        = "resources/pdfs/f1040.pdf";
    /** The resulting manipulating FORMINP. */
    public static final String FORMOUT
        = "results/part4/chapter13/F1040AddAction.pdf";
    /** The input field, first name. */
    public static final String FIELDINP 
        = "topmostSubform[0].Page1[0].p1-t4[0]";
    /** The output field, last name. */
    public static final String FIELDOUT
        = "topmostSubform[0].Page1[0].p1-t5[0]";

    /**
     * Manipulates a PDF file src with the file dest as result
     * @param src the original PDF
     * @param dest the resulting PDF
     * @throws IOException
     * @throws DocumentException
     */
    public void manipulatePdf(String src, String dest)
        throws IOException, DocumentException {
        PdfReader reader = new PdfReader(src);
        reader.removeUsageRights();
        PdfStamper stamper 
          = new PdfStamper
            ( reader
            , new FileOutputStream(dest)
            , '\0'
            , true
            );
        PdfWriter writer = stamper.getWriter();
        AcroFields form = reader.getAcroFields();
        AcroFields.Item inp_item = form.getFieldItem(FIELDINP);
        PdfDictionary widgetRefDict = 
            (PdfDictionary) PdfReader.getPdfObject( inp_item.getWidgetRef(0));
        PdfDictionary actionDict = widgetRefDict.getAsDict(PdfName.AA);
        if(actionDict == null) actionDict=new PdfDictionary();
        actionDict.put
          ( PdfName.F
          , PdfAction.javaScript
            ( "app.alert('FIELDINP');}"
            , writer
            )
          );
        stamper.close();
    }
    
    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     */
    public static void main(String[] args)
        throws IOException, DocumentException {
        F1040AddAction form = new F1040AddAction();
        form.manipulatePdf(FORMINP, FORMOUT);
    }
}
------------------------------------------------------------------------------
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

Reply via email to