Hi all, thanks to help of you (especially 1T3XT BVBA) I'm now able to create tables with PdfFormFields in them.
But now I'm having a strange side-effect: The code attached generates the attached pdf. As you can see, the PdfFormFields of the last table are drawn on the second page, not in the table as I would expect. My questions: - Am I doing this the wrong way? If so, what is my problem here? - Is this a bug? If so, is there a workaround? Thanks Jonny
/* in_action/chapter15/SenderReceiver.java */ package com.foo; import java.io.FileOutputStream; import java.io.IOException; import java.util.Random; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.ExceptionConverter; import com.lowagie.text.Paragraph; import com.lowagie.text.Rectangle; 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.PdfWriter; import com.lowagie.text.pdf.TextField; /** * This example was written by Bruno Lowagie. It is part of the book 'iText in * Action' by Manning Publications. * ISBN: 1932394796 * http://www.1t3xt.com/docs/book.php * http://www.manning.com/lowagie/ */ public class SenderReceiver1 implements PdfPCellEvent { protected PdfFormField parent; protected String partialFieldName; protected PdfWriter writer; protected boolean required; public SenderReceiver1(PdfWriter writer, PdfFormField parent, String name, boolean required) { this.writer = writer; this.parent = parent; this.partialFieldName = name; this.required = required; } /** * @see com.lowagie.text.pdf.PdfPCellEvent#cellLayout(com.lowagie.text.pdf.PdfPCell, * com.lowagie.text.Rectangle, com.lowagie.text.pdf.PdfContentByte[]) */ public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] cb) { TextField tf = new TextField(writer, new Rectangle(rect.getLeft(2), rect .getBottom(2), rect.getRight(2), rect.getTop(2)), partialFieldName); if (required) tf.setOptions(TextField.REQUIRED); tf.setFontSize(12); try { parent.addKid(tf.getTextField()); } catch (Exception e) { throw new ExceptionConverter(e); } } public static void main(String[] args) { System.out.println("Chapter 15: example Sender Receiver"); System.out.println("-> Creates a PDF file with an AcroForm"); System.out.println("-> jars needed: iText.jar"); System.out.println("-> files generated in /results subdirectory:"); System.out.println(" sender_receiver.pdf"); // step 1 Document document = new Document(); try { // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Temp/senderreceiver_" + new Random().nextInt() + ".pdf")); // step 3 document.open(); // step 4 document.add(new Paragraph("Sender")); PdfFormField sender = PdfFormField.createEmpty(writer); sender.setFieldName("sender"); document.add(createTable(writer, sender)); writer.addAnnotation(sender); document.add(new Paragraph("Sender1")); PdfFormField sender1 = PdfFormField.createEmpty(writer); sender1.setFieldName("sender1"); document.add(createTable(writer, sender1)); writer.addAnnotation(sender1); document.add(new Paragraph("Sender2")); PdfFormField sender2 = PdfFormField.createEmpty(writer); sender2.setFieldName("sender2"); document.add(createTable(writer, sender2)); writer.addAnnotation(sender2); document.add(new Paragraph("Sender3")); PdfFormField sender3 = PdfFormField.createEmpty(writer); sender3.setFieldName("sender3"); document.add(createTable(writer, sender3)); writer.addAnnotation(sender3); document.add(new Paragraph("Sender4")); PdfFormField sender4 = PdfFormField.createEmpty(writer); sender4.setFieldName("sender4"); document.add(createTable(writer, sender4)); writer.addAnnotation(sender4); document.add(new Paragraph("Sender5")); PdfFormField sender5 = PdfFormField.createEmpty(writer); sender5.setFieldName("sender5"); document.add(createTable(writer, sender5)); writer.addAnnotation(sender5); document.add(new Paragraph("Receiver")); PdfFormField receiver = PdfFormField.createEmpty(writer); receiver.setFieldName("receiver"); document.add(createTable(writer, receiver)); writer.addAnnotation(receiver); } catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } // step 5 document.close(); } private static PdfPTable createTable(PdfWriter writer, PdfFormField parent) { PdfPTable table = new PdfPTable(2); PdfPCell cell; table.getDefaultCell().setPadding(5f); table.addCell("Your name:"); cell = new PdfPCell(); cell.setCellEvent(new SenderReceiver1(writer, parent, "name", true)); table.addCell(cell); table.addCell("Your home address:"); cell = new PdfPCell(); cell.setCellEvent(new SenderReceiver1(writer, parent, "address", false)); table.addCell(cell); table.addCell("Postal code:"); cell = new PdfPCell(); cell.setCellEvent(new SenderReceiver1(writer, parent, "postal_code", false)); table.addCell(cell); table.addCell("Your email address:"); cell = new PdfPCell(); cell.setCellEvent(new SenderReceiver1(writer, parent, "email", false)); table.addCell(cell); return table; } }
senderreceiver_387053930.pdf
Description: Adobe PDF document
------------------------------------------------------------------------------ Learn how Oracle Real Application Clusters (RAC) One Node allows customers to consolidate database storage, standardize their database environment, and, should the need arise, upgrade to a full multi-node Oracle RAC database without downtime or disruption http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________ iText-questions mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/itext-questions 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
