0

I have been working with the generation of Editable PDF using iTextSharp.

Problem:

I want to create a Radio Group field inside a column of a table.

I tried and my code is:

//Gender section starts here --->
//Table creation
PdfPTable genderFieldTable = new PdfPTable(1);
genderFieldTable.HorizontalAlignment = 0;//0=Left, 1=Centre, 2=Right
genderFieldTable.DefaultCell.FixedHeight = 100f;
genderFieldTable.TotalWidth = 400;
genderFieldTable.LockedWidth = true;    

//Creating the RadioButton group starts here ---->

PdfFormField _radioGroup = PdfFormField.CreateRadioButton(pdfWriter, true);
_radioGroup.FieldName = "Gender";

string[] genders = { "Male", "Female" };

RadioCheckField genderRadioCheckField = null;
PdfFormField radioGField;
for (int i = 0; i < genders.Length; i++)
{
    genderRadioCheckField = new RadioCheckField(pdfWriter, new Rectangle(200, 806, 170, 890), null, genders[i]);
    //,,40, 806 - i * 40, 60, 788 - i * 40
    genderRadioCheckField.BackgroundColor = BaseColor.LIGHT_GRAY;
    genderRadioCheckField.Rotation = 180;
    genderRadioCheckField.CheckType = RadioCheckField.TYPE_CIRCLE;
    radioGField = genderRadioCheckField.RadioField;
    ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase(genders[i], new Font(Font.FontFamily.HELVETICA, 8)), 70, 790 - i * 40, 0);
    _radioGroup.AddKid(radioGField);
}

//pdfWriter.AddAnnotation(_radioGroup);
iTextSharp.text.pdf.events.FieldPositioningEvents genderEvents
    = new iTextSharp.text.pdf.events.FieldPositioningEvents(pdfWriter, genderRadioCheckField.RadioField);
PdfPCell genderFieldCell = new PdfPCell();
genderFieldCell.FixedHeight = 75f;

genderFieldCell.CellEvent = genderEvents;
genderFieldTable.AddCell(genderFieldCell);
formTable.AddCell(genderFieldTable);

I got the Male and Female text(not the radio circle field), but it is located somewhere and not inside the table.

Please help me to get Radiogroup inside the table.

krlzlx
  • 5,752
  • 14
  • 47
  • 55
Vikash
  • 857
  • 1
  • 13
  • 32
  • Possible duplicate of [iText RadioGroup/RadioButtons across multiple PdfPCells](http://stackoverflow.com/questions/29393050/itext-radiogroup-radiobuttons-across-multiple-pdfpcells) – Bruno Lowagie Oct 30 '15 at 15:01

2 Answers2

0

The radio buttons should be on the doc they are probably just off the page because of the Rectangle coordinates you've set.

Try:

genderRadioCheckField = new RadioCheckField(writer, new Rectangle(40, 806 - i * 40, 60, 788 - i * 40), null, genders[i]);

instead of

genderRadioCheckField = new RadioCheckField(pdfWriter, new Rectangle(200, 806, 170, 890), null, genders[i]);

There is no need to comment out the values under that line if you use them that should be your expected result. Hope this helps.

Saleh
  • 150
  • 10
  • I have used co-ordinates according to your suggestion, it is not working, still the same result. – Vikash Oct 30 '15 at 11:38
  • This answer is wrong: there is no relation between the coordinates of the `Rectangle` and the position of the cells in the table. The answer to this question is simple, you can find it [here](http://stackoverflow.com/questions/29393050/itext-radiogroup-radiobuttons-across-multiple-pdfpcells). – Bruno Lowagie Oct 30 '15 at 15:02
  • Do you know the version of iTextsharp you are using? – Saleh Oct 30 '15 at 15:11
  • I've tested it on a the latest version I'm using and managed to line up the text and the circles. If it's not working with you it must be something else in your code. The other thing that I have done which I didn't mention is uncomment the code `pdfWriter.AddAnnotation(_radioGroup);` also – Saleh Oct 30 '15 at 15:18
  • @Vikash Bruno is your guy on this he's got another way of implementation if you are still getting issues. I haven't tried it but he's the guy behind iText so I'd go with his method altogether. – Saleh Oct 30 '15 at 15:25
  • I think there might be a dll mismatch. Could you please share the code which you had simulated? @Bruno Lowagie – Vikash Nov 12 '15 at 12:53
0

Please take a look at the CreateRadioInTable example.

In this example, we create a PdfFormField for the radio group and we add it after constructing and adding the table:

PdfFormField radiogroup = PdfFormField.createRadioButton(writer, true);
radiogroup.setFieldName("Language");
PdfPTable table = new PdfPTable(2);
// add cells
document.add(table);
writer.addAnnotation(radiogroup);

When we create the cells for the radio buttons, we add an event, for instance:

cell.setCellEvent(new MyCellField(radiogroup, "english"));

The event looks like this:

class MyCellField implements PdfPCellEvent {
    protected PdfFormField radiogroup;
    protected String value;
    public MyCellField(PdfFormField radiogroup, String value) {
        this.radiogroup = radiogroup;
        this.value = value;
    }
    public void cellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases) {
        final PdfWriter writer = canvases[0].getPdfWriter();
        RadioCheckField radio = new RadioCheckField(writer, rectangle, null, value);
        try {
            radiogroup.addKid(radio.getRadioField());
        } catch (final IOException ioe) {
            throw new ExceptionConverter(ioe);
        } catch (final DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }
}
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • @Vikash As you can see, it works for me. Maybe you should outsource the assignment to somebody who can interpret my example correctly. – Bruno Lowagie Nov 05 '15 at 10:30
  • are you implementing it in iTextSharp in JAVA?. I am implementing in .NET. I changed my code according to .NET iTextsharp library. – Vikash Nov 05 '15 at 10:34
  • iText and iTextSharp are kept in sync. Whatever works in Java also works in iTextSharp. When you say "I tried, but no result", you are not inviting people to help you as explained here: http://lowagie.com/doesntwork Questions that can be answered with "it works for me" should be avoided. – Bruno Lowagie Nov 05 '15 at 10:36
  • Ok, I'll try. I'll let you know once finished. Thanks. – Vikash Nov 05 '15 at 10:40