2016-09-01 85 views
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); 
      } 

     } 
    } 

现在我得到的第二列文本,但它已不再是可编辑的。 请任何帮助。提前致谢。

回答

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文本,我可以编辑它。