public class SignatureTicketView extends AbstractPdfView
{
private static final Logger logger = Logger.getLogger(SignatureTicketView.class);
private static DateFormat formatter = new SimpleDateFormat("dd.MM.yyyy hh:mm");
@Override
protected void buildPdfDocument(Map model, Document document,
PdfWriter writer, HttpServletRequest request,
HttpServletResponse response) throws Exception
{
Date timestamp = (Date)model.get("timestamp");
float[] widths = {30f, 70f};
PdfPTable table = new PdfPTable(widths);
table.getDefaultCell().setPadding(5f);
table.setHorizontalAlignment(Element.ALIGN_LEFT);
table.getDefaultCell().setBorderWidth(0);
table.getDefaultCell().setBorderColor(Color.WHITE);
//...
PdfPCell cell = new PdfPCell();
TextField tf = new TextField(writer,new Rectangle(0,0,100,25),"timestamp");
tf.setBackgroundColor(Color.WHITE);
tf.setBorderColor(Color.WHITE);
tf.setBorderWidth(0);
tf.setText(formatter.format(timestamp));
tf.setDefaultText(formatter.format(timestamp));
tf.setOptions(TextField.READ_ONLY);
cell.setMinimumHeight(20);
cell.setCellEvent(new FieldCell(tf.getTextField(),writer,100));
table.addCell(cell);
//...
document.add(table);
}
class FieldCell implements PdfPCellEvent
{
PdfFormField formField;
PdfWriter writer;
int width;
public FieldCell(PdfFormField formField, PdfWriter writer, int width)
{
super();
this.formField = formField;
this.writer = writer;
this.width = width;
}
@Override
public void cellLayout(PdfPCell cell, Rectangle rect,PdfContentByte[] canvas)
{
try
{
// delete cell border
PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
cb.reset();
formField.setWidget(
new Rectangle(rect.getLeft(),
rect.getBottom(),
rect.getLeft()+width,
rect.getTop()),
PdfAnnotation
.HIGHLIGHT_NONE);
writer.addAnnotation(formField);
}catch(Exception e){
System.out.println(e);
}
}
}
@Override
protected void writeToResponse(HttpServletResponse response, ByteArrayOutputStream baos) throws IOException
{
ByteArrayInputStream in = new ByteArrayInputStream(baos.toByteArray());
PdfReader reader = new PdfReader(in);
baos = new ByteArrayOutputStream();
try
{
PdfStamper stamper = new PdfStamper(reader,baos);
stamper.setFormFlattening(true);
stamper.close();
} catch (DocumentException e)
{
logger.error(e.getMessage());
}
response.setContentType(getContentType());
response.setContentLength(baos.size());
ServletOutputStream out = response.getOutputStream();
baos.writeTo(out);
out.flush();
}
}