On 03/19/12 06:32, iText Info wrote:
> Op 19/03/2012 12:21, Larry Evans schreef:
>> I have studied the TextFieldActions example which you cited:
>>
>>    http://itextpdf.com/examples/iia.php?id=238
>>
>> In particular, in that example, there's:
>>
>>          TextField name = new TextField(writer, new Rectangle(130, 806,
>> 256, 780), "name");
> 
> TextField is a class that can only be used to CREATE new forms from scratch.
> You can't use it to change existing forms.
> 
>> However, my problem is that the .pdf file I'm starting from is:
>>
>>    http://www.irs.gov/pub/irs-pdf/f1040.pdf
>>
>> and I couldn't figure a way to get a TextField from that.
> 
> Correct. You shouldn't attempt to get a TextField object when dealing 
> with an existing PDF.
> 
> 
>>    I could only
>> find a way to get an AcroFields.Item from an AcroFields and a field
>> name;
> 
> That's what you need to do.
> 
>>   however, I could not figure how to get a PdfFormField from an
>> AcroField.
> 
> That's not possible. Just like TextField, PdfFormField is used for form 
> CREATION only, not for form manipulation. You're on the wrong track if 
> that's what you're trying to do.
> 
>> Item which I could then use to setAdditionalActions.
> 
> You need to add the infrastructure that is needed for an Additional 
> Action on the lowest level /AA and related dictionaries.
> 
> If it doesn't work, something in your infrastructure is missing. I'm 
> working on other assignments, so I don't have the time to look at any 
> actual PDFs.

The problem is here:

        PdfStamper stamper
          = new PdfStamper
            ( reader
            , new FileOutputStream(dest)
            , '\0'
            , true
            );

I wanted to preserve the ability to fill out the form as
shown in the Listing 8.29 of the book; however,
preserving that ability precludes adding any actions, AFAICT.
The attached illustrates this.  When the boolean arg to
manipulatepdf is true. the pdf can be filled; however, the
javascript action is not executed.  OTOH, when the boolean
arg to manipulatepdf is false, the pdf can no longer
be filled but the javascript action is executed.

BTW, I did read sections 8.7.1 and 8.7.2 of the book
(about Reader-enabling a form); however, since
there was no mention of any javascripts in those
sections, I thought it would work to modify the form
with a javascript and still retain the ability to fill
the form.

-regards,
Larry

/*
 * This class what modified from the one in the book
 * "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to: http://itextpdf.com/examples/
 * This example only works with the AGPL version of iText.
 */

package part2.chapter07;

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

import part1.chapter03.MovieTemplates;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfDestination;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;

public class EventsAndOpenAction {
    /** The resulting PDF for movie source. */
    public static final String RESULT_MOVIE
        = "results/part2/chapter07/events_and_open_action_movie";
    /** The f1040 source. */
    public static final String SRC_F1040
        = "resources/pdfs/f1040.pdf";
    /** The resulting PDF for f1040 source. */
    public static final String RESULT_F1040
        = "results/part2/chapter07/events_and_open_action_f1040";

    /**
     * Manipulates a PDF file src with the file dest as result
     * @param src the original PDF
     * @param resource a resource that will be used as advertisement
     * @param dest the resulting PDF
     * @throws IOException
     * @throws DocumentException
     */
    public void manipulatePdf(String src, String dest, boolean enable_extended)
        throws IOException, DocumentException {
    	// Create a reader
        PdfReader reader = new PdfReader(src);
        // Create a stamper
        PdfStamper stamper;
        String dest_ext=String.format("%s_%b.pdf",dest,enable_extended);
        if( enable_extended){
          stamper= new PdfStamper
            ( reader
            , new FileOutputStream(dest_ext)
            , '\0'
            , true
            );
        }
        else {
          stamper= new PdfStamper
            ( reader
            , new FileOutputStream(dest_ext)
            );
        }
        // Get the writer (to add actions and annotations)
        PdfWriter writer = stamper.getWriter();
        PdfAction action = PdfAction.javaScript(
            "app.alert('OpenAction');", writer);
        writer.setOpenAction( action);
        // Close the stamper
        stamper.close();
    }
    
    /**
     * Main method creating the PDF.
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException
     */
    public static void main(String[] args)
        throws IOException, DocumentException {
        MovieTemplates.main(args);
        EventsAndOpenAction e=new EventsAndOpenAction();
        e.manipulatePdf( MovieTemplates.RESULT, RESULT_MOVIE, true);
        e.manipulatePdf( SRC_F1040, RESULT_F1040, true);
        e.manipulatePdf( MovieTemplates.RESULT, RESULT_MOVIE, false);
        e.manipulatePdf( SRC_F1040, RESULT_F1040, false);
    }
}
------------------------------------------------------------------------------
This SF email is sponsosred by:
Try Windows Azure free for 90 days Click Here 
http://p.sf.net/sfu/sfd2d-msazure
_______________________________________________
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