2

I am creating a PDF in which the AcroForm fields will be rendered inside a table. I am having issue with RadioButtons when they spread across two pages. I have modified example shown on this link http://itextpdf.com/sandbox/acroforms/CreateRadioInTable . Only the craetePdf method from above example is modified.

public void createPdf(String dest) throws IOException, DocumentException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(dest));
    document.open();
    PdfFormField radiogroup = PdfFormField.createRadioButton(writer, true);
    radiogroup.setFieldName("Language");
    PdfPTable table = new PdfPTable(2);
    PdfPCell cell;
    for (int i = 0; i < 25; i++) {
        cell = new PdfPCell(new Phrase("Question " + i));
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("Answer " + i));
        table.addCell(cell);
    }
    for (int i = 0; i <25; i++) {
        cell = new PdfPCell(new Phrase("Radio: " + i));
        table.addCell(cell);
        cell = new PdfPCell();
        cell.setCellEvent(new MyCellField(radiogroup, "radiogroup" + i));
        table.addCell(cell);
    }
    document.add(table);
    writer.addAnnotation(radiogroup);
    document.close();
}

A PdfPTable is created with 50 rows. When the radiobutton gets split in between two pages, it gets messed up in formatting. Please check this link for output PDF from above code. http://www.docdroid.net/13smb/checkbox-in-cell.pdf.html

By looking at the PDF it appears that all the radiobuttons gets rendered on page 2 because the table ends on page 2. I have tried to set page number with setPageInPlace(pageNumber) while creating RadioCheckFields as following, but it did not work.

RadioCheckField radio = new RadioCheckField(writer, rectangle, null, value);
PdfFormField field = radio.getRadioField();
field.setPlaceInPage(placeInPage);

Is there a way to fix this issue? I have tried to use PdfStamper, but was not able to fix this problem. Any help will be greatly appreciated.

  • 1
    At the moment you are adding the `radiogroup` all the page dictionaries have already been sent to the `OutputStream`. iText can not go back to these page dictionaries to add the kids of the radio group. If you want to use `setPlaceInPage`, you need to do this *before* the page has been rendered. You can't do that, because at that time, you don't even know on which page your radio buttons will occur. Hence you are asking something that is currently not supported in iText. You'll have to ask the people at Premium support to implement this (that's not free). – Bruno Lowagie Jun 18 '15 at 14:32
  • @BrunoLowagie Thanks for quick reply. Can `PdfStamper` correct the page numbers of misplaced radiobutton in second pass? It seems `AcroFields.Item` class has a property named `page` which is an `ArrayList` of page numbers of individual `RadioCheckField`. Can it be modified to correct the problem? – Amit Walvekar Jun 18 '15 at 15:34
  • 1
    Maybe, but that's hard work. I have looked into the problem more closely. I think I've found a solution where you add the widget annotations early, and then prevent that the annotations are added a second time when adding the group. This needs more testing though. It requires a patch to iText. – Bruno Lowagie Jun 18 '15 at 15:39
  • @BrunoLowagie if such a patch becomes available, it will be a lifesaver. If I understood correctly, it means I can use following during table cell rendering in the cellLayout method. `RadioCheckField radio = new RadioCheckField(writer, rectangle, null, value); PdfFormField field = radio.getRadioField(); writer.addAnnotation(field);` Please let me know how do I remain informed about the availability of such patch? Thanks again. – Amit Walvekar Jun 18 '15 at 19:13
  • 1
    All patches are available on GitHub: https://github.com/itext/itextpdf/commit/30cd9e733d04923814eba1aa325972a19b18a2d2 Note that this patch expects that you add `field` in the cell event. If you don't, the wrong behavior you're experiencing will persist. – Bruno Lowagie Jun 19 '15 at 06:14
  • Thanks, the patch worked nicely. Just to add to this, the addAnnotation needs to happen before kids are added to group. Like this `writer.addAnnotation(field); radiogroup.addKid(field);` If we add kids to the group before writer.addAnnotation then the radioCheckFields don't show up. – Amit Walvekar Jun 19 '15 at 20:56

0 Answers0