Hi Bruno,

Thanks. I did find your example before, but my code is different from yours.
Please look at the following code. I want to create name1 field on page1,
name2 field on page2, but I found 2 name1 fields on both pages, and on 2nd
page name1 and name2 fields are overlapped . Could you tell me why?

I really appreciate all your help.

**************************************
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.ExceptionConverter;
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.PdfReader;
import com.lowagie.text.pdf.PdfStamper;

public class Sarah implements PdfPCellEvent {
        protected PdfFormField parent;
        protected PdfFormField kid;
        protected float padding;
        protected PdfWriter writer;
        protected int pageNo;

        public Sarah(PdfWriter writer, PdfFormField parent, PdfFormField
kid, float padding, int pageNo) {
                this.writer = writer;
           this.kid = kid;
           this.parent = parent;
           this.padding = padding;
                this.pageNo = pageNo;
        }

        public void cellLayout(PdfPCell cell, Rectangle rect,
PdfContentByte[] cb) {
             kid.setWidget(new Rectangle(rect.getLeft(padding),
rect.getBottom(padding),
                                rect.getRight(padding), rect.getTop(padding)),
                                PdfAnnotation.HIGHLIGHT_INVERT);
                  try { kid.setPlaceInPage(pageNo);
                        writer.addAnnotation(kid);
                    parent.addKid(kid);         
              } catch (Exception e) {
                    throw new ExceptionConverter(e);
              }
        }

       public static void main(String[] args) {
           createPdf();
                try {   PdfReader reader;
                        PdfStamper stamper;
                        reader = new
PdfReader("C:/_JBuilder2005Webs/sarah.pdf");
                        stamper = new PdfStamper(reader, new
FileOutputStream(
                                       
"C:/_JBuilder2005Webs/sarah_data.pdf"));
                        AcroFields form = stamper.getAcroFields();
                        form.setField("person.name1", "hello");
                        form.setField("person.name2", "Sarah");
                        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/sarah.pdf"));
                        document.open();
                        PdfFormField person = PdfFormField.createEmpty(writer);
                        person.setFieldName("person");
                        document.add(createTable(writer, person, 1));
                                      document.newPage();
                                      document.add(createTable(writer,
person, 2));
                        writer.addAnnotation(person);
                } catch (DocumentException de) {
                        System.err.println(de.getMessage());
                } catch (IOException ioe) {
                        System.err.println(ioe.getMessage());
                }
                document.close();
        }

        private static PdfPTable createTable(PdfWriter writer, PdfFormField 
parent,
int pageNum)
                        throws IOException, DocumentException {
                PdfPTable table = new PdfPTable(2);
                PdfPCell cell;
                TextField field;
                table.addCell("Your name:");
                cell = new PdfPCell();
                field = new TextField(writer, new Rectangle(0, 0), 
"name"+pageNum);
                cell.setCellEvent(new Sarah(writer, parent, 
field.getTextField(), 1,
pageNum));
                table.addCell(cell);
                return table;
        }
}
***********************************************************
-- 
View this message in context: 
http://www.nabble.com/why-pdf-fields-of-1st-page-move-to-2nd-page-tp15299931p15467153.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