如何将复选框添加到Itext pdf表中

时间:2014-01-04 09:49:49

标签: java pdf itext

我在Itext PDF表格中生成复选框但是在表格内部没有生成其生成外表格。请帮我如何将该复选框附加到PDF表格中。请你帮我解决。 以下是我的代码:

Class A{
         public void createFourColumnBody() throws DocumentException, FileNotFoundException {
                com.itextpdf.text.Document document = new com.itextpdf.text.Document(PageSize.A4);
                com.itextpdf.text.pdf.PdfWriter writer1 = PdfWriter.getInstance(document, new FileOutputStream("D:\\PDF_Java.pdf", false));
                document.open();
                float[] widths = new float[]{30f, 30f};
                com.itextpdf.text.pdf.PdfPTable table = new com.itextpdf.text.pdf.PdfPTable(widths);
                table.setWidthPercentage(100);
                table.setHorizontalAlignment(Element.ALIGN_LEFT);
                PdfFormField checkboxGroupField = PdfFormField.createCheckBox(writer1);
                PdfPCell cell = table.getDefaultCell();
                PdfPCell cell12;
                cell = new PdfPCell(new Paragraph("checkbox3"));
                table.addCell(cell);
                cell12 = new PdfPCell(table.getDefaultCell());
                cell12.setCellEvent(new CellField(writer1, checkboxGroupField, true));
                table.addCell(cell12);
                writer1.addAnnotation(checkboxGroupField);
                document.add(table);
                document.close();
        }   

    public static void main(String[] args) throws DocumentException, FileNotFoundException    {
        A a1 = new A();
        a1.createFourColumnBody();
    }
    }

1 个答案:

答案 0 :(得分:2)

CheckboxCell示例中对此进行了解释。这个例子是在回答Resizing a form field in iTextSharp时写的,但它也回答了你的问题。

请注意,由于您使用了[itextpdf]标记而不是[itext]或[itextsharp],因此您的问题仍未得到解答。我只是巧合地发现了这个问题。