[英]How to create editable field in PDF using iText
1)首先我嘗試以下。下面的代碼是創建表的第二列可編輯,但是我想在相應的兩行的第二列中同時具有myVO.getClientAuthorized()和myVO.getClientSize()的值,但是使用以下代碼在第二列中沒有任何值。
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)然后我將代碼更改為以下代碼。
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);
}
}
}
現在,我在第二列中獲取文本,但此文本不再可編輯。 請幫忙。 提前致謝。
我為上述問題找到了解決方案。
我在cellLayout()方法中添加了以下代碼。
textField.setText(textField.getFieldName());
因此完整的cellLayout()方法看起來像下面這樣:
public void cellLayout(PdfPCell cell, Rectangle rectangle,
PdfContentByte[] canvases) {
final PdfWriter writer = canvases[0].getPdfWriter();
final TextField textField = new TextField(writer, rectangle,
fieldname);
textField.setText(textField.getFieldName());
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);
}
}
這樣,我可以在生成的PDF中看到文本,也可以對其進行編輯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.