0
1)首先,我尝试了以下内容。下面的代码创建表的第二列可编辑,但是我希望myVO.getClientAuthorized()和myVO.getClientSize()的值也在第二列在相应的两行,但下面的代码我没有得到任何价值的第二列。如何使用iText创建PDF中的可编辑字段
public PdfPTable createTable(MyVO myVO){
PdfPTable table = null;
table = new PdfPTable(2);
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
PdfPCell cell;
cell = new PdfPCell(new Phrase(
myVO.getClientAuthorizedLbl()));
table.addCell(cell);
cell = new PdfPCell();
cell.setCellEvent(new MyCellField(myVO
.getClientAuthorized()));
table.addCell(cell);
cell = new PdfPCell(new Phrase(
myVO.getClientSizeLbl()));
table.addCell(cell);
cell = new PdfPCell();
cell.setCellEvent(new MyCellField(myVO
.getClientSize()));
table.addCell(cell);
return table;
}
class MyCellField implements PdfPCellEvent {
protected String fieldname;
public MyCellField(String fieldname) {
this.fieldname = fieldname;
}
public void cellLayout(PdfPCell cell, Rectangle rectangle,
PdfContentByte[] canvases) {
final PdfWriter writer = canvases[0].getPdfWriter();
final TextField textField = new TextField(writer, rectangle,
fieldname);
try {
final PdfFormField field = textField.getTextField();
writer.addAnnotation(field);
} catch (final IOException ioe) {
throw new ExceptionConverter(ioe);
} catch (final DocumentException de) {
throw new ExceptionConverter(de);
}
}
}
2)然后,我改变代码below.Basically我更换
cell = new PdfPCell();
with
cell = new PdfPCell(new Phrase(myVO.getClientAuthorized()));
and second cell = new PdfPCell();
with
cell = new PdfPCell(new Phrase(myVO.getClientSize()));
below is changed code :
public PdfPTable createTable(MyVO myVO){
PdfPTable table = null;
table = new PdfPTable(2);
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
PdfPCell cell;
cell = new PdfPCell(new Phrase(
myVO.getClientAuthorizedLbl()));
table.addCell(cell);
cell = new PdfPCell(new Phrase(myVO.getClientAuthorized()));
cell.setCellEvent(new MyCellField(myVO
.getClientAuthorized()));
table.addCell(cell);
cell = new PdfPCell(new Phrase(
myVO.getClientSizeLbl()));
table.addCell(cell);
cell = new PdfPCell(new Phrase(myVO
.getClientSize()));
cell.setCellEvent(new MyCellField(myVO
.getClientSize()));
table.addCell(cell);
return table;
}
class MyCellField implements PdfPCellEvent {
protected String fieldname;
public MyCellField(String fieldname) {
this.fieldname = fieldname;
}
public void cellLayout(PdfPCell cell, Rectangle rectangle,
PdfContentByte[] canvases) {
final PdfWriter writer = canvases[0].getPdfWriter();
final TextField textField = new TextField(writer, rectangle,
fieldname);
try {
final PdfFormField field = textField.getTextField();
writer.addAnnotation(field);
} catch (final IOException ioe) {
throw new ExceptionConverter(ioe);
} catch (final DocumentException de) {
throw new ExceptionConverter(de);
}
}
}
现在我得到的第二列文本,但它已不再是可编辑的。 请任何帮助。提前致谢。