如何使用iText在PDF中创建可编辑字段

时间:2016-09-01 05:00:54

标签: java itext

1)首先我尝试了以下.Below代码创建表的第二列可编辑,但是我希望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);
            }

        }
    }

现在我在第二列中获取文本,但它不再可编辑。 请帮忙。提前谢谢。

1 个答案:

答案 0 :(得分:0)

我找到了上述问题的解决方案。

我在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中看到文本,我也可以编辑它。