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);
}
}
}
現在我得到的第二列文本,但它已不再是可編輯的。 請任何幫助。提前致謝。