[英]How to create editable field in PDF using iText
1) First I tried following.Below code is creates second column of the table editable,however I want to have values of myVO.getClientAuthorized() and myVO.getClientSize() also in second column in respective two rows,but with following code I am not getting any value in second column. 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) Then I changed code to below.Basically I replaced 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);
}
}
}
Now I am getting text in second column,but it is no more editable. 现在,我在第二列中获取文本,但此文本不再可编辑。 Any help please.
请帮忙。 Thanks in advance.
提前致谢。
I found solution for my above question. 我为上述问题找到了解决方案。
I added following code in cellLayout() method. 我在cellLayout()方法中添加了以下代码。
textField.setText(textField.getFieldName());
So complete cellLayout() method ow looks like as below : 因此完整的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);
}
}
With this,I can see text in the generated PDF and I can edit it as well. 这样,我可以在生成的PDF中看到文本,也可以对其进行编辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.