Hi Bruno,
Now I'm confused by adding radio buttons on different page. 
I want name1(text), preferred1(radio) on 1st page, name2 and
preferred2(radio) on 2nd page. I also want to use a cool looking checkbox
instead of big X. (I can make big X checkboxes working, but not the
following code.) Could you help me? Really appriciate.
******************************************************
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.PdfAnnotation;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfFormField;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPCellEvent;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.TextField;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.RadioCheckField;
import com.lowagie.text.pdf.BaseField;
import java.awt.Color;
import com.lowagie.text.ExceptionConverter;

public class Form2pages implements PdfPCellEvent {
        protected PdfFormField kid;
        protected float padding;
        protected int pageNo;
        protected String fieldName;
        protected String fieldValue;
        protected String fieldType;
        protected PdfWriter writer;

        public Form2pages(PdfWriter writer, PdfFormField kid, String
fieldName, 
                          String fieldValue, String fieldType, int pageNo) {
            this.kid = kid;
            this.fieldName = fieldName;
            this.fieldValue = fieldValue;
            this.fieldType = fieldType;
            this.writer = writer;
            this.pageNo = pageNo;
        }

        public void cellLayout(PdfPCell cell, Rectangle rect,
PdfContentByte[] cb) {
          if(fieldType.equalsIgnoreCase("text")){
            kid.setWidget(new Rectangle(rect.getLeft(padding),
rect.getBottom(padding),
                                rect.getRight(padding),
rect.getTop(padding)),
                                PdfAnnotation.HIGHLIGHT_INVERT);
          }else if(fieldType.equalsIgnoreCase("check")){
            float llx = rect.getLeft() + ( (rect.getRight() -
rect.getLeft()) / 2f) - 5f;
            float lly = rect.getBottom() + ( (rect.getTop() -
rect.getBottom()) / 2f) - 5f;
            try {
              RadioCheckField rf = new RadioCheckField(writer, new
Rectangle(llx, lly, llx + 10, lly + 10), fieldName, fieldValue);
              rf.setCheckType(RadioCheckField.TYPE_CHECK);
              rf.setBorderWidth(BaseField.BORDER_WIDTH_THIN);
              rf.setBorderColor(Color.black);
              rf.setBackgroundColor(Color.white);
              rf.setChecked(true);
              PdfFormField ff = rf.getCheckField();
              ff.setPlaceInPage(pageNo);
              kid.addKid(ff);
            } catch (Exception e) {
              throw new ExceptionConverter(e);
            }
          }
        }

       public static void main(String[] args) {
           createPdf();
                try {   PdfReader reader;
                        PdfStamper stamper;
                        reader = new
PdfReader("C:/_Jbuilder2005Webs/Form2pages.pdf");
                        stamper = new PdfStamper(reader, new
FileOutputStream( "C:/_Jbuilder2005Webs/Form2pages_data.pdf"));
                        AcroFields form = stamper.getAcroFields();
                        form.setField("person.name1", "hello");
                        form.setField("person.preferred1","English");
                        form.setField("person.name2", "Sarah");
                        form.setField("person.preferred2","French");
                        stamper.close();
                } catch (IOException e) {
                        e.printStackTrace();
                } catch (DocumentException e) {
                        e.printStackTrace();
                }
        }

        public static void createPdf() {
                Document document = new Document();
                try {
                        PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("C:/_Jbuilder2005Webs/Form2pages.pdf"));
                        document.open();

                        PdfFormField person =
PdfFormField.createEmpty(writer);
                        person.setFieldName("person");

                        // name1 on 1st page
                        TextField field1 = new TextField(writer, new
Rectangle(0, 0), "name1");
                        PdfFormField kid_name1 = field1.getTextField();
                        kid_name1.setPlaceInPage(1);
                        person.addKid(kid_name1);

                        // preferred1 on 1st page
                        String[] checkboxLabels = {"English","French"};
                        PdfFormField kid_preferred1 =
PdfFormField.createRadioButton(writer, true);
                        kid_preferred1.setFieldName("preferred1");
                        kid_preferred1.setPlaceInPage(1);
                        person.addKid(kid_preferred1);                     

                        // name2 on 2nd page
                        TextField field2 = new TextField(writer, new
Rectangle(0, 0), "name2");
                        PdfFormField kid_name2 = field2.getTextField();
                        kid_name2.setPlaceInPage(2);
                        person.addKid(kid_name2);      
                        
                        // preferred2 on 2nd page
                        PdfFormField kid_preferred2 =
PdfFormField.createRadioButton(writer, true);
                        kid_preferred2.setFieldName("preferred2");
                        kid_preferred2.setPlaceInPage(2);
                        person.addKid(kid_preferred2);
                        
                        writer.addAnnotation(person);
                        
                        document.add(createTextTable(null,
kid_name1,null,"Your Name1:", 30, 1));                   
                        document.add(createCheckTable(writer,
kid_preferred1,"preferred1", checkboxLabels, 80, 1));
                        document.newPage();
                        document.add(createTextTable(null,
kid_name2,null,"Your Name2", 30, 2));
                        document.add(createCheckTable(writer,
kid_preferred2,"preferred2", checkboxLabels, 80, 2));             
 
                } catch (DocumentException de) {
                        System.err.println(de.getMessage());
                } catch (IOException ioe) {
                        System.err.println(ioe.getMessage());
                }
                document.close();
        }

        private static PdfPTable createTextTable(PdfWriter writer,
PdfFormField kid, 
                                                 String fieldStr,
                                                 String label, int
kid_height,
                                                 int pageNo)
                        throws IOException, DocumentException {
                PdfPTable table = new PdfPTable(2);
                table.setTotalWidth(540f);
                table.addCell(label);
                PdfPCell cell = new PdfPCell();
                cell.setFixedHeight(kid_height);
                cell.setCellEvent(new Form2pages(writer, kid, null, null,
"text", pageNo));
                table.addCell(cell);
                return table;
        }

        private static PdfPTable createCheckTable(PdfWriter writer,
PdfFormField kid, 
                                                  String fieldName,
                                                  String[] checkboxLabels,
int kid_height,
                                                  int pageNo)
                        throws IOException, DocumentException {
                PdfPTable table = new PdfPTable(2);
                table.setTotalWidth(540f);
                table.addCell("Preferred Language:");

                PdfPTable subtable = new PdfPTable(new float[]{4,10});
                for(int i=0; i<checkboxLabels.length; i++){
                    PdfPCell cell = new PdfPCell();
                    cell.setFixedHeight(kid_height);
                    cell.setCellEvent(new Form2pages(writer, kid, fieldName,
checkboxLabels[i], "check", pageNo) );
                    subtable.addCell(cell);
                    cell = new PdfPCell(new Phrase(checkboxLabels[i]));
                    subtable.addCell(cell);
                }
                table.addCell(new PdfPCell(subtable));     
                return table;
        }


}
***********************************************************

Sarah
-- 
View this message in context: 
http://www.nabble.com/why-pdf-fields-of-1st-page-move-to-2nd-page-tp15299931p15508686.html
Sent from the iText - General mailing list archive at Nabble.com.


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Reply via email to