Free Support Forum - aspose.com

Add RadioButton in PDF using C# | Aspose.PDF for .NET | Buttons are not showing in Adobe Reader

Hello,

I am trying to add radio button to my pdf file using code sample I found here (https://github.com/aspose-pdf/Aspose.PDF-for-.NET/blob/master/Examples/CSharp/AsposePDF/Forms/RadioButtonWithOptions.cs):

using (Aspose.Pdf.Document document = new Aspose.Pdf.Document(@"C:\\sample.pdf"))
{
    try
    {
        Page page = document.Pages.Add();
        Table table = new Table {ColumnWidths = "120 120 120"};
        page.Paragraphs.Add(table);

        Row r1 = table.Rows.Add();
        Cell c1 = r1.Cells.Add();
        Cell c2 = r1.Cells.Add();
        Cell c3 = r1.Cells.Add();

        RadioButtonField rf = new RadioButtonField(page) {PartialName = "radio"};
        document.Form.Add(rf, 1);

        RadioButtonOptionField opt1 = new RadioButtonOptionField
        {
            OptionName = "Item1", Height = 15, Width = 15
        };

        RadioButtonOptionField opt2 = new RadioButtonOptionField
        {
            OptionName = "Item2", Width = 15, Height = 15
        };

        RadioButtonOptionField opt3 = new RadioButtonOptionField
        {
            OptionName = "Item3", Width = 15, Height = 15
        };

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

        opt1.Border = new Border(opt1) {Width = 1, Style = BorderStyle.Solid};
        opt1.Characteristics.Border = System.Drawing.Color.Black;
        opt1.DefaultAppearance.TextColor = System.Drawing.Color.Red;
        opt1.Caption = new TextFragment("Item1");

        opt2.Border = new Border(opt1);
        opt2.Border.Width = 1;
        opt2.Border.Style = BorderStyle.Solid;
        opt2.Characteristics.Border = System.Drawing.Color.Black;
        opt2.DefaultAppearance.TextColor = System.Drawing.Color.Red;
        opt2.Caption = new TextFragment("Item2");

        opt3.Border = new Border(opt1);
        opt3.Border.Width = 1;
        opt3.Border.Style = BorderStyle.Solid;
        opt3.Characteristics.Border = System.Drawing.Color.Black;
        opt3.DefaultAppearance.TextColor = System.Drawing.Color.Red;
        opt3.Caption = new TextFragment("Item3");

        c1.Paragraphs.Add(opt1);
        c2.Paragraphs.Add(opt2);
        c3.Paragraphs.Add(opt3);

        document.Save(@"C:\radio_button_with_options.pdf");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

As a result I received pdf document with 3 radion buttons. Buy my question is why I cannot see any radio buttons via Adobe Reader? As you mentioned here (Add annotation in PDF using C# .NET | Aspose.PDF | Border is shown without specifying it)

Aspose.PDF mimics the behavior of Adobe Reader and follows its standards.

But I cannot see any radio button via Adobe Reader (screenshoot attached). However, I can see radio buttons If I open my file via Microsoft Edge browser (screenshoot also attached). Should radio buttons be displayed in Adobe Reader? If yes, why I cannot see it?

files.zip (7.8 KB)
radio_buttons_in_edge.png (6.3 KB)
radio_buttons_adobe_reader.png (10.3 KB)

@AlexBilakovskyi

Please try to use the below updated code snippet where we changed the sequence of some code lines and let us know in case you still notice any issue:

Page page = document.Pages.Add();
Table table = new Table { ColumnWidths = "120 120 120" };
page.Paragraphs.Add(table);

Row r1 = table.Rows.Add();
Cell c1 = r1.Cells.Add();
Cell c2 = r1.Cells.Add();
Cell c3 = r1.Cells.Add();

RadioButtonField rf = new RadioButtonField(page) { PartialName = "radio" };

RadioButtonOptionField opt1 = new RadioButtonOptionField
{
 OptionName = "Item1",
 Height = 15,
 Width = 15
};

RadioButtonOptionField opt2 = new RadioButtonOptionField
{
 OptionName = "Item2",
 Width = 15,
 Height = 15
};

RadioButtonOptionField opt3 = new RadioButtonOptionField
{
 OptionName = "Item3",
 Width = 15,
 Height = 15
};

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

opt1.Border = new Border(opt1) { Width = 1, Style = BorderStyle.Solid };
opt1.Characteristics.Border = System.Drawing.Color.Black;
opt1.DefaultAppearance.TextColor = System.Drawing.Color.Red;
opt1.Caption = new TextFragment("Item1");

opt2.Border = new Border(opt1);
opt2.Border.Width = 1;
opt2.Border.Style = BorderStyle.Solid;
opt2.Characteristics.Border = System.Drawing.Color.Black;
opt2.DefaultAppearance.TextColor = System.Drawing.Color.Red;
opt2.Caption = new TextFragment("Item2");

opt3.Border = new Border(opt1);
opt3.Border.Width = 1;
opt3.Border.Style = BorderStyle.Solid;
opt3.Characteristics.Border = System.Drawing.Color.Black;
opt3.DefaultAppearance.TextColor = System.Drawing.Color.Red;
opt3.Caption = new TextFragment("Item3");


document.Form.Add(rf, page.Number);

c1.Paragraphs.Add(opt1);
c2.Paragraphs.Add(opt2);
c3.Paragraphs.Add(opt3);

document.Save(dataDir + @"radio_button_with_options.pdf");

@asad.ali

Thank you for your answer! It now works exactly as I expected it to.

1 Like