Op 4/01/2011 17:51, Johannes Becker schreef:
Hi,

thanks for the quick reply.

  Does the problem also exist in the most recent iText version?
Yes it does (iText 5.0.5).
It doesn't in the SNAPSHOT version of iText 5.0.6.
See attached code sample as well as the resulting PDF,
so whatever problem there was: it's already fixed ;-)

Attachment: senderreceiver_1766675841.pdf
Description: Adobe PDF document

/* in_action/chapter15/SenderReceiver.java */
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfFormField;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.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 SenderReceiver implements PdfPCellEvent {
 
        protected PdfFormField parent;
 
        protected String partialFieldName;
 
        protected PdfWriter writer;
 
        protected boolean required;
 
        public SenderReceiver(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("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 SenderReceiver(writer, parent, "name", 
true));
                table.addCell(cell);
 
                table.addCell("Your home address:");
                cell = new PdfPCell();
                cell.setCellEvent(new SenderReceiver(writer, parent, "address", 
false));
                table.addCell(cell);
 
                table.addCell("Postal code:");
                cell = new PdfPCell();
                cell.setCellEvent(new SenderReceiver(writer, parent, 
"postal_code",
                                false));
                table.addCell(cell);
 
                table.addCell("Your email address:");
                cell = new PdfPCell();
                cell.setCellEvent(new SenderReceiver(writer, parent, "email", 
false));
                table.addCell(cell);
 
                return table;
        }
}
------------------------------------------------------------------------------
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

Reply via email to