We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Radio Button Positioning PDF Java

Hi,

I have following code and that does not positioning radio buttons at required position mentioned in Rectangle object. I also tried other option where Radio button take place at required position but option’s text is not showing in that case, so that doesn’t work for me as well.
Please guide me what could be the possible solution.

Assume that I have generated PDF with below code and just sharing the snippet here.

Table table = new Table();
table.setColumnWidths(“120 120 120”);
page.getParagraphs().add(table);
Row r1 = table.getRows().add();
Cell c1 = r1.getCells().add();
Cell c2 = r1.getCells().add();
Cell c3 = r1.getCells().add();

	RadioButtonField rf = new RadioButtonField(page);
	rf.setPartialName("radio");
	document.getForm().add(rf, 1);

	RadioButtonOptionField opt1 = new RadioButtonOptionField(page, new Rectangle(170, 200, 270, 250));
	RadioButtonOptionField opt2 = new RadioButtonOptionField(page, new Rectangle(290, 200, 390, 250));
	RadioButtonOptionField opt3 = new RadioButtonOptionField(page, new Rectangle(410, 200, 510, 250));

	opt1.setOptionName("Item1");
	opt2.setOptionName("Item2");
	opt3.setOptionName("Item3");

	opt1.setWidth(15);
	opt1.setHeight(15);
	opt2.setWidth(15);
	opt2.setHeight(15);
	opt3.setWidth(15);
	opt3.setHeight(15);

	opt1.setBorder(new Border(opt1));
	opt1.getBorder().setWidth(1);
	opt1.getBorder().setStyle(BorderStyle.Solid);
	opt1.getCharacteristics().setBorder(Color.BLACK);
	opt1.getDefaultAppearance().setTextColor(Color.RED);
	opt1.setCaption(new TextFragment("Item1"));
	opt2.setBorder(new Border(opt2));
	opt2.getBorder().setWidth(1);
	opt2.getBorder().setStyle(BorderStyle.Solid);
	opt2.getCharacteristics().setBorder(Color.BLACK);
	opt2.getDefaultAppearance().setTextColor(Color.RED);
	opt2.setCaption(new TextFragment("Item2"));
	opt3.setBorder(new Border(opt3));
	opt3.getBorder().setWidth(1);
	opt3.getBorder().setStyle(BorderStyle.Solid);
	opt3.getCharacteristics().setBorder(Color.BLACK);
	opt3.getDefaultAppearance().setTextColor(Color.RED);
	opt3.setCaption(new TextFragment("Item3"));

	rf.add(opt1);
	rf.add(opt2);
	rf.add(opt3);

	c1.getParagraphs().add(opt1);
	c2.getParagraphs().add(opt2);
	c3.getParagraphs().add(opt3);

Thanks

@darshpatel

Could you please share the code snippet with which the radio buttons are showing on desired position along with an expected output PDF document. We will test the scenario in our environment and address it accordingly.

@asad.ali

here is the snippet but that does not bring label next to radio button but position radio button on PDF.

If you find a solution make sure that radio button belongs to the same group, I mean only one option is allowed to be selected from all of it.

	RadioButtonField rf = new RadioButtonField(page);
	rf.setPartialName("radio");

	RadioButtonOptionField opt1 = new RadioButtonOptionField(page, new Rectangle(170, 200, 270, 250));
	RadioButtonOptionField opt2 = new RadioButtonOptionField(page, new Rectangle(290, 200, 390, 250));
	RadioButtonOptionField opt3 = new RadioButtonOptionField(page, new Rectangle(410, 200, 510, 250));

	opt1.setOptionName("Item1");
	opt2.setOptionName("Item2");
	opt3.setOptionName("Item3");

	opt1.setWidth(15);
	opt1.setHeight(15);
	opt2.setWidth(15);
	opt2.setHeight(15);
	opt3.setWidth(15);
	opt3.setHeight(15);

	opt1.setBorder(new Border(opt1));
	opt1.getBorder().setWidth(1);
	opt1.getBorder().setStyle(BorderStyle.Solid);
	opt1.getCharacteristics().setBorder(Color.BLACK);
	opt1.getDefaultAppearance().setTextColor(Color.RED);
	opt1.setCaption(new TextFragment("Item1")); // opt1.setPartialName("Item1");

	opt2.setBorder(new Border(opt2));
	opt2.getBorder().setWidth(1);
	opt2.getBorder().setStyle(BorderStyle.Solid);
	opt2.getCharacteristics().setBorder(Color.BLACK);
	opt2.getDefaultAppearance().setTextColor(Color.RED);
	opt2.setCaption(new TextFragment("Item2")); // opt2.setPartialName("Item2");

	opt3.setBorder(new Border(opt3));
	opt3.getBorder().setWidth(1);
	opt3.getBorder().setStyle(BorderStyle.Solid);
	opt3.getCharacteristics().setBorder(Color.BLACK);
	opt3.getDefaultAppearance().setTextColor(Color.RED);
	opt3.setCaption(new TextFragment("Item3")); // opt3.setPartialName("Item3");

	rf.add(opt1);
	rf.add(opt2);
	rf.add(opt3);

	document.getForm().add(rf, 1);

