我主要知道如何使用itext创建可编辑的pdf单元格。但在某些情况下,只能使用Tab键访问可编辑单元格。它无法点击。
可能是什么原因?它似乎与定位或协调问题(矩形x / y值)有关。但也许它是一个内部的迭代错误。
public void cellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) {
if (this.enable) {
final PdfWriter writer = canvases[0].getPdfWriter();
final TextField textField = new TextField(writer, rectangle, String.format("text_%s", this.fieldname));
textField.setOptions(BaseField.EDIT);
try {
final PdfFormField field = textField.getTextField();
field.setFieldFlags(PdfFormField.FF_EDIT);
writer.addAnnotation(field);
} catch (final IOException ioe) {
throw new ExceptionConverter(ioe);
} catch (final DocumentException de) {
throw new ExceptionConverter(de);
}
}
}
答案 0 :(得分:0)
当您从PdfFormField
创建TextField
时,默认情况下它是交互式的(可编辑的),因此无需设置任何标记。我已经在一个小概念证明中复制/粘贴了您的代码:
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);
}
}
}
我已经将概念证明命名为CreateFormInTable
,这就是我对细胞事件所做的事情:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable table = new PdfPTable(2);
PdfPCell cell;
cell = new PdfPCell(new Phrase("Name:"));
table.addCell(cell);
cell = new PdfPCell();
cell.setCellEvent(new MyCellField("name"));
table.addCell(cell);
cell = new PdfPCell(new Phrase("Address"));
table.addCell(cell);
cell = new PdfPCell();
cell.setCellEvent(new MyCellField("address"));
table.addCell(cell);
document.add(table);
document.close();
}
结果是form_in_table.pdf。从屏幕截图中可以看出,字段是可编辑的:
如果这不能回答您的问题,请提供一个SSCCE来重现问题。我是iText的原始开发者。我从来没有见过你所说的那种行为。