Sarah1 wrote:
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?

Because your sample is written by somebody
who writes code without understanding what
every line means ;-)

See attachment (and adapt it wisely).
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;

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

        public Sarah(PdfFormField kid) {
            this.kid = kid;
        }

        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);
             System.out.println(pageNo);
        }

       public static void main(String[] args) {
           createPdf();
                try {   PdfReader reader;
                        PdfStamper stamper;
                        reader = new
PdfReader("sarah.pdf");
                        stamper = new PdfStamper(reader, new
FileOutputStream(
                                       
"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("sarah.pdf"));
                        document.open();
                        PdfFormField person = PdfFormField.createEmpty(writer);
                        person.setFieldName("person");
                        TextField field1 = new TextField(writer, new 
Rectangle(0, 0), "name1");
                        PdfFormField kid1 = field1.getTextField();
            kid1.setPlaceInPage(1);
            person.addKid(kid1);
                        TextField field2 = new TextField(writer, new 
Rectangle(0, 0), "name2");
            PdfFormField kid2 = field2.getTextField();
            kid2.setPlaceInPage(2);
            person.addKid(kid2);
                        writer.addAnnotation(person);
                        document.add(createTable(kid1));
            document.newPage();
            document.add(createTable(kid2));
            document.newPage();
                } catch (DocumentException de) {
                        System.err.println(de.getMessage());
                } catch (IOException ioe) {
                        System.err.println(ioe.getMessage());
                }
                document.close();
        }

        private static PdfPTable createTable(PdfFormField kid)
                        throws IOException, DocumentException {
                PdfPTable table = new PdfPTable(2);
                table.addCell("Your name:");
                PdfPCell cell = new PdfPCell();
                cell.setCellEvent(new Sarah(kid));
                table.addCell(cell);
                return table;
        }
}
-------------------------------------------------------------------------
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