Free Support Forum - aspose.com

Trying to Create Aspose Form Fields Dynamically

Hi,


I am trying to create FormFields Dynamically using the following code:

doc.Pages.Add();
// Instatiate RadioButtonField object with page number as argument
RadioButtonField radio = new RadioButtonField(doc.Pages[1]);
// Add first radio button option and also specify its origin using Rectangle object
radio.AddOption(“Test”, new Rectangle(0, 0, 20, 20));
// Add second radio button option
radio.AddOption(“Test1”, new Rectangle(20, 20, 40, 40));
// Add radio button to form object of Document object
doc.Form.Add(radio);
But it is throwing me error as below:

at Aspose.Pdf.InteractiveFeatures.Forms.RadioButtonOptionField. ( , Annotation )
at Aspose.Pdf.InteractiveFeatures.Forms.RadioButtonOptionField.createXForm(Boolean , Annotation )
at Aspose.Pdf.InteractiveFeatures.Forms.RadioButtonOptionField. (Annotation )
at Aspose.Pdf.InteractiveFeatures.Forms.Field. ()
at Aspose.Pdf.InteractiveFeatures.Forms.Field.set_Rect(Rectangle value)
at Aspose.Pdf.InteractiveFeatures.Forms.RadioButtonField.AddOption(String optionName, Rectangle rect)
at FileUploadApi.Controllers.DocFileSaveController.Post(RequestDataModel wholeData) in C:\Users\nithin.eate\Desktop\FileUploadWebAPI\FileUploadApi\FileUploadApi\Controllers\DocFileSaveController.cs:line 181
at lambda_method(Closure , Object , Object[] )
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.b__9(Object instance, Object[] methodParameters)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)

Can Anybody help me out in resolving this issue.

Regards,
Nithin Eate

Hi Nithin,


Thanks for contacting support.

In order to accomplish your requirements, please try using following code snippet. For your reference, I have also attached the output generated over my end.

[C#]

Document doc = new
Document();<o:p></o:p>

doc.Pages.Add();

// Instatiate RadioButtonField object with page number as argument

Aspose.Pdf.Forms.RadioButtonField radio = new Aspose.Pdf.Forms.RadioButtonField(doc.Pages[1]);

radio.PartialName = "radio";

Aspose.Pdf.Forms.RadioButtonOptionField opt1 = new Aspose.Pdf.Forms.RadioButtonOptionField();

Aspose.Pdf.Forms.RadioButtonOptionField opt2 = new Aspose.Pdf.Forms.RadioButtonOptionField();

opt1.OptionName = "Item1";

opt2.OptionName = "Item2";

opt1.Width = 12;

opt1.Height = 12;

opt2.Width = 12;

opt2.Height = 12;

radio.Add(opt1);

radio.Add(opt2);

opt1.Caption = new Aspose.Pdf.Text.TextFragment("Item10");

opt1.Border = new Aspose.Pdf.Annotations.Border(opt1);

opt1.Border.Width = 1;

opt1.Border.Style = Aspose.Pdf.Annotations.BorderStyle.Solid;

opt1.Characteristics.Border = System.Drawing.Color.Yellow;

opt1.DefaultAppearance.TextColor = System.Drawing.Color.Red;

opt2.Caption = new Aspose.Pdf.Text.TextFragment("Item11");

opt2.Border = new Aspose.Pdf.Annotations.Border(opt1);

opt2.Border.Width = 1;

opt2.Border.Style = Aspose.Pdf.Annotations.BorderStyle.Solid;

opt2.Characteristics.Border = System.Drawing.Color.Yellow;

opt2.DefaultAppearance.TextColor = System.Drawing.Color.Red;

doc.Form.Add(radio);

// add RadioButton Option to paragraphs collection of first page of PDF file

doc.Pages[1].Paragraphs.Add(opt1);

// add RadioButton Option to paragraphs collection of first page of PDF file

doc.Pages[1].Paragraphs.Add(opt2);

doc.Save(“c:/pdftest/Resultantfile.pdf”);

Thanks for your Quick reply.


I got the issue fixed.

I have one more query. How to place radio buttons in certain position of the page.

I dint found any position related parameter in RadioButtonField Class.

Hi Nithin,


Thanks for the acknowledgement.

The RadioButton controls are added in default layout of PDF file i.e. Top-Left to Bottom-Right and it cannot be placed using obsolete positioning. However in order to accomplish this requirement, you may try using FloatingBox instance which honors Obsolete positioning and can be placed at any location in the page. So you need to create FloatingBox object, add radiobutton options to paragraphs collection of FloatingBox. Please try using following code snippet.

For your reference, I have also attached the output generated over my end.

[C#]

Document doc = new Document();<o:p></o:p>

Page page = doc.Pages.Add();

Aspose.Pdf.Forms.RadioButtonField radio = new Aspose.Pdf.Forms.RadioButtonField(doc.Pages[1]);

radio.PartialName = "radio";

Aspose.Pdf.Forms.RadioButtonOptionField opt1 = new Aspose.Pdf.Forms.RadioButtonOptionField();

Aspose.Pdf.Forms.RadioButtonOptionField opt2 = new Aspose.Pdf.Forms.RadioButtonOptionField();

opt1.OptionName = "Item1";

opt2.OptionName = "Item2";

opt1.Width = 12;

opt1.Height = 12;

opt2.Width = 12;

opt2.Height = 12;

radio.Add(opt1);

radio.Add(opt2);

opt1.Caption = new Aspose.Pdf.Text.TextFragment("Item10");

opt1.Border = new Aspose.Pdf.Annotations.Border(opt1);

opt1.Border.Width = 1;

opt1.Border.Style = Aspose.Pdf.Annotations.BorderStyle.Solid;

opt1.Characteristics.Border = System.Drawing.Color.Yellow;

opt1.DefaultAppearance.TextColor = System.Drawing.Color.Red;

opt2.Caption = new Aspose.Pdf.Text.TextFragment("Item11");

opt2.Border = new Aspose.Pdf.Annotations.Border(opt1);

opt2.Border.Width = 1;

opt2.Border.Style = Aspose.Pdf.Annotations.BorderStyle.Solid;

opt2.Characteristics.Border = System.Drawing.Color.Yellow;

opt2.DefaultAppearance.TextColor = System.Drawing.Color.Red;

doc.Form.Add(radio);

// add RadioButton Option to paragraphs collection of first page of PDF file

// doc.Pages[1].Paragraphs.Add(opt1);

// add RadioButton Option to paragraphs collection of first page of PDF file

// doc.Pages[1].Paragraphs.Add(opt2);

FloatingBox fb = new FloatingBox();

fb.Top = 100.0f;

fb.Left = 100.0f;

page.Paragraphs.Add(fb);

fb.Width = 50.0f;

fb.Paragraphs.Add(opt1);

fb.Paragraphs.Add(opt2);

// save PDF file

doc.Save("c:/pdftest/FloatingBox.pdf");