面对CellField上Itext checkBox的问题

时间:2016-08-23 10:56:28

标签: java itext

我正在使用下面的类添加复选框以及一些文本

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[] {
            30 f, 30 f
        };
        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();
    }
}

我在上面的代码

中遇到了问题

cell12.setCellEvent(new CellField(writer1, checkboxGroupField, true));

问题是'无法找到符号类CellField'

有任何帮助吗?

1 个答案:

答案 0 :(得分:0)

您收到错误'无法找到符号类CellField'是正常的,因为这不是官方的iText类。检查iText 5 API documentation,您将找不到它。

也许这是你写的课,但忘了导入。也许这是别人写的一个类,但你忘了复制它。您是否从示例中复制了代码?如果是这样,请告诉我们您在哪里找到该示例。

我用Google搜索“setCellEvent(new CellField”),我找到了对这个问题的引用: My checkboxes are always checked. I cannot create them unchecked

在这种情况下,CellField类的编写如下:

protected class CellField implements PdfPCellEvent {
    private PdfFormField parent;

    private String partialFieldName;
    private PdfWriter writer;
    private boolean checked;

    public CellField(PdfWriter writer, PdfFormField parent, boolean checked) {
        this.writer = writer;
        this.parent = parent;
        this.checked = checked;
    }

    public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] cb) {
        try {
            createCheckboxField(rect);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    private void createCheckboxField(Rectangle rect) throws IOException, DocumentException {
        RadioCheckField rf = new RadioCheckField(writer, new Rectangle(rect.getLeft(2), rect.getBottom(2),
                rect.getRight(2), rect.getTop(2)), partialFieldName, "");
        rf.setChecked(checked);
        rf.setBorderColor(GrayColor.GRAYBLACK);
        rf.setBackgroundColor(GrayColor.GRAYWHITE);
        rf.setCheckType(RadioCheckField.TYPE_CHECK);

        parent.addKid(rf.getCheckField());
    }
}

如果您确实知道使用由其他人编写的名为CellField的类的灵感,请确保将其与您正在使用的其他代码一起复制。

还可以通过复制您不理解的代码来避免失去Karma。如果你追求伟大,你应该充分了解你做出的每一个决定。这包括了解您从其他人那里借来的代码。如果你不追求伟大......那么,我们通常不会那样打扰那样的人,对吗?