ThanksHelloWorld_out.pdf (89.9 KB)

@darshpatel

We were able to show the captions for radio button options by adding following lines:

// if we add following lines then position of the buttons are changed
page.getParagraphs().add(opt1);
page.getParagraphs().add(opt2);
page.getParagraphs().add(opt3);

However, the position of the radio buttons were changed in the output PDF. For this, we have logged an issue as PDFJAVA-39580 in our issue management system. We will further look into its details and keep you posted with the status of its correction. Please be patient and spare us some time.

We are sorry for the inconvenience.

RadioButtonSample.pdf (73.0 KB)

@darshpatel

After investigation of the earlier logged ticket, we found that when adding RadioButtonOptionField like a paragraph of the page, the rectangle of RadioButtonOptionField does not set a position of the radio button.

In this case position of the radio button can be set using a Table generator.
Please, see the next code snippet:

        Document document = new Document();

        Page page = document.getPages().add();
        RadioButtonField rf = new RadioButtonField(page);
        rf.setPartialName("radio");

        Table table = new Table();
        table.setColumnWidths("100 20 100 20 100");
        Rectangle pageRect = page.getRect();
        table.setTop((float) pageRect.getHeight()-200-15);
        table.setLeft(170);
        page.getParagraphs().add(table);

        Row r1 = table.getRows().add();
        Cell c1 = r1.getCells().add();
        Cell c2 = r1.getCells().add();
        Cell c3 = r1.getCells().add();
        Cell c4 = r1.getCells().add();
        Cell c5 = r1.getCells().add();

        RadioButtonOptionField opt1 = new RadioButtonOptionField();
        RadioButtonOptionField opt2 = new RadioButtonOptionField();
        RadioButtonOptionField opt3 = new RadioButtonOptionField();

        opt1.setOptionName("Item1");
        opt2.setOptionName("Item2");
        opt3.setOptionName("Item3");

        opt1.setWidth(15);
        opt1.setHeight(15);
        opt2.setWidth(15);
        opt2.setHeight(15);
        opt3.setWidth(15);
        opt3.setHeight(15);


        rf.add(opt1);
        rf.add(opt2);
        rf.add(opt3);
        document.getForm().add(rf, 1);

        opt1.setBorder(new Border(opt1));
        opt1.getBorder().setWidth(1);
        opt1.getBorder().setStyle(BorderStyle.Solid);
        opt1.getCharacteristics().setBorder(com.aspose.pdf.Color.getBlack());
        opt1.getDefaultAppearance().setTextColor(java.awt.Color.RED);
        opt1.setCaption(new TextFragment("Item1"));
        opt2.setBorder(new Border(opt2));
        opt2.getBorder().setWidth(1);
        opt2.getBorder().setStyle(BorderStyle.Solid);
        opt2.getCharacteristics().setBorder(com.aspose.pdf.Color.getBlack());
        opt2.getDefaultAppearance().setTextColor(java.awt.Color.RED);
        opt2.setCaption(new TextFragment("Item2"));
        opt3.setBorder(new Border(opt3));
        opt3.getBorder().setWidth(1);
        opt3.getBorder().setStyle(BorderStyle.Solid);
        opt3.getCharacteristics().setBorder(com.aspose.pdf.Color.getBlack());
        opt3.getDefaultAppearance().setTextColor(java.awt.Color.RED);
        opt3.setCaption(new TextFragment("Item3"));

        c1.getParagraphs().add(opt1);
        c3.getParagraphs().add(opt2);
        c5.getParagraphs().add(opt3);


        document.save(dataDir + "PDFJAVA_39580_output.pdf");