I created a PDF with a table inside. I managed to create fields in selected cells by listening to the PdfPCellEvent:
cell.setCellEvent( new PdfPCellEvent() {
@Override
public void cellLayout( PdfPCell pdfPCell,
Rectangle rectangle,
PdfContentByte[] canvas ) {
PdfContentByte pcb = canvas[canvas.length - 1];
PdfWriter writer = pcb.getPdfWriter();
PdfFormField field = PdfFormField.createTextField( writer, false, false, 3 );
field.setName( "quantity " + hashCode() );
field.setFieldFlags( PdfFormField.FF_EDIT );
Rectangle rect = new Rectangle( rectangle.getLeft() + 5, rectangle.getTop() - 5,
rectangle.getLeft() + rectangle.getWidth() - 5,
rectangle.getTop() - rectangle.getHeight() + 5);
field.setWidget( rect, PdfAnnotation.HIGHLIGHT_OUTLINE );
field.setFieldFlags( PdfAnnotation.FLAGS_PRINT );
writer.addAnnotation( field );
writer.flush();
}
} );
It works fine as far as entering text into the fields is concerned. But once the field looses focus, the content disappears. If I set the focus to the field again, the data reappears.
My question is: what do I need to do in order for the text in the PdfFormField to stay printed?
there is a sample PDF here: http://qr.sertal.ch/output.pdf
thank you for your help.
I got an answer from the iText mailing list. The way to add form fields described in the book is deprecated. Here is the new way, which works fine:
I had the most problems with the orientation on a landscape page, until I found out you could set the Rotation of the field